Ivan Shmakov wrote:
>>>>>> Sunnan  <[EMAIL PROTECTED]> writes:
>  > (define (fib)
>  >   (let loop ((a 1) (b 1))
>  >     (set! fib (lambda () (loop b (+ a b))))
>  >     a))
>
>         This `set!' isn't necessary, compare:
>
> (define fib
>   (let ((a 1) (b 1))
>     (lambda ()
>       (let ((saved a))
>         (set! a b)
>         (set! b (+ saved b))
>         saved))))


Yes, this is the longer, classic style. As a fib generator with only
two counters, it works fine, but you have to save the entire state "by
hand" with a set!  for each original variable, and you also introduce
an extra variable for the "swap". Your version is probably faster, and
probably more familiar to many programmers.

I was just fascinated by the two perverse ideas of
1. freezing a delayed call to a named let
2. resetting/redefining the very function we're in (similar to some call/cc tricks)

>         Both variants should be quite portable.  (Especially on
>         non-compilers.)
>

Thanks.

Sunnan



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

Reply via email to