0

I am following Hadley Wickham's Advanced R, and working through an example on lazy evaluation.

I first create an add function as :

> add <- function(x){
+     function(y) x+y
+ }

Then I use apply function on it as :

adders <- lapply(1:10, add)

Now according to the book, due to lazy evaluation in R, when I try,

adders[[1]](20) 

should yield answer as 10 + 20 = 30, but for me it gives the answer 21.

> adders[[1]](20)
[1] 21

Any suggestions on why is the lazy evaluation not working on expected lines ?

Sarang Manjrekar
  • 1,839
  • 5
  • 31
  • 61
  • 2
    Lazy evaluation has been changed after R 3.2.0. See here:http://stackoverflow.com/questions/16129902/explain-a-lazy-evaluation-quirk – Feng Mar 14 '17 at 14:57
  • https://github.com/wch/r-source/blob/trunk/doc/NEWS.Rd#L2748 – Roland Mar 14 '17 at 15:06

0 Answers0