3.42 Exercise 3.42
Suppose Ben Bitdiddle now wants to serialize the withdraw and deposit procedures outside of the definition of the dispatch function:
(define (make-account balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (let ((protected (make-serializer))) (let ((protected-withdraw (protected withdraw)) (protected-deposit (protected deposit))) (define (dispatch m) (cond ((eq? m 'withdraw) (protected withdraw)) ((eq? m 'deposit) (protected deposit)) ((eq? m ' balance) balance) (else (error "Unknown request -- MAKE-ACCOUNT" m)))) dispatch)))
This is a safe change to make. Performing multiple destructive updates at the same time will still not be allowed, giving the same guarantees without the performance impact of introducing a new procedure to the serialization set every time such an update is made.