Re: Slots & Ampersands
- To: mathgroup at smc.vnet.net
- Subject: [mg74399] Re: Slots & Ampersands
- From: Albert <awnl at arcor.net>
- Date: Mon, 19 Mar 2007 22:09:06 -0500 (EST)
- References: <etlq8s$rab$1@smc.vnet.net>
Hi, > I've always regarded the Slot and Ampersand as being > inseparable in Mathematica (the Laurel & Hardy of > Mathematica?), > and was surprised to find one of them absent in > > Plot[Sin[x], {x, 0, 2Pi}, ColorOutput -> > (RGBColor[1, 0, 0]&)] > > [Help Browser under ColorOutput::colpc ,Examples] > > Question 1 > > Where is Laurel gone? Is he inside ColorOutput or > RGBColor? Laurel is not there, he is just not that useful... You should first learn that #& is just a shortcut for Function[Slot[1]], which makes clear tha # is a slot for an argument of the function, that is the first argument to be precise, which you could also write as #1. #2 would be the second argument and so on, ## is a (Slot)Sequence of all arguments. Since you can define Functions which don't need arguments or at least don't make use of them, the single & just means this: A function which doesn't make use of any of the arguments you give it. So it can go without the # very well. On the other hand you can define expressions with just # (Slot[n] that is) and no & or Function in it, e.g. when building a function body automatically. Mathematica will just not do anything with the slots in that case, I think. Example: f=Print["I am happy to ignore my arguments"]& (* check that it is indeed a Function and # is a Slot[]: *) FullForm[f] FullForm[#] (* call it with or without arguments: *) f[] f[x] f[1,3,4,{None,101}] (* define a function body and only later construct a Function from it:*) a = #1 + 4 ff = Evaluate[a] & ff[3] > Question 2 > > Are there other similar constructs in Mathematica? [No # ] As far as I know, the only construct where the character # (Slot[]) makes sense is Function, but I am willing to learn more... hth, albert