I'm trying to understand just how lazy evaluation in R works. Does it only apply to the evaluation of function arguments? Because that I understand, e.g.
f <- function(x = x, y = x*2) {
c(x, y)
}
f(2)
[1] 2 4
But in other languages, e.g. Haskell, lazy evaluation means that a function call only gets evaluated if it's ever actually used. So I would expect something like this to run in an instant:
g <- function(x) {
y <- sample(1:100000000)
return(x)
}
g(4)
But it clearly evaluates the sample
call even though its result doesn't get used.
Could somebody explain exactly how this works, or point me in the direction of where it is explained in detail?
Similar questions: