Pure functions?
- To: mathgroup at smc.vnet.net
- Subject: [mg93204] Pure functions?
- From: AES <siegman at stanford.edu>
- Date: Fri, 31 Oct 2008 03:05:06 -0500 (EST)
- Organization: Stanford University
This is a follow-on to the thread "Notation using # with exponents and &", renamed to focus on the question in the Subject line. In asking questions about the term pure function, I'm not trying to be contentious or argumentative. I'm just trying to learn a bit about the concept. When I search on this term in Google, Wikipedia is the first hit that comes up, with the opening statement: -------------------- In computer programming, a function may be described as pure if both these statements about the function hold: 1. The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change as program execution proceeds, nor can it depend on any external input from I/O devices. 2. Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices. The result value need not depend on all (or any) of the argument values. However, it must depend on nothing other than the argument values. --------------------- I appreciate that Wikipedia is not always authoritative; and its coverage of this particular topic is neither lengthy nor particularly detailed. Still, it's what's in there, at the moment. The Mathematica documentation for & opens with: --------------------- 'body &' is a pure function. The formal parameters are # (or #1), #2, etc. --------------------- and then shortly thereafter: --------------------- When Function[body] or body& is applied to a set of arguments, # (or #1) is replaced by the first argument, #2 by the second, and so on. #0 is replaced by the function itself. --------------------- So let's consider the constructs: f1 = #1^3 + #2^4 & f2 = #1^x + #2^y & both of which function perfectly well (I think) as pure functions in the Mathematica sense. My initial (and admittedly naive) interpretation is that f1 is also a pure function per the Wiki definition, because it "always evaluates the same result value given the same argument value(s)" and "the function result value [does not] depend on any hidden information or state that may change as program execution proceeds" (unless of course someone goes so far as to redefine the meanings of "3" or of "4"!). The function f2 is not pure in the Wiki sense, however (at least, that's my again admittedly naive interpretation), because f2[arg1,arg2] can give very different results when used in different parts of a program, since its result depends on "hidden" information (hidden in some sense, anyway) about the values that may have been assigned to the parameters x and y elsewhere in the program. And, given the quotes from the Mathematica definition, it seems clear that x and y are not "formal parameters" or accessible "arguments" of the function f2. So, the result of f2 does clearly "depend on [something] other than [just] the argument values". Finally, Mathematica seems to put some focus on the _unnamed_ (or "anonymous") property of its pure functions, while the Wiki statement makes no mention at all of this. So, what _is_ the real story on "pure functions"?