18 hours ago
my attempt to do the exercises in sicp.
Wednesday, November 25, 2009
sicp exercise 3.11
; Exercise 3.11. In section 3.2.3 we saw how the environment model described the behavior of procedures with local state. Now we have seen how internal definitions work. A typical message-passing procedure contains both of these aspects. Consider the bank account procedure of section 3.1.1:
; (define (make-account balance)
; (define (withdraw amount)
; (if (>= balance amount)
; (begin (set! balance (- balance amount))
; "Insufficient funds"))
; (define (deposit amount)
; (set! balance (+ balance amount))
; (define (dispatch m)
; (cond ((eq? m 'withdraw) withdraw)
; ((eq? m 'deposit) deposit)
; (else (error "Unknown request -- MAKE-ACCOUNT"
; Show the environment structure generated by the sequence of interactions
; (define acc (make-account 50))
; ((acc 'deposit) 40)
; ((acc 'withdraw) 60)
; Where is the local state for acc kept? Suppose we define another account
; (define acc2 (make-account 100))
; How are the local states for the two accounts kept distinct? Which parts of the environment structure are shared between acc and acc2?
The local state for acc is kept in E1.
If another account acc2 is created, it will store the local state in a seperate environment.
The procedure bodies of withdraw, deposit and dispatch are shared between acc and acc2.