Re: Pure functions?

*To*: mathgroup at smc.vnet.net*Subject*: [mg93265] Re: Pure functions?*From*: Bill Rowe <readnews at sbcglobal.net>*Date*: Sat, 1 Nov 2008 05:12:11 -0500 (EST)

On 10/31/08 at 3:05 AM, siegman at stanford.edu (AES) wrote: >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. >--------------------- >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. Correct. >My interpretation is that f1 is also a pure function per the Wiki >definition, Correct. >The function f2 is not pure in the Wiki sense, Incorrect. >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. Once the values of x,y are fixed, it should be clear f2 is no different than f1. In fact, f1 is literally a specific instance of f2 with x = 3 and y = 4. Each selection for x,y defines a new function and all of these are pure functions. The fact you can write a function in terms of symbols instead of numbers does not change it from pure to impure. Contrast this with the functions Wikipedia gives as examples of impure functions. For example, the Mathematica function RandomReal[] gives a different result every time it is called without changing any parameters. This is key. Here, the information needed to determine what will result when RandomReal[] is executed is not available to you at all. You have no way to predict what RandomReal will return. In contrast, you specify f2 and know exactly what it will return all of the time. >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. Yes. In fact, many of the functions not considered pure functions in Mathematica meet the Wikipedia definition for pure function. For example, Sin[x] always computes the sine of x and clearly has no I/O side effect. But a function need not be unnamed to be a pure function in the Mathematica sense. That is both: Sin[#]& NormalDistribution[#1, #2]& have obvious names and are pure functions in the Mathematica sense. And your f2 above has a name, specifically f2. Instead of the function being unnamed, I see the focus as being on the unnamed arguments to the function. That is for Sin[x] the argument has a name, x and the function Sin[#]& is the function without a named argument.