MathGroup Archive 2003

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

Search the Archive

Re: Scoping, named patterns, local vs global variables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg44900] Re: Scoping, named patterns, local vs global variables
  • From: drbob at bigfoot.com (Bobby R. Treat)
  • Date: Sun, 7 Dec 2003 06:03:46 -0500 (EST)
  • References: <bqmqus$r95$1@smc.vnet.net> <bqpncl$931$1@smc.vnet.net> <bqs95b$i1b$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

>>Yet, I still want the local scoping behavior implied by employment
of named pattern variables.

No problem. Just make sure x has no value, like this...

x =.;
Clear[f]
f[x_] = x^2
f[3]
?f

or, if x MUST have a value, and x MUST be the name of your pattern,
then...

x = 1;
Block[{x}, Clear[f]; f[x_] = x^2;]
f[3]
?f

or use a different pattern name:

x = 1;
Clear[f]
f[neverGlobal_] = neverGlobal^2
f[3]
?f

If you want to use SetDelayed but evaluate the RHS now, you have the
same three choices where the scope of x is concerned, starting with...

x =.
Clear[f]
f[x_] := Evaluate[Integrate[ProductLog[x], x]]
?f

There are situations where Remove[x] might be required rather than x=.

Bobby

frankeye at cox.net (Frank Iannarilli) wrote in message news:<bqs95b$i1b$1 at smc.vnet.net>...
> 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.


  • Prev by Date: RE: Re: Scoping, named patterns, local vs global variables
  • Next by Date: Re: Re: Suggestion: Visualization of complex functions with Mathematica
  • Previous by thread: RE: Re: Scoping, named patterns, local vs global variables
  • Next by thread: RE: Re: Scoping, named patterns, local vs global variables