[plt-scheme] Re: newbie question

From: Jeremy Price (donomii at gmail.com)
Date: Thu Feb 26 05:02:41 EST 2009

Pretty much.  Note that you actually have three different 'n's created
by the (let ([n 0]), because you call the let 3 times

> and when I call ha
>
>    (let ([n 0])
>
> will not take effect any more.

Yep, you're overwriting the value of n in memory with the (set!).  You
might also like to try:
(define ha (make-running-total))
(define hb (make-running-total))
(define hc (make-running-total))
(ha)
(hb)(hb)
(hc)(hc)(hc)
(ha)
etc

On Wed, Feb 25, 2009 at 5:10 PM, donaldtc at yahoo.cn <donaldtc at yahoo.cn> wrote:
> Thank you all.
> I think I've found the answer.
> When I use
>
> (define ha (make-running-total))
>
> ha actually is an instance of the procedure
>
>      (lambda ()
>        (let ([m n])
>          (set! n (+ n 1))
>          (list m n)))
>
> and when I call ha
>
>    (let ([n 0])
>
> will not take effect any more.
>
> This is why I cannot use (make-running-total) to call the procedure
> but ((make-running-total)).
>
> Am I right?
>
> On Feb 21, 11:26 am, "donal... at yahoo.cn" <donal... at yahoo.cn> wrote:
>> With the following program
>>
>> #lang scheme
>> (define make-running-total
>>   (lambda ()
>>     (let ([n 0])
>>       (lambda ()
>>         (let ([m n])
>>           (set! n (+ n 1))
>>           (list m n))))))
>> ((make-running-total))
>> ((make-running-total))
>> (define ha (make-running-total))
>> (ha)
>> (ha)
>>
>> why the result is
>>
>> (0 1)
>> (0 1)
>> (0 1)
>> (1 2)
>>
>> It seems when I call (ha), the n is a static variable.
>> _________________________________________________
>>   For list-related administrative tasks:
>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


Posted on the users mailing list.