MathGroup Archive 2008

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

Search the Archive

Re: Scoping question



Hi Yaroslav,

the first "outer x is 1" comes from the application of:

(Print["outer x is ", x]; f2[#]) &

to the first element of Range[2]. The second from the application to the 

second element of Range[2].

The "outer x is 2" come from: f1[x + 1].

If you want to see how the sequence in which the functions are called, 

you may e.g. use Trace. E.g.:

Trace[f1[1], f1[_] | f2[_]]

hope this helps, Daniel



Yaroslav Bulatov wrote:

> The code below defines a function with parameter x. The body contains

> another function definition which uses this parameter. Why do "inner x

> is" and "outer x is" lines start reporting different values at one

> point?

> 

> f1[x_] := (

>   f2[y_] := (Print["inner x is ", x]; If[x + 1 < 3, f1[x + 1], 1]);

>   (Print["outer x is ", x]; f2[#]) & /@ Range[2];

>   )

> f1[1]

> 




  • Prev by Date: Re: Summation question
  • Next by Date: Re: Summation question
  • Previous by thread: Re: Scoping question
  • Next by thread: Re: Scoping question