[R] functional (?) programming in r

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue Nov 18 01:33:39 CET 2008


Gabor Grothendieck wrote:
> On Mon, Nov 17, 2008 at 5:42 PM, Wacek Kusnierczyk
> <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
>   
>> i haven't seen any docs page that would explain such cases, so it's hard
>> to judge for me.  this might be an unforeseen side effect of the
>> intended behaviour of the promise mechanism.
>>     
>
> The R Language Definition manual that comes with R has a section on promises.
>   

on promises yes, but the question was whether the behaviour discussed
before is obvious and intended, or unforeseen.
haskell has lazy evaluation as well, and yet works differently in a
similar case. 

i suspect lapply internally for-loops over a variable needed by all the
promises, as stavros hinted, hence the outcome.  if the closure in which
the promises are forced contained the whole list 1:5, and the promises
pointed to its elements, the promise mechanism would not interfere with
the expected (by me, maybe not by r folks) result.  what i would
(r-naively) expect is that lapply for-loops over an index variable, and
each promise picks from the list a value at that index.  perhaps this
might be a suggestion for a change, but i think most users don't write
such weird code.

vQ



More information about the R-help mailing list