Re: Bug, quirk or expected behavior of Slot[]?
- To: mathgroup at smc.vnet.net
- Subject: [mg101805] Re: Bug, quirk or expected behavior of Slot[]?
- From: earthnut at web.de (Bastian Erdnuess)
- Date: Fri, 17 Jul 2009 05:06:18 -0400 (EDT)
- References: <h3n5q6$2bm$1@smc.vnet.net>
Salvatore Mangano <smangano at into-technology.com> wrote:
> Someone on the "Linked In" Mathematica User group pointed this out to me:
>
>
> In[26]:= Clear[y]
> Function[Module[{y=0},#===y]][y]
> Function[Module[{y=0},#1===y]][y]
> Function[t,Module[{y=0},t===y]][y]
>
> Out[27]= True
> Out[28]= True
> Out[29]= False
>
> It leads me to believe # is more like a placeholder for macro-like
> substitution than a formal parameter like t.
>
> Thoughts?
Hmm,
In[_]:= Function[ t, Module[ { y = 0 }, t === y ][ y$ ]
Out[_]= True
In the help to Function is written:
- The named formal parameters x_i in Function[ { x_1, ... }, body ]
are treated as local, and are renamed x_i$ when necessary to avoid
confusion with actual arguments supplied to the function.
- Function constructs can be nested in any way. Each is treated as a
scoping construct, with named inner variables being renamed if
necessary.
It might be the case that Function[ vars, body ] is considered to be a
scoping construct, but Function[ body ] not. (Since '#'s don't count as
'named inner variables' and so don't need to be 'scoped'?)
And as scoping construct it makes Modules -- another scoping construct,
to rename all local variables. ?
Bastian