MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: & without #

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73066] Re: [mg73042] & without #
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Thu, 1 Feb 2007 02:54:49 -0500 (EST)
  • Reply-to: hanlonr at cox.net

Clear[f];

Array[f,5]

{f[1],f[2],f[3],f[4],f[5]}

Array[f,{2,4}]

{{f[1,1],f[1,2],f[1,3],f[1,4]},{f[2,1],f[2,2],f[2,3],f[2,4]}}

Array[f[x][#]&,5]

{f[x][1],f[x][2],f[x][3],f[x][4],f[x][5]}

Array[f[x,#]&,5]

{f[x,1],f[x,2],f[x,3],f[x,4],f[x,5]}

Array[f[#,x]&,5]

{f[1,x],f[2,x],f[3,x],f[4,x],f[5,x]}

In each case above, function used the arguments provided by Array

Array[f&,5]

{f,f,f,f,f}

Array[f&,{2,4}]

{{f,f,f,f},{f,f,f,f}}

function takes no arguments so the arguments provided by Array had no effect

Array[f[x]&,5]

{f[x],f[x],f[x],f[x],f[x]}

Array[f[x]&,{2,4}]

{{f[x],f[x],f[x],f[x]},{f[x],f[x],f[x],f[x]}}

f already had its argument and did not need or use arguments provided by Array. This is what you did with Random. Since you did not need additional arguments, you could have just as easily used Table rather than Array

Table[Random[Integer,10],{5}]

{8,2,10,7,3}

Table[Random[Integer,10],{2},{4}]

{{9,10,8,8},{10,7,3,3}}


Bob Hanlon

---- Kristen W Carlson <carlsonkw at gmail.com> wrote: 
> Hi,
> 
> From Built-in Functions/Cases, here is this (undocumented?) usage of &
> without #:
> 
> 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}
> 
> 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]}
> 
> 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".
> 
> Kris
> 


  • Prev by Date: Re: sort and positon matrix element help
  • Next by Date: Re: fundamental Integrate question
  • Previous by thread: Re: sort and positon matrix element help
  • Next by thread: Re: & without #