Skip to content

Instantly share code, notes, and snippets.

@eraserhd
Last active December 15, 2015 12:59
Show Gist options
  • Select an option

  • Save eraserhd/5263820 to your computer and use it in GitHub Desktop.

Select an option

Save eraserhd/5263820 to your computer and use it in GitHub Desktop.
Breaking my brain with continuations.
(define (make-generator proc)
(define restart #f)
(define (generator)
(call/cc
(lambda (return-c)
(define (return v)
(set! return-c (call/cc
(lambda (restart-c)
(set! restart restart-c)
(return-c v)))))
(if restart
(restart return-c)
(proc return)))))
generator)
(define g (make-generator
(lambda (return)
(return "Hello, ...")
(return "World!")
(return "This is a strange test")
(return "... of how you can hurt your brain")
(return "... with continuations.")
(let infinite-loop ()
(return " (now we return this forever) ")
(infinite-loop)))))
(display (g)) (newline)
(display (g)) (newline)
(display (g)) (newline)
(display (g)) (newline)
(display (g)) (newline)
(display (g)) (newline)
(display (g)) (newline)
(display (g)) (newline)
(display (g)) (newline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment