#lang racket ; example of implementing your own delay/force ; a promise is a mutable pair ; if the first is 0, the second is a lambda () that when evaluated ; gives the value of the promise ; if the first is 1, the second is the actual value of the promise ; ; (define (pdelay x) (mcons 0 (lambda () x))) (define (pforce promise) (if (= 1 (mcar promise)) ; already got value memoized (mcdr promise) ; evaluate the promise and memoize it (begin (set-mcar! promise 1) (set-mcdr! promise ((mcdr promise))) (mcdr promise)) ) )