Re: & without #
- To: mathgroup at smc.vnet.net
- Subject: [mg73074] Re: & without #
- From: albert <awnl at arcor.de>
- Date: Thu, 1 Feb 2007 03:27:04 -0500 (EST)
- References: <epp68i$cv0$1@smc.vnet.net>
Hi, I think to fully understand the behaviour you need to learn about pure functions (lookup Function in the helpbrowser) and the evaluation of expressions. Both are covered in the documentation. > From Built-in Functions/Cases, here is this (undocumented?) usage of & > without #: Basically the combination of # and & are just a shortcut for Function[]. # is the argument of the function, or to be more precise the first argument, you can adress other arguments with #1, #2, #3, ... and ## is the sequence of all arguments given. For Function[] there is also the version with named arguments, where you can use arbitrary variables as arguments like in: Function[{x,y},x-y]. Also note that the body of the function, that is x-y in this case is _not_ evaluated, before you call the function with or without arguments. This you can see easily by just evaluating: Function[1+1] which will not be simplified to Function[2]! Of course it is possible to define a function which does not make use of its arguments, that is the body of the function does not contain any reference to the arguments of the function. This is what happens here: > L=Array[Random[Integer,10]&,20] > > {4,5,9,6,8,5,4,0,9,4,5,2,10,6,3,7,4,2,2,8} This is from the usage message of Array: Array[f, n] generates a list of length n, with elements f[i] so the result is a list of length 20 with elements (Random[Integer,10]&)[i]. If you evaluate this for any argument, it will return a random number between 0 and 10, and that's what the result is: a list of 20 random numbers between 0 and 10. Because the body of the function is evaluated only when the function is called, you get 20 different numbers in this case. > Here is what happens without the &: > > L=Array[Random[Integer,10],20] > > {3[1],3[2],3[3],3[4],3[5],3[6],3[7],3[8],3[9],3[10],3[11],3[12],3[13],3[ > 14],3[15],3[16],3[17],3[18],3[19],3[20]} here the result is a list of length 20 with elements 3[i], which comes by the fact that Random[Integer,10] is evaluated before it is used for the list generation and in your case happened to evaluate to 3. Now Array builds a list with 3[i] for i from 1 to 20, and that's exactly the result you have been seeing. Note that the 3 will change to any number between 0 and 10 when you reevaluate this definition of L. > Can someone who understands this please explain as completely as you > can, including how & and # work > together, given the behavior of & alone. As said before, & without # is just a function which does not make use of it's arguments, whether you give them to the function or not. > This might help and I also post it to help illuminate for those who > haven't seen this & usage; it is from Andrzej a while ago (Andrzej I > hope you don't mind): > > In general it means a constant function. if constant means that the result is independent of the arguments given, that's corrrect. > For example 3& will return 3 > with any argument. But the are at least two "special" functions, > which will work like "variable constants" when used in this way. One > of them is Random[]& (and various variants of it). Another is Unique > [symbol]&, which on every evaluation will produce a unique name based > on "symbol". There is nothing special about Random[] or Unique[] here, they just happen to be expressions which give a different result each time they are evaluated, there are many more, like for example Date[]. The 3 in 3& happens to always evaluate to 3, and (94+1) in (94+1)& happens to always evaluate to 95. Nevertheless the sum is calculated each time you evaluate the function, you can see what happens with: fun=(94+1)& Trace[fun[]] hth, albert