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: [mg44913] RE: [mg44895] Re: Scoping, named patterns, local vs global variables
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Tue, 9 Dec 2003 03:32:22 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: frankeye at cox.net [mailto:frankeye at cox.net]
To: mathgroup at smc.vnet.net
>Sent: Saturday, December 06, 2003 10:46 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg44913] [mg44895] Re: Scoping, named patterns, local vs global
>variables
>
>
>Oliver Friedrich <oliver.friedrich at tzm.de> wrote in message 
>news:<bqpncl$931$1 at smc.vnet.net>...
>> frankeye at cox.net (Frank Iannarilli) wrote in
>> news:bqmqus$r95$1 at smc.vnet.net: 
>> 
>> > Summary punchline:
>> > 
>> > x=1;
>> > f[x_]=x^2; 
>> > In: f[3]
>> > Out: 1  (and not 3^2=9, since x is global)
>> > 
>> > 
>> 
>> Hallo Frank,
>> 
>> I'm wondering which version you use. I checked out your 
>example above in 
>> 4.2 and I've got the result which we all expect and that is 
>according to 
>> the desired behaviour written in the handbook, i.e the x as 
>pattern name 
>> is treated local.
>> 
>> I'm keen on knowing the answer to your problem
>> 
>
>Using 5.0, Windows2000.  
>
>
>To respond to Jean-Michel and Bobby's point, yes, I understand that
>SetDelayed (:=) will behave in the manner I wish **as regarding**
>local scoping behavior of the named patterns (formal arguments).
>
>But there are times that I really want the immediate rhs evaluation
>offered by the Set (=) behavior, for example to avoid recomputing some
>"kernel" within the rhs upon repeated lhs evaluation.  Yet, I still
>want the local scoping behavior implied by employment of named pattern
>variables.  I tried to get both by doing:
>
>    f[x_]:=Evaluate[rhs(x)]
>
>but evidently the Evaluate[] extinguishes the local scoping, i.e., the
>global value of x is substituted immediately into the rhs.
>
>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.
>

This is part of a message I did not send:

...Perhaps you want to define f with current value of n, ok, you'r free to
do so:

In[12]:= x = 1;
        n = 2;
        Block[{x}, f[x_] = x^n];
        f[3]
Out[15]= 9

In[16]:= ?f
         Global`f
         f[x_] = x^2


But anyways, it would be better to make everything explicit at the point of
definition:

In[17]:= x = 1;
         Block[{x, n = 2}, f[x_] = x^n];
         f[3]
Out[19]= 9

In[20]:= ?f
          Global`f
          f[x_] = x^2

This also is a way, if the rhs contains a complicated expression which is
opportune to be evaluated partially before definition to be more performant
at time of "call".


--
Hartmut Wolf


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