11.4.2 Lazy Programming
Lazy evaluation enables programming constructs that are not possible with eager evaluation. For example, with lazy evaluation we can define a procedure that behaves like the if expression special form.
We first define true
and false
as procedures that take two parameters and output the first or second parameter:
(define false (lambda (a b) b))
Then, this definition defines a procedure with behavior similar to the if
special form:
With eager evaluation, this would not work since all operands would be evaluated; with lazy evaluation, only the operand that corresponds to the appropriate consequent or alternate expression is evaluated.
Lazy evaluation also enables programs to deal with seemingly infinite data structures. This is possible since only those values of the apparently infinite data structure that are used need to be created.
Suppose we define procedures similar to the Scheme procedures for manipulating pairs:
(define car (lambda (p) (p true)))
(define cdr (lambda (p) (p false)))
(define null false)
(define null? (lambda (x) (= x false)))
These behave similarly to the corresponding Scheme procedures, except in LazyCharme their operands are evaluated lazily. This means, we can define an infinite list:
With eager evaluation, (intsfrom 1)
would never finish evaluating; it has no base case for stopping the recursive applications. In LazyCharme, however, the operands to the cons
application in the body of intsfrom
are not evaluated until they are needed. Hence, (intsfrom 1)
terminates and produces a seemingly infinite list, but only the evaluations that are needed are performed:
1
LazyCharme> (car (cdr (cdr (cdr (intsfrom 1)))))
4
Some evaluations fail to terminate even with lazy evaluation. For example, assume the standard definition of listlength
:
(lambda (lst) (if (null? lst) 0 (+ 1 (listlength (cdr lst))))))
An evaluation of (length (intsfrom 1))
never terminates. Every time an application of listlength
is evaluated, it applies cdr
to the input list, which causes intsfrom
to evaluate another cons
, increasing the length of the list by one. The actual length of the list is infinite, so the application of listlength
does not terminate.
Lists with delayed evaluation can be used in useful programs. Reconsider the Fibonacci sequence from Chapter 7. Using lazy evaluation, we can define a list that is the infinitely long Fibonacci sequence:^{1}
(define fibos (fibogen 0 1))
The $n^{th}$ Fibonacci number is the $n^{th}$ element of fibos
:
(lambda (n)
(listgetelement fibos n)))
where listgetelement is defined as it was defined in Chapter 5.
Another strategy for defining the Fibonacci sequence is to first define a procedure that merges two (possibly infinite) lists, and then define the Fibonacci sequence recursively. The mergelists
procedure combines elements in two lists using an input procedure.
(lambda (lst1 lst2 proc)
(if (null? lst1) null
(if (null? lst2) null
(cons (proc (car lst1) (car lst2))
(mergelists (cdr lst1) (cdr lst2) proc))))))
We can define the Fibonacci sequence as the combination of two sequences, starting with the 0
and 1
base cases, combined using addition where the second sequence is offset by one position:
The sequence is defined to start with 0
and 1
as the first two elements. The following elements are the result of merging fibos
and (cdr fibos)
using the +
procedure. This definition relies heavily on lazy evaluation; otherwise, the evaluation of (mergelists fibos (cdr fibos) +)
would never terminate: the input lists are effectively infinite.
Exercise 11.3. Define the sequence of factorials as an infinite list using delayed evaluation.
Exercise 11.4.
Describe the infinite list defined by each of the following definitions. (Check your answers by evaluating the expressions in LazyCharme.)
a. (define p (cons 1 (mergelists p p +)))
b. (define t (cons 1 (mergelists t (mergelists t t +) +)))
c. (define twos (cons 2 twos))
d. (define doubles (mergelists (intsfrom 1) twos *))
Exercise 11.5. $\left[\star \right]$ $\left[\star \right]$ A simple procedure known as the Sieve of Eratosthenes for finding prime numbers was created by Eratosthenes, an ancient Greek mathematician and astronomer. The procedure imagines starting with an (infinite) list of all the integers starting from 2. Then, it repeats the following two steps forever:
 Circle the first number that is not crossed off; it is prime.
 Cross off all numbers that are multiples of the circled number.
To carry out the procedure in practice, of course, the initial list of numbers must be finite, otherwise it would take forever to cross off all the multiples of 2. But, with delayed evaluation, we can implement the Sieve procedure on an effectively infinite list.
Implement the sieve procedure using lists with lazy evaluation. You may find the listfilter
and mergelists
procedures useful, but will probably find it necessary to define some additional procedures.

This example is based on Abelson and Sussman, Structure and Interpretation of Computer Programs, Section 3.5.2, which also presents several other examples of interesting programs constructed using delayed evaluation. ↩