chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] Re: Style Guide


From: Sunnan
Subject: Re: [Chicken-users] Re: Style Guide
Date: Wed, 07 Nov 2007 12:49:18 +0100
User-agent: Thunderbird 2.0.0.6 (X11/20071022)

Ivan Shmakov wrote:
>>>>>> Sunnan  <address@hidden> 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





reply via email to

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