Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Why is y a local variable here?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg63169] Re: [mg63085] Why is y a local variable here?
  • From: "Steven T. Hatton" <hattons at globalsymmetry.com>
  • Date: Fri, 16 Dec 2005 07:22:26 -0500 (EST)
  • References: <200512130841.DAA08255@smc.vnet.net> <976C0928-EE8E-4FE2-824E-FE1F2C3636EF@omegaconsultinggroup.com>
  • Sender: owner-wri-mathgroup at wolfram.com

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


  • Prev by Date: Re: own package doesnt work correctly?
  • Next by Date: Re: Re: Mathematica Programmer vs. Programming in Mathematica
  • Previous by thread: Re: Why is y a local variable here?
  • Next by thread: Re: Why is y a local variable here?