(define (cef f) (let ((x '()) (y '())) (cons (lambda () (if (null? y) (set! y (f x))) y) ; getter (lambda (nx) (set! y '()) (set! x nx)) ; setter )))f is the function to be continuously evaluated. The function cef above, given f, returns a pair of functions, a getter and a setter. We presume that f never returns '() and that what f yields depends only on its argument. The getter always returns what ever f would yield given the most recent argument to the setter.
(define gs (cef (lambda (x) (display "Ugh ") (expt 10 x)))) ((cdr gs) 100) ((cdr gs) 10) ((car gs)) ⇒ Ugh 10000000000 ((car gs)) ⇒ 10000000000Note that the substantial work of computing 10100 was entirely avoided because no one asked for that value. 1010 was needed twice but computed (Ugh) only once.
Of course this version does not support mutation of x by machine store commands. Here is the more general pattern.