Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

部分継続便利だなー。

(define (generator$ iterator$)
  (let ((cont-storage #f))
    (define (make-next cont)
      (lambda ()
        (let/cc return
          (step cont return))))
    (define (step cont return)
      (set! cont-storage
            (lambda (x cont*)
              (return x (make-next cont*))))
      (cont))
    (make-next
     (lambda ()
       (iterator$ (lambda (x)
                    (let/cc cont
                      (cont-storage x cont))))
       (cont-storage #f (lambda () #f))))))

(define itr (generator$ (cut for-each <> (list 1 2 3))))

(define (eat walker)
  (receive (v walker*) (walker)
    (when v (set! itr walker*))
    v))

;;;
(dotimes (i 3) (print (eat itr)))
;; => 1,2,3

;;;;;;;;;;;;;;;;;;
;;; partcont
;;;;;;;;;;;;;;;;;;

(use gauche.partcont)

(define (generator$ walker)
  (define (cont)
    (reset (walker (lambda (x) (shift k (set! cont k) x)))
           #f))
  (lambda () (cont)))

(define itr (generator$ (cut for-each <> '(1 2 3))))

;;;
(dotimes (i 3) (print (itr)))
;; => 1,2,3

後でくわしく