guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: about coroutines


From: Keith Wright
Subject: Re: about coroutines
Date: Mon, 19 Nov 2001 12:52:27 -0500

> Cc: address@hidden, address@hidden
> From: Marius Vollmer <address@hidden>
> 
> >    *bug2 - obscure memory leak in certain uses of call/cc
> 
> Confirmed as well.  This version of coroutines works:
> 
>     (define next (make-fluid))
> 
>     (define (run-coroutines . procs)
>       (with-fluids ((next (cdr procs)))
>         ((car procs))))
> 
>     (define (yield)
>       (call/cc (lambda (k)
>                  (let ((n (fluid-ref next)))
>                    (cond ((not (null? n))
>                           (fluid-set! next (append (cdr n) (list k)))
>                           ((car n))))))))

Just another random data point:

I changed the Knuth coroutine example I posted a couple of days ago thus:

  ;; |nextchar| - read next character skipping "blank columns".
;  (define (nextchar)
;    (let ((c (read-char)))
;      (if (char=? c #\space) (nextchar) c)))

  ;; generate infinite input
  (define nextchar
    (let* ((clist (string->list "Hello, world9!"))
           (cl clist))
      (lambda ()
        (if (null? cl)
            (set! cl clist))
        (let ((c (car cl)))
          (set! cl (cdr cl))
          c))))

It chugs along without any sign of slowing down.  ("Chugs", not "zips",
or "flies"; it also shows no sign of speeding up.)  So the use of fluid
is not essential to make things work.

-- 
     -- Keith Wright  <address@hidden>

Programmer in Chief, Free Computer Shop <http://www.free-comp-shop.com>
         ---  Food, Shelter, Source code.  ---



reply via email to

[Prev in Thread] Current Thread [Next in Thread]