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: [mg44967] Re: Scoping, named patterns, local vs global variables
  • From: drbob at bigfoot.com (Bobby R. Treat)
  • Date: Thu, 11 Dec 2003 05:28:14 -0500 (EST)
  • References: <bqv1j0$svn$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

SetDelayed has attribute HoldAll, while Set only has attribute
HoldFirst. Without changing that, there's no way for Set to respect
scoping indicated on the LHS. That may be the ONLY difference between
the two, in effect.

Who knows how many behaviors would be affected, if such a fundamental
function were changed?

Anyway, as you say, it isn't difficult to get the behavior we want.

Bobby

"David Park" <djmp at earthlink.net> wrote in message news:<bqv1j0$svn$1 at smc.vnet.net>...
> It would be interesting if WRI or someone would give an explanation of why a
> pattern name on the lhs in a Set statement shouldn't override any previous
> definitions for the pattern name just as it does with SetDelayed. Is there a
> useful application for this behavior? This is an unintuitive trap that many
> fall into.
> 
> Still, if one wants to use Set (to avoid repeating a long calculation) there
> are simple solutions. Just don't set values for the pattern variable, or use
> a different pattern variable name, or clear the pattern variable first. It's
> not an impossible situation. One just has to take a little extra care.
> 
> David Park
> djmp at earthlink.net
> http://home.earthlink.net/~djmp/
> 
> 
> 
> From: Frank Iannarilli [mailto:frankeye at cox.net]
To: mathgroup 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: Compile
  • Next by Date: Semidefinite programming in Mathematica
  • Previous by thread: RE: Re: Scoping, named patterns, local vs global variables
  • Next by thread: Re: Scoping, named patterns, local vs global variables