[plt-scheme] newbie question

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat Feb 21 10:45:59 EST 2009

Here is an evaluation according to the rules of HtDP [http:// 
www.htdp.org/2003-09-26/Book/curriculum-Z-H-47.html#node_chap_38]

> ;; --- global immutable def
> (define make-running-total
>   (lambda ()
>     (let ([n 0])
>       (lambda ()
>         (let ([m n])
>           (set! n (+ n 1))
>           (list m n))))))
>
> ;; --- program
> ((make-running-total))
> ((make-running-total))
> (define ha (make-running-total))
> (ha)
> (ha)
>
> ==>
>
> (((lambda ()
>     (let ([n 0])
>       (lambda ()
>         (let ([m n])
>           (set! n (+ n 1))
>           (list m n)))))))
>
> ((make-running-total))
> (define ha (make-running-total))
> (ha)
> (ha)
>
> ==>
>
> ((let ([n 0])
>       (lambda ()
>         (let ([m n])
>           (set! n (+ n 1))
>           (list m n)))))
>
> ((make-running-total))
> (define ha (make-running-total))
> (ha)
> (ha)
>
> ==>
>
> (define n1 0)
> ((lambda ()
>    (let ([m n1])
>      (set! n1 (+ n1 1))
>      (list m n1))))
>
> ((make-running-total))
> (define ha (make-running-total))
> (ha)
> (ha)
>
> ==>
>
> (define n1 0)
> (let ([m n1])
>   (set! n1 (+ n1 1))
>   (list m n1))
>
> ((make-running-total))
> (define ha (make-running-total))
> (ha)
> (ha)
>
> ==>
>
> (define n1 0)
> (let ([m 0])
>   (set! n1 (+ n1 1))
>   (list m n1))
>
> ((make-running-total))
> (define ha (make-running-total))
> (ha)
> (ha)
>
> ==>
>
> (define n1 0)
> (define m1 0)
> (set! n1 (+ n1 1))
> (list m1 n1)
>
> ((make-running-total))
> (define ha (make-running-total))
> (ha)
> (ha)
>
> ==>
>
> (define n1 0)
> (define m1 0)
> (set! n1 (+ 0 1))
> (list m1 n1)
>
> ((make-running-total))
> (define ha (make-running-total))
> (ha)
> (ha)
>
> ==>
>
> (define n1 1)
> (define m1 0)
> (list m1 n1)
>
> ((make-running-total))
> (define ha (make-running-total))
> (ha)
> (ha)
>
> ==>
>
> (define n1 1)
> (define m1 0)
> (list 0 1) ;;; <------- this is a value, time to reduce the next  
> expression
>
> ((make-running-total))
> (define ha (make-running-total))
> (ha)
> (ha)
>

It is easy to continue from here and to confirm your result. Just  
keep in mind that every time you lift a let-binding for an assignable  
variable, rename it to a new name.

-- Matthias











On Feb 20, 2009, at 10:26 PM, donaldtc 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



Posted on the users mailing list.