Re: Why is y a local variable here?
- To: mathgroup at smc.vnet.net
- Subject: [mg63189] Re: Why is y a local variable here?
- From: Peter Pein <petsie at dordos.net>
- Date: Sat, 17 Dec 2005 03:46:21 -0500 (EST)
- References: <200512130841.DAA08255@smc.vnet.net> <976C0928-EE8E-4FE2-824E-FE1F2C3636EF@omegaconsultinggroup.com> <dnv444$oas$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Steven T. Hatton schrieb: > On Thursday 15 December 2005 11:27, Omega Consulting wrote: > >>I have a hard time understanding the purpose of your example. You say >>that you want to define >> >>sd[y_] = SDP[n - 1, y] >> >>in your function. But you want the global value of y to be used. If >>so, why have y as a parameter of sd. If the global value comes in, >>then sd will always return the same thing (n and y are fixed values). >> >>If you want the function to use the global value, don't make it a >>parameter of sd. > > > I didn't want Global`y to be used, I just didn't understand why it was being > hidden (renamed) when code using almost the same construct was seeing the > Global`y. > > >>The other part of your question is where does y$ come from, why isn't >>it y (which would then evaluate into the global y value). This is a >>subtle side-effect of how Mathematica scoping works. It is somewhat >>complicated and usually only appears when you nest one scoping >>construct inside another. In this case, the = for sd inside the := >>for SDP. The details are in section 2.7.4 of the Mathematica Book >>(Advanced Topic: Variables in Pure Functions and Rules). That >>describes the situation in a much better way than I could reproduce >>here. > > > That was my only question. Thank you. I will need to read that section > carefully a few times. I believe the replacement is due to there being an > unbound variable "n" within the sd[y_]=SDP[n-1,y];, and that variable is > changed during evaluation. The discussion seems to focus on pure functions, > but I suspect the same rules apply to Modules and other block structures as > well. That seems to be what the rather cryptic comment about lambda > expressions is about. > > Here is another nuance of the situation. The following is the correct > implementation of the algorithm. If I change it by making y local to the > Module, it does not us "x" in the result. Instead, the result contains the > instance variable of the Module. > > SDP[0, x_] := x > SDP[n_Integer?Positive, x_] := Module[{sd, srp = Sqrt[Prime[n]], py}, > sd[y_] = SDP[n - 1, y]; > Expand[sd[x + srp] sd[x - srp]]] > > SDP[5, x] > > Maeder makes in interesting observation at the bottom of page 221 in > _Programming in Mathematica_. > > Steven > Hi Steven, if you want to save evaluation time with the sd[]-construct, this will fail, because sd[] will be redefined with evevery call of SDP[]. In that case you should follow the standard procedure: SDP[0,x_]=x; SDP[n_,x_}:=SDP[n,x]=...
- References:
- Why is y a local variable here?
- From: "Steven T. Hatton" <hattons@globalsymmetry.com>
- Why is y a local variable here?