Skip to content

Instantly share code, notes, and snippets.

@c2d7fa
Last active August 21, 2021 18:31
Show Gist options
  • Save c2d7fa/412225a2c8b377d294625d0ca1512ac5 to your computer and use it in GitHub Desktop.
Save c2d7fa/412225a2c8b377d294625d0ca1512ac5 to your computer and use it in GitHub Desktop.
Generators in terms of call/cc
(define (generator p)
(set! gc #f) ; Continuation in generator
(lambda ()
(call/cc (lambda (return)
(if gc
(gc #f)
(p (lambda (value)
(call/cc (lambda (cgc)
(set! gc cgc)
(return value))))))))))
(define g
(generator (lambda (yield)
(define (loop i)
(yield i)
(loop (+ i 1)))
(loop 0))))
(print (g))
(print (g))
(print (g))
(print (g))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment