[plt-scheme] Re: newbie question

From: Thomas Chust (chust at web.de)
Date: Thu Feb 26 06:09:37 EST 2009

2009-02-25 donaldtc at yahoo.cn <donaldtc at yahoo.cn>:
> [...]
> 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.
> [...]

Hello,

I think one cannot say that the outer let doesn't take effect any
more. The outer let establishes a variable that is still strongly
reachable when the inner lambda is called.

However, two things are important to note: Every time
make-running-total is called, a new variable n is established and a
new closure is created by the inner lambda that uses this variable.
Every time the closure created by the inner lambda is called, the
value of the variable n it uses is changed. Therefore, although the
variable n still exists, it isn't bound to the same value any longer
after the first call of the closure returned by make-running-total.

cu,
Thomas


-- 
When C++ is your hammer, every problem looks like your thumb.


Posted on the users mailing list.