Inf and lazy evaluation
On May 15, 2012, at 07:25 , R. Michael Weylandt wrote:
The only place I know lazy evaluation really is visible and widely used is in the passing of function arguments. It's what allows magic like zz <- 1:5 plot(zz) to know your variable was called "zz."
This is actually *not* lazy evaluation. You could achieve the same effect just by passing the defining expression along with zz. Real lazy evaluation is seen in the arguments to switch() (and "if" once you realize that it is actually a function too) -- only the chosen expression is evaluated. Also, in function definitions, we have default arguments only sometimes evaluated, or not evaluated until some other computation has taken place; e.g., take a look at
args(hist.default)
function (x, breaks = "Sturges", freq = NULL, probability = !freq,
include.lowest = TRUE, right = TRUE, density = NULL, angle = 45,
col = NULL, border = NULL, main = paste("Histogram of", xname),
xlim = range(breaks), ylim = NULL, xlab = xname, ylab, axes = TRUE,
plot = TRUE, labels = FALSE, nclass = NULL, warn.unused = TRUE,
...)
NULL
and notice that you won't actually ever be setting xlim=range("Sturges"), which would result in an error. What will in fact happen is that you first get the conversion
breaks <- switch(breaks, sturges = nclass.Sturges(x),
`freedman-diaconis` = , fd = nclass.FD(x), scott = nclass.scott(x),
stop("unknown 'breaks' algorithm"))
(note, in passing, that lazy evaluation prevents the stop() from kicking in, if a known algorithm is given). Then later on, we have
breaks <- pretty(range(x), n = breaks, min.n = 1)
so by the time we need the xlim, breaks will be a numeric vector which you _can_ take the range of.
Peter Dalgaard, Professor Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com