-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex-3.48.scm
48 lines (46 loc) · 1.58 KB
/
ex-3.48.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(define (make-account-and-serializer balance)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(let ((balance-serializer (make-serializer))
(id (generate-id)))
(define (dispatch m)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
((eq? m 'balance) balance)
((eq? m 'serializer) balance-serializer)
((eq? m 'id) id)
(else (error "Unknown request -- MAKE-ACCOUNT"
m))))
dispatch))
(define generate-id
(let ((id 0)
(serializer (make-serializer)))
(serializer
(lambda ()
(set! id (+ id 1))
id))))
; (define (serialized-exchange account1 account2)
; (let ((serializer1 (account1 'serializer))
; (serializer2 (account2 'serializer)))
; (let ((s-exchange (if (< (account1 'id) (account2 'id))
; (serializer2 (serializer1 exchange))
; (serializer1 (serializer2 exchange)))))
; (s-exchange
; account1
; account2))))
(define (serialized-exchange account1 accout2)
(define (s-exchange account1 account2)
(let ((serializer1 (account1 'serializer))
(serializer2 (account2 'serializer)))
((serializer1 (serializer2 exchange))
account1
account2)))
(if (< (account1 'id) (account2 'id))
(s-exchange account2 account1)
(s-exchange account1 account2)))