[R] lazy evaluation question

markleeds at verizon.net markleeds at verizon.net
Mon Jan 19 04:02:13 CET 2009


I've been going back to old difficult R-list "evaluation" emails that I 
save  in order to understand evaluation better and below still confuses 
me. Could someone explain why A) works and B) doesn't. A variant of 
below is in the  Pat's Inferno book also but I'm still not clear on what 
is happening. Thanks.

f <- function() {
   # FORCING i here doesn't help
   i*i
}

F <- function(i) {
   force(i)
   print(f())
   }

A) THIS WORKS
for ( i in 1:4 ) {
   F(i)
}

B) THIS DOESN'T
lapply(1:4,function(i) F(i))




More information about the R-help mailing list