[plt-scheme] Re: yield-oriented sequence constructor

From: Doug Orleans (dougorleans at gmail.com)
Date: Fri Mar 21 21:19:34 EDT 2008

On Fri, Mar 21, 2008 at 5:47 PM, Doug Orleans <dougorleans at gmail.com> wrote:
>  (define (make-yield-sequence thunk)
>   (make-do-sequence
>    (lambda ()
>      (let ((done (gensym)))
>        (values
>         (lambda (pos) (apply values (cdr pos)))
>         (lambda (pos) (call/cc (car pos)))
>         (let/cc return
>           (parameterize ((current-yield-proc
>                           (let loop ((return return))
>                             (lambda vals
>                               (current-yield-proc
>                                (loop
>                                 (let/cc continue
>                                   (return (cons continue vals)))))))))
>             (thunk)
>             (yield done)))
>         (lambda (pos) (or (null? (cdr pos)) (not (eq? (cadr pos) done))))
>         void
>         void)))))

Here's a much simpler version of make-yield-sequence, though it seems
kinda like voodoo to me.  I'm wondering if I can just lift this yield
to the top level and get rid of the parameter altogether, but then how
do I install the default behavior in a top-level prompt?

(require scheme/control)
(define (make-yield-sequence thunk)
  (define (yield . vals) (control k (cons k vals)))
   (lambda ()
      (lambda (pos) (apply values (cdr pos)))
      (lambda (pos) (prompt ((car pos))))
       (parameterize ((current-yield-proc yield))
       (control k #f))

--dougorleans at gmail.com

Posted on the users mailing list.