Re: Why is y a local variable here?
- To: mathgroup at smc.vnet.net
- Subject: [mg63125] Re: Why is y a local variable here?
- From: "Steven T. Hatton" <hattons at globalsymmetry.com>
- Date: Thu, 15 Dec 2005 03:06:34 -0500 (EST)
- References: <200512130841.DAA08255@smc.vnet.net> <dnopmf$2j5$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
ggroup at sarj.ca wrote:
> On Tuesday, December 13, 2005 at 03:41 GMT -0500, Steven T. Hatton wrote:
> <snip>
>> I do not understand why y is not "contaminated" by the global value when
>> it appears in sd[y_] = SDP[n - 1, y];. Can someone explain this?
>
> Don't you need to use SetDelayed instead of Set? In other words,
> doesn't the syntax for a function definition need to be something
> like: sd[y_]:=SDP[x-1, y]; or sd=Function[y, SDP[x-1,y]; ?
No. The idea is to stack recursive calls to SDP, and assign the result of
popping the stack to sd[y_] which is then used in the subsequent
Expand[sd[x + srp] sd[x - srp]]. Evaluating the following should show what
happens in terms of recursion:
Clear[SDP, x, y]
SDP[0, x_] := x
SDP[n_Integer?Positive, x_] :=
Module[
{sd, srp = Sqrt[Prime[n]], py},
Print["Push >>>>>>>>>>>>"];
Information[sd];
sd[y_] = SDP[n - 1, y];
Print["Pop <<<<<<<<<<<<<"];
Information[sd];
Expand[sd[x + srp] sd[x - srp]]
]
SDP[5, x]
--
The Mathematica Wiki: http://www.mathematica-users.org/
Math for Comp Sci http://www.ifi.unizh.ch/math/bmwcs/master.html
Math for the WWW: http://www.w3.org/Math/
- References:
- Why is y a local variable here?
- From: "Steven T. Hatton" <hattons@globalsymmetry.com>
- Why is y a local variable here?