从 Section 2.9. Assignment of The Scheme Programming Language 中看到thunk这个概念,
Local state is sometimes useful for caching computed values or allowing a computation to be evaluated lazily , i.e., only once and only on demand. The procedure lazy below accepts a thunk , or zero-argument procedure, as an argument. Thunks are often used to “freeze” computations that must be delayed for some reason, which is exactly what we need to do in this situation. When passed a thunk t , lazy returns a new thunk that, when invoked, returns the value of invoking t . Once computed, the value is saved in a local variable so that the computation need not be performed again. A boolean flag is used to record whether t has been invoked and its value saved.
(define lazy (lambda (t) (let ([val #f] [flag #f]) (lambda () (if (not flag) (begin (set! val (t)) (set! flag #t))) val))))
The procedure lazy below accepts a *thunk* , or zero-argument procedure,