Kon Lovett wrote:

(define (fib)
 (let loop ((a 1) (b 1))
   (set! fib (lambda () (loop b (+ a b))))
   a))


;; maybe non-portable outside of current chicken semantics?

Except for the caveat about some compilers, no. However the above doesn't "make" a generator, in the sense that it can be reused. To restart the sequence 'fib' must be rebound. Certainly not "disgusting", just of limited use.

I've used the idiom for more complex generators with multiple yield points but it quickly becomes tangled. I don't like the programming style that too many generators afford, so I don't do this so much. I usually use generator for generating ids where http://okmij.org/ftp/Scheme/monad-in-Scheme.html would use either a global variable or a state monad.


Sunnan



_______________________________________________
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to