Blame


1 665c255d 2023-08-04 jrmu Exercise 3.42. Ben Bitdiddle suggests that it's a waste of time to create a new serialized procedure in response to every withdraw and deposit message. He says that make-account could be changed so that the calls to protected are done outside the dispatch procedure. That is, an account would return the same serialized procedure (which was created at the same time as the account) each time it is asked for a withdrawal procedure.
2 665c255d 2023-08-04 jrmu
3 665c255d 2023-08-04 jrmu (define (make-account balance)
4 665c255d 2023-08-04 jrmu (define (withdraw amount)
5 665c255d 2023-08-04 jrmu (if (>= balance amount)
6 665c255d 2023-08-04 jrmu (begin (set! balance (- balance amount))
7 665c255d 2023-08-04 jrmu balance)
8 665c255d 2023-08-04 jrmu "Insufficient funds"))
9 665c255d 2023-08-04 jrmu (define (deposit amount)
10 665c255d 2023-08-04 jrmu (set! balance (+ balance amount))
11 665c255d 2023-08-04 jrmu balance)
12 665c255d 2023-08-04 jrmu (let ((protected (make-serializer)))
13 665c255d 2023-08-04 jrmu (let ((protected-withdraw (protected withdraw))
14 665c255d 2023-08-04 jrmu (protected-deposit (protected deposit)))
15 665c255d 2023-08-04 jrmu (define (dispatch m)
16 665c255d 2023-08-04 jrmu (cond ((eq? m 'withdraw) protected-withdraw)
17 665c255d 2023-08-04 jrmu ((eq? m 'deposit) protected-deposit)
18 665c255d 2023-08-04 jrmu ((eq? m 'balance) balance)
19 665c255d 2023-08-04 jrmu (else (error "Unknown request -- MAKE-ACCOUNT"
20 665c255d 2023-08-04 jrmu m))))
21 665c255d 2023-08-04 jrmu dispatch)))
22 665c255d 2023-08-04 jrmu
23 665c255d 2023-08-04 jrmu Is this a safe change to make? In particular, is there any difference in what concurrency is allowed by these two versions of make-account ?