Skip to content

Instantly share code, notes, and snippets.

@yamasushi
Last active December 17, 2015 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yamasushi/5530823 to your computer and use it in GitHub Desktop.
Save yamasushi/5530823 to your computer and use it in GitHub Desktop.
(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
(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)
1
2
3
#<eof>
--->()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment