delay construct is used together with the procedure
implement lazy evaluation or call by need.
(delay expression) returns an object called a
promise which at some point in the future can be asked (by
force procedure) to evaluate
expression, and deliver the resulting value.
The effect of expression returning multiple values
(delay-force expression) is conceptually similar to
(delay (force expression)),
with the difference that forcing the result
delay-force will in effect result in a tail call to
while forcing the result of
(delay (force expression))
might not. Thus
iterative lazy algorithms that might result in a long series of chains of
can be rewritten using
delay-force to prevent consuming
unbounded space during evaluation.
procedure forces the value of a promise
If no value has been computed for the promise, then a value is
computed and returned. The value of the promise must be cached (or
"memoized") so that if it is forced a second time, the previously
computed value is returned.
Consequently, a delayed expression is evaluated using the parameter
values and exception handler of the call to
requested its value.
is not a promise, it may be returned unchanged.
(force (delay (+ 1 2))) ==> 3
(let ((p (delay (+ 1 2))))
(list (force p) (force p)))
==> (3 3)
(delay (cons n (next (+ n 1)))))))
(lambda (stream) (car (force stream))))
(lambda (stream) (cdr (force stream))))
(head (tail (tail integers)))
promise? procedure returns
#t if its argument is a promise, and
#f otherwise. Note
that promises are not necessarily disjoint from other Scheme types such
make-promise procedure returns a promise which, when forced, will return
obj. It is similar to
delay, but does not delay
its argument: it is a procedure rather than syntax.
If obj is already a promise, it is returned.