1
$\begingroup$

After Wikipedia, if a function is pure, then:

[it] always evaluates the same result value given the same argument value(s).

So: if a function, let's call it f, calls another function, g, then it's behavior clearly depends on the structure of the function g. Should f take g as an argument in order to be pure?

$\endgroup$
2
  • $\begingroup$ This is probably opinion based, but if I were to write down the semantics, each function would get an implicit argument with the environment where it can look up things like the function $g$. $\endgroup$
    – adrianN
    Jul 26, 2016 at 10:05
  • $\begingroup$ I'm not sure what computation context you're considering, but usually g would be free inside of f. It wouldn't be an argument though, at least not in the traditional meaning of the word $\endgroup$
    – pdexter
    Jul 26, 2016 at 13:49

3 Answers 3

2
$\begingroup$

No, f doesn't have to take g as an argument. Wikipedia's definition is a bit informal. Also informally, the behavior of a pure function is also allowed to depend on the code of the program. Or, to put it another way, the code of the program is considered fixed, not something we vary, so we don't try to track dependences on it.

So, something like f(x) = 2*g(x) is fine: if g is pure, f will be too.

$\endgroup$
2
  • $\begingroup$ By that definition, global constants are fine as well, correct? $\endgroup$
    – TLW
    Jul 28, 2016 at 2:32
  • $\begingroup$ @TLW, yes, that's right. Please, if you have a follow-up question, please post a new question, rather than using comments for that. thank you! $\endgroup$
    – D.W.
    Jul 28, 2016 at 6:48
1
$\begingroup$

No. For an algebraic structure over a set you fix the set of function symbols and the set of variables from the start. Then you can think of functions as constants. (Technically constants are functions with zero argument but anyway.) Therefore calling (pure) functions does not disrupt the purity.

$\endgroup$
1
$\begingroup$

The fact that f calls g doesn't influence the purity. All that's required is that for a given input to f, f always returns the same output. That means g must be pure. It also means if someone changes g so it's impure, then f becomes impure, too. But at that point it's a different function.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.