Permalink
Browse files

lazy evaluation functions

  • Loading branch information...
1 parent 9ca5206 commit 00306193e1077ae1af72f7c988017852de608b00 @cpylua committed Apr 15, 2012
Showing with 23 additions and 0 deletions.
  1. +23 −0 lib/core.scm
View
@@ -510,6 +510,7 @@
(define sort merge-sort)
+; continuation
(define call/cc call-with-current-continuation)
(define (values . vals)
@@ -518,3 +519,25 @@
(define (current-continuation)
(call/cc (lambda (cc) (cc cc))))
+
+; delay evaluation
+(define (make-promise proc)
+ (let ((result-ready? #f)
+ (result #f))
+ (lambda ()
+ (if result-ready?
+ result
+ (let ((x (proc)))
+ (if result-ready?
+ result
+ (begin (set! result-ready? #t)
+ (set! result x)
+ result)))))))
+
+(define-macro delay
+ (lambda (exp)
+ `(make-promise (lambda () ,exp))))
+
+(define (force promise)
+ (promise))
+

0 comments on commit 0030619

Please sign in to comment.