Skip to content
Prev 294606 / 398502 Next

Inf and lazy evaluation

On May 15, 2012, at 07:25 , R. Michael Weylandt wrote:

            
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
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.