Last active
August 21, 2021 18:31
-
-
Save c2d7fa/412225a2c8b377d294625d0ca1512ac5 to your computer and use it in GitHub Desktop.
Generators in terms of call/cc
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
(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