[R] Scope?

Luke Tierney luke at stat.umn.edu
Mon Feb 11 16:08:43 CET 2002


On Mon, Feb 11, 2002 at 02:08:29PM +0100, Göran Broström wrote:
> Thanks to Doug Bates and Ray Brownrigg, who both referred to 
> 'lazy evaluation' as an argument for not allowing my construct.
> However, I'm not completely convinced. As I understand it, when
> a variable pops up (on the 'right hand side') in an expression in a 
> function,  R  looks for it in the following order:
> 
> 1. Local (to the function) variable?
> 2. Formal argument?
> 3. In the defining environment?
> 
> The potential problem occurs when the answer to  1.  is 'No' and the
> answer to  2.  is 'Yes', and no value was given to  x  in the call.
> Then the current procedure obviously is to look at the default, and go
> thru steps  1.-3.  again to find it. (Am I correct?) Then  x=x  is found,
> and the 'recursive default ...' is detected. But I mean that when looking 
> for the default among the formal arguments, *the current one must be 
> excluded*, and the search continue to  3., if necessary.
> 
> What have I overlooked?
> 
> Göran
> 

A call establishes only a local frame with the defining environment as
its parent.  At the start of a call all formal arguments are given
bindings to deferred evaluations, either of the supplied argument
where the deferred evaluation will use the caller's environment, or of
default expressions that will be evaluated in the local environment.
When the value of a variable is requested its binding is looked up in
the environment and a deferred evaluation is carried out if necessary.
If the deferred evaluation needs its value to find its value, you get
an error.  [This is simplified a bit but should capture the essence.]

For your example

    ex = function(x = x) x

the evaluation of the call ex() is roughly equivalent to

   env <- new.env(parent = environment(ex)) # create the call frame
   assign("x", delay(x, env), env)          # insert binding for
                                            # x with deferred evaluation of
                                            # the default expression
   eval(body(ex), env)                      # evaluate the body in the new env

If you paste this into R you get the same error as when evaluating ex().

Hope that helps.

luke

-- 
Luke Tierney
University of Minnesota                      Phone:           612-625-7843
School of Statistics                         Fax:             612-624-8868
313 Ford Hall, 224 Church St. S.E.           email:      luke at stat.umn.edu
Minneapolis, MN 55455 USA                    WWW:  http://www.stat.umn.edu
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list