[R] Scope?

Göran Broström gb at stat.umu.se
Mon Feb 11 14:08:29 CET 2002


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

On 9 Feb 2002, Douglas Bates wrote:

> Göran Broström <gb at stat.umu.se> writes:
> 
> > I find the following behaviour slightly disturbing:
> > 
> > > x = 6
> > > ex = function(x = x) x
> > > ex()
> > Error in ex() : recursive default argument reference
> > 
> > Can't  R  distinguish between formal and actual arguments? (Just kidding!)
> > Seriously, what bad things could happen if I was allowed to get the 
> > answer  6  here?
> 
> You would lose lazy evaluation.  The default value of an argument is
> evaluated at the time that the argument is first accessed during
> function evaluation, not at the time of definition of the function.
> That allows you to define default values of arguments in terms of
> other values calculated within the function.
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 

-- 
 Göran Broström                      tel: +46 90 786 5223
 professor                           fax: +46 90 786 6614
 Department of Statistics            http://www.stat.umu.se/egna/gb/
 Umeå University
 SE-90187 Umeå, Sweden             e-mail: gb at stat.umu.se

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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