Skip to content
Prev 43192 / 63424 Next

delayedAssign changing values

On Apr 27, 2012, at 00:10 , ghostwheel wrote:

            
That's not possible. It involves evaluating an expression, and there is no limit to what side effect this can have.
Just don't do that, then.... However, lazy evaluation _per se_ does cause nightmares, or at least surprising behavior. My favorite one (because it actually involves a relevant piece of statistics) is

loglike <- function(x,n) function(p) dbinom(x, n, p, log=TRUE)
n <- 10
x <- 7
ll <- loglike(x, n)
x <- 1
curve(ll) # max at 0.1


which has the issue that x (and n too) is not evaluated until the ll function is called, at which time it may have been changed from the value it had when ll was created.
They'll have to be rather contrived, but printing is one, and perhaps maintaining a count of function calls could be another.
It's actually the _normal_ way to create a promise, namely binding actual arguments to formal arguments.  It is just that some trickery is used in order to make the situation visible. 

I agree that the example looks a bit out of place, though. Perhaps there ought to be a help page on lazy evaluation and a reference to it?  (Any volunteers?)