Blob


3 Exercise 3.45. Louis Reasoner thinks our bank-account system is unnecessarily complex and error-prone now that deposits and withdrawals aren't automatically serialized. He suggests that make-account-and-serializer should have exported the serializer (for use by such procedures as serialized-exchange) in addition to (rather than instead of) using it to serialize accounts and deposits as make-account did. He proposes to redefine accounts as follows:
5 (define (make-account-and-serializer balance)
6 (define (withdraw amount)
7 (if (>= balance amount)
8 (begin (set! balance (- balance amount))
9 balance)
10 "Insufficient funds"))
11 (define (deposit amount)
12 (set! balance (+ balance amount))
13 balance)
14 (let ((balance-serializer (make-serializer)))
15 (define (dispatch m)
16 (cond ((eq? m 'withdraw) (balance-serializer withdraw))
17 ((eq? m 'deposit) (balance-serializer deposit))
18 ((eq? m 'balance) balance)
19 ((eq? m 'serializer) balance-serializer)
20 (else (error "Unknown request -- MAKE-ACCOUNT"
21 m))))
22 dispatch))
24 Then deposits are handled as with the original make-account:
26 (define (deposit account amount)
27 ((account 'deposit) amount))
29 Explain what is wrong with Louis's reasoning. In particular, consider what happens when serialized-exchange is called.