SICP/ex-3_38-xx.scm

79 lines
1.8 KiB
Scheme
Raw Normal View History

2020-12-27 20:49:49 +01:00
(load "util.scm")
(display "\nex-3.38\n")
2020-12-28 19:14:07 +01:00
(display "[answered]\n")
2020-12-27 20:49:49 +01:00
2020-12-28 19:14:07 +01:00
; Peter Paul Mary
; 110 90 45
; Peter Mary Paul
; 110 55 35
; Mary Peter Paul
; 50 60 40
; Mary Paul Peter
; 50 30 40
; Paul Mary Peter
; 80 40 50
; Paul Peter Mary
; 80 90 45
(display "\nex-3.39\n")
(display "[answered]\n")
; (define x 10)
; (define s (make-serializer))
; (parallel-execute (lambda () (set! x ((s (lambda () (* x x))))))
; (s (lambda () (set! x (+ x 1)))))
; 101: P1 sets x to 100 and then P2 increments x to 101.
; 121: P2 increments x to 11 and then P1 sets x to x times x.
; 100: P1 accesses x (twice), then P2 sets x to 11, then P1 sets x.
; 11: P2 accesses x, then P1 sets x to 100, then P2 sets x.
; My friends on Scheme-Wiki agree with this solution so I feel good about this.
(display "\nex-3.40\n")
(display "[answered]\n")
; (define x 10)
; (parallel-execute (lambda () (set! x (* x x)))
; (lambda () (set! x (* x x x))))
; 10 * 10 = 100
; 10 * 10 * 10 = 1'000
; 10 * 1000 = 10'000
; 10 * 10 * 100 = 10'000
; 10 * 100 * 100 = 100'000
; 10^6 = 1'000'000
; (define x 10)
; (define s (make-serializer))
; (parallel-execute (s (lambda () (set! x (* x x))))
; (s (lambda () (set! x (* x x x)))))
; 10^2 = 100 -> 100^3 = 1000000
; 10^3 = 1000 -> 1000^2 = 1000000
(display "\nex-3.41\n")
(display "[answered]\n")
; Ben's concern does not apply. Say, we have a withdraw and a balance check.
; Depending on the order of execution the balance check would show the balance
; before or after the withdraw. This behavior is expectected and would not be
; migated by serializing balance.
(display "\nex-3.42\n")
(display "[answered]\n")
; This should work without issues in all cases. I don't have a good mental
; model of how the serializer works, yet.
(display "\nex-3.43\n")
(display "\nex-3.44\n")
2020-12-27 20:49:49 +01:00