Created
May 10, 2015 18:21
-
-
Save beoliver/6c5f6db2a1c88c9631ef to your computer and use it in GitHub Desktop.
scheme thunks (delayed evaluation)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; delay and force are defined in r5rs, | |
;; but if we were defining them for ourselves | |
(define-syntax my-delay | |
(syntax-rules () | |
((_ expr) | |
(lambda () expr)))) | |
(define (my-force expr) | |
(expr)) | |
(define (not-a-delay expr) | |
;; expr will be evaluated before the closure | |
(lambda () expr)) | |
;; leys assume we have an infinite proc | |
(define (infinite-proc x) (infinite-proc x)) | |
(define my-thunk (my-delay (infinite-proc 10))) | |
;; DON'T DO THIS | |
(define this-wont-work (not-a-delay (infinite-proc 10))) | |
;; streams --- | |
(define-syntax my-lazy-cons | |
(syntax-rules () | |
((_ x xs) | |
(cons x (my-delay xs))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment