Blame


1 665c255d 2023-08-04 jrmu
2 665c255d 2023-08-04 jrmu
3 665c255d 2023-08-04 jrmu 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:
4 665c255d 2023-08-04 jrmu
5 665c255d 2023-08-04 jrmu (define (make-account-and-serializer balance)
6 665c255d 2023-08-04 jrmu (define (withdraw amount)
7 665c255d 2023-08-04 jrmu (if (>= balance amount)
8 665c255d 2023-08-04 jrmu (begin (set! balance (- balance amount))
9 665c255d 2023-08-04 jrmu balance)
10 665c255d 2023-08-04 jrmu "Insufficient funds"))
11 665c255d 2023-08-04 jrmu (define (deposit amount)
12 665c255d 2023-08-04 jrmu (set! balance (+ balance amount))
13 665c255d 2023-08-04 jrmu balance)
14 665c255d 2023-08-04 jrmu (let ((balance-serializer (make-serializer)))
15 665c255d 2023-08-04 jrmu (define (dispatch m)
16 665c255d 2023-08-04 jrmu (cond ((eq? m 'withdraw) (balance-serializer withdraw))
17 665c255d 2023-08-04 jrmu ((eq? m 'deposit) (balance-serializer deposit))
18 665c255d 2023-08-04 jrmu ((eq? m 'balance) balance)
19 665c255d 2023-08-04 jrmu ((eq? m 'serializer) balance-serializer)
20 665c255d 2023-08-04 jrmu (else (error "Unknown request -- MAKE-ACCOUNT"
21 665c255d 2023-08-04 jrmu m))))
22 665c255d 2023-08-04 jrmu dispatch))
23 665c255d 2023-08-04 jrmu
24 665c255d 2023-08-04 jrmu Then deposits are handled as with the original make-account:
25 665c255d 2023-08-04 jrmu
26 665c255d 2023-08-04 jrmu (define (deposit account amount)
27 665c255d 2023-08-04 jrmu ((account 'deposit) amount))
28 665c255d 2023-08-04 jrmu
29 665c255d 2023-08-04 jrmu Explain what is wrong with Louis's reasoning. In particular, consider what happens when serialized-exchange is called.