Skip to content
Back to formatted view

Raw Message

Message-ID: <494AEB6E.3010409@biostat.ku.dk>
Date: 2008-12-19T00:31:42Z
From: Peter Dalgaard
Subject: understanding recursive functions
In-Reply-To: <494AE7C1.3060504@idi.ntnu.no>

Wacek Kusnierczyk wrote:
> Wacek Kusnierczyk wrote:
>> it's a pity that <<- is not explained in ?`<<-` clearly enough:
>>
>> "  The operators '<<-' and '->>' cause a search to made through the
>>      environment for an existing definition of the variable being
>>      assigned.  If such a variable is found (and its binding is not
>>      locked) then its value is redefined, otherwise assignment takes
>>      place in the global environment.  Note that their semantics differ
>>      from that in the S language, but are useful in conjunction with
>>      the scoping rules of R.  See 'The R Language Definition' manual
>>      for further details and examples."
>>
>>   
> 
> the description is further imprecise as to what happens if such a
> variable is found but is locked:
> 
> foo = function() {
>    x = 0
>    lockBinding("x", environment())
>    (function() x <<- 1)()
>    unlockBinding("x", environment())
> }
> 
> foo()
> # error
> 
> if analysed logically (IF (found AND not locked THEN assign) ELSE assign
> globally), the description is wrong. 

Yes.

If such a variable is found  then its value is redefined (unless its 
binding is locked), otherwise assignment take place in the global 
environment.

-- 
    O__  ---- Peter Dalgaard             ?ster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907