RE: Re: Scoping, named patterns, local vs global variables
- To: mathgroup at smc.vnet.net
- Subject: [mg44898] RE: [mg44895] Re: Scoping, named patterns, local vs global variables
- From: "David Park" <djmp at earthlink.net>
- Date: Sun, 7 Dec 2003 06:03:44 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
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.