-
-
Save shirok/5530942 to your computer and use it in GitHub Desktop.
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
(use gauche.generator) | |
; | |
#| | |
Scheme:generatorとdoとwhile | |
http://practical-scheme.net/wiliki/wiliki.cgi?Scheme%3agenerator%e3%81%a8do%e3%81%a8while#H-17qe7ru | |
|# | |
(define (generator-generate proc) | |
(define next #f) | |
(define return #f) | |
(lambda () | |
(let/cc break ;; break = generatorがcallされた後に相当する継続 | |
(set! return break) ;; 環境を書き換え | |
(if (not next) | |
(begin | |
(proc | |
(^x ;; yield | |
(let/cc cc ;; cc = yieldがcallされた後に相当する継続 | |
(set! next cc) ;; 環境を書き換え | |
(return x)))) ;; breakの後に待ち構えている継続をcall | |
(return (eof-object)) ) | |
(next) ) ;; yieldの後に待ち構えている継続をcall | |
))) | |
(define x (generator-generate (^[y] (y 1) (y 2) (y 3) ))) | |
($ print $ x) | |
($ print $ x) | |
($ print $ x) | |
($ print $ x) | |
(set! x (generator-generate (^[y] (y 1) (y 2) (y 3) ))) | |
($ print "--->" $ generator->list x) |
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
1 | |
2 | |
3 | |
#<eof> | |
--->() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment