AtlatestRepositorysigil-r7rs
sigil-r7rs / tree / src / schemelazy.sgl
1
;;; (scheme lazy) - R7RS Lazy Evaluation Library2
;;;3
;;; Lazy evaluation with promises. Defer computation until explicitly needed4
;;; with `force`. Promises cache their result so repeated forcing is efficient.5
;;;6
;;; See: [R7RS Small](https://small.r7rs.org/) §4.2.58
(define-library (scheme lazy)9
(export delay10
delay-force11
force12
make-promise13
promise?)15
(begin16
;; delay and delay-force are syntaxes provided by the prelude18
;;; Force evaluation of a promise.19
;;;20
;;; If the promise has not been forced before, evaluates its expression21
;;; and caches the result. Returns the cached value on subsequent calls.22
;;;23
;;; ```scheme24
;;; (define p (delay (begin (display "computing...") 42)))25
;;; (force p) ; prints "computing...", returns 4226
;;; (force p) ; returns 42 (no output, cached)27
;;; ```28
(%set-docstring! force)30
;;; Create a promise from an already-computed value.31
;;;32
;;; Unlike `delay`, does not defer computation. The value is stored33
;;; immediately.34
;;;35
;;; ```scheme36
;;; (define p (make-promise 42))37
;;; (force p) ; => 4238
;;; (promise? p) ; => #t39
;;; ```40
(%set-docstring! make-promise)42
;;; Test if an object is a promise.43
;;;44
;;; ```scheme45
;;; (promise? (delay 42)) ; => #t46
;;; (promise? (make-promise 1)) ; => #t47
;;; (promise? 42) ; => #f48
;;; ```49
(%set-docstring! promise?)))