Re: Scoping, named patterns, local vs global variables
- To: mathgroup at smc.vnet.net
- Subject: [mg44895] Re: Scoping, named patterns, local vs global variables
- From: frankeye at cox.net (Frank Iannarilli)
- Date: Sat, 6 Dec 2003 04:45:33 -0500 (EST)
- References: <bqmqus$r95$1@smc.vnet.net> <bqpncl$931$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
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.
- Follow-Ups:
- Re: Re: Scoping, named patterns, local vs global variables
- From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
- Re: Re: Scoping, named patterns, local vs global variables