Re: & without #
- To: mathgroup at smc.vnet.net
- Subject: [mg73079] Re: & without #
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Thu, 1 Feb 2007 03:47:28 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <epp68i$cv0$1@smc.vnet.net>
Kristen W Carlson wrote:
> Hi,
>
> From Built-in Functions/Cases, here is this (undocumented?) usage of &
> without #:
Nothing really undocumented here. The symbol '&' is a shortcut for the
function *Function*. The symbol '#' is another form for *Slot*.
Now, a pure function may accept parameters (or variables) or may not
accept parameter. The latter case implies that we cannot use *Slot* or
the # symbol. Therefore, we cannot call it with specific arguments. This
does not mean that the function must return or do always the same thing.
To understand the difference, have a look at the full form to both
expression:
> 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}
In[1]:=
HoldForm[Array[Random[Integer,10]&,20]]//FullForm
Out[1]//FullForm=
HoldForm[Array[Function[Random[Integer,10]],20]]
Above, we are building an array of /functions/ that accept no argument,
yet return some random values.
In[2]:=
% // ReleaseHold // Trace
[output deleted]
> 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]}
In[3]:=
HoldForm[Array[Random[Integer,10],20]]//FullForm
Out[3]//FullForm=
HoldForm[Array[Random[Integer,10],20]]
Above, we are building an array of random values.
In[4]:=
% // ReleaseHold // Trace
[output deleted]
> Can someone who understands this please explain as completely as you
> can, including how & and # work
> together, given the behavior of & alone. And is this documented anywhere?
>
> 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. 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".
Hope this helps.
Regards,
Jean-Marc