+ 2
[Solved] Some questions about how sources define functions and pureness.
I've been reading q&a on SL, and comments in Python Core and intermediate, and opened some links that have been provided there and read them, some from Quora included. Now just when I think I'm understanding it I start bumping into expressions that give me trouble, such as, a pure function doing no I\O (whichever direction that slash leans), it being like a math function, etc When I say sin(x) x is input and sin(x) is obviously the output, why do they even mention i-o when there's a whole module full of it? Do they mean math functions used in computer languages? Character exceeding the question limit, the rest is down in a post.
11 Answers
+ 2
Allow me to suggest another implementation of abs() that uses a pure function; single return and no branching:
#include <iostream>
#include <math.h>
using namespace std;
int myAbs(int a) {
return sqrtl(a*a);
}
int main() {
cout<<myAbs(-3);
return 0;
}
Output: 3
Just trying to twist your mind by implementing a discontinuous function with a continuous one!
+ 2
The reason the Tuesday function is impure is because it relies on time. Whether time is an impure function is actually kind of a deep question, but its probably impure because its always changing and not mathematically the same like abs value. Your abs value of -3 is always 3, but time is never the same. Random is another strange one. But they are strange for different reasons. Thanks for making me think.
+ 1
What about piecewise functions, how're those not math functions and why would a site use a conditional in function definition and say the result depends on the condition(hence impure) , when, all things equal, that won't change either. Absolute value is a piecewise function yet is called pure. (I have no problem with it being pure, it just seems to contradict the definitions I see)
Not all math functions are one to one and some are implicit. I don't understand what they mean.
**********************************************
I understand that it changes nothing outside of itself, given the same arguments, it returns the same value
Is this enough to go by?
**********************************************
+ 1
madeline Thank you. This is weird, if I may:
Tuesday could be a function where the days of the week could be iterated over based on another input and if it's Tuesday in mod 7, it could return something.
So you're not actually talking about the absolute time but the operation-procedure time maybe, the Big-O that I haven't been into yet?
The thing is isn't it just a Boolean for all g(x) knows? Doesn't care what's outside, returns x+1 if Tuesday and x else. (Also I gather that if Tuesday() is impure, so is g(x) for calling it?)
I do understand what Brian said but I perceive what you said to be something different.
+ 1
Brian By the way please feel absolutely free to suggest anything you want, and think is useful, as you see fit đ
+ 1
Korkunç the Terrible Frusciante
I guess I was trying to convey that time itself will always be an impure function because it will never return the same value. But I was commenting on the fact that the function for time would be a very difficult one to create, and I have no idea how it would be created, which really made me think a lot. Tuesday may/may not be impure, I am honestly not sure! I was just kind of amazed at the function for time, and kind of stopped there.
Big O notation is completely different than impure/pure functions. It's a measurement of how efficient your program is. Good luck in your studies, you're coming up with some good questions.
+ 1
madeline Thank you, I feel like less of a nuisance now :-)
I know it's different, I superficially know the concept, I thought it was possible that you didn't literally mean time but that if Tuesday() is a function where number of operations changed, that would then reflect on g(x) where it's called, changing g(x) for every input, that in turn changing the num of ops for the whole code,thus qualifying as impure.
When we don't know what we don't know, everything becomes a possibility and trying to ascertain there's no misunderstanding on our part becomes an issue.
Thank you for your elaboration :)
+ 1
I think you could transform g(x) into a pure function by adding a datetime parameter to g(x), and add it to any time-dependent functions that it calls. Then it would give one-for-one return values for the inputs.
def g(x, t):
if is Tuesday(t):
... etc.
0
Jay Matthews, Thank you. But:
http://cardiffmathematicscodeclub.github.io/tutorial/2015/10/18/introduction-to-functional-programming-in-JUMP_LINK__&&__python__&&__JUMP_LINK-p1.html
(code with Tuesday)
def g(x):
if is Tuesday():
return x +1
else:
return x
says g(x) is impure in the above link.
How's it any different than x<0? It's a piecewise function, the function called is only introduced by a condition?
I said I have no oroblem abs being pure, why aren't all piecewise functions pure?
Also by I/O do they mean prompting it inside the definition? (https://www.quora.com/Whats-the-difference-between-pure-and-non-pure-functions-in-programming)
0
Does this mean that there could be something outside the function, in the code, which could give random values depending on which Tuesday() could get called or not, for the same x?
OK thank you very much. I think I see it now.
(Although by def that change would not be due to this function, but rather Tuesday()? arrgh)
0
Brian Thank you, yeah that's another way that hadn't occurred to me at the time of asking.
May I ask if the bottom line is a pure function is a single return function were f(x) never changes?
Could we say that whatever happens with an impure function is a direct outcome of being in violation of the aforesaid condition?