Skip to content

Instantly share code, notes, and snippets.

@beoliver
Created May 10, 2015 18:21
Show Gist options
  • Save beoliver/6c5f6db2a1c88c9631ef to your computer and use it in GitHub Desktop.
Save beoliver/6c5f6db2a1c88c9631ef to your computer and use it in GitHub Desktop.
scheme thunks (delayed evaluation)
;; 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