MathGroup Archive 2003

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

Search the Archive

Re: Re: Scoping, named patterns, local vs global variables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg44896] Re: [mg44895] Re: Scoping, named patterns, local vs global variables
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Sun, 7 Dec 2003 06:03:43 -0500 (EST)
  • References: <bqmqus$r95$1@smc.vnet.net> <bqpncl$931$1@smc.vnet.net> <200312060945.EAA18382@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 6 Dec 2003, at 18:45, Frank Iannarilli wrote:

>
> I do like what The Book declares/implies regarding local scoping
> behavior for Set; otherwise, if I instead wanted the global value to
> override local scoping, why then would I bother to use the named
> pattern variable on the lhs and rather just do:
>     f[x]=x^2;
> or
>     f[_]=x^2;
>
>
> Thanks, all for your comments thus far.
>
>
>
As far as I can tell all the Mathematica Book references you brought up 
refer either to using  scoping constructions such as Function or With . 
Taking your example:

x=1;

f=Function[x,x^2];

f[3]

9

Moreover, the Mathematica Book quite explicitly tells you *not to 
expect* what you claim it " declares/implies regarding local scoping 
behavior for Set". Here is the relevant passage form the same section 
you have been quoting:

When you apply a rule such as f[x_] -> rhs, or use a definition such as 
f[x_] := rhs, Mathematica implicitly has to substitute for x everywhere 
in the expression rhs. It effectively does this using the /. operator. 
As a result, such substitution does not respect scoping constructs. 
However, when the insides of a scoping construct are modified by the 
substitution, the other variables in the scoping construct are renamed.

Note the phrase "insides of a scoping construct".


Andrzej Kozlowski
Chiba, Japan
http://www.mimuw.edu.pl/~akoz/


  • Prev by Date: Re: Scoping, named patterns, local vs global variables
  • Next by Date: Re: How to stop a kerne calculationl without stopping the frontend
  • Previous by thread: Re: Scoping, named patterns, local vs global variables
  • Next by thread: Re: Scoping, named patterns, local vs global variables