sicp-ex-3.40



<< Previous exercise (3.39) | Index | Next exercise (3.41) >>


meteorgan

  
  
 1,000,000: p1 set x to 100, then p2 set x to x*x*x. or p2 set x to 1,000, then p1 set x to x*x. 
 10,000: p1 get x = 10, then p2 set x to 1000, then p1 compute 10*1000. or p2 get x, compute x*x = 100, then p1 set x to 100, then p2 set x to 100*100. 
 100: p1 compute x*x = 100, then p2 set x to 10*10*10, then p1 set x to 100. 
 100,000: p2 get x = 10, then p1 set x to 100, then p2 set x to 10*100*100. 
 1000: p2 compute 10*10*10 = 1000, then p1 set x to 100, then p2 set x to 1000. 
  
 after serializing, only 1,000,000 exists. 
  

Elaboration on the same answer:


P1: (lambda () (set! x (* x x)))
P2: (lambda() (set! x (* x x x)))

** "x1", "x2", "x3" below refer to the first, second, and third arguments of the respective procedures

P1 events:
a) access x1
b) access x2
c) new value = x1 * x2
d) set! x to new value

P2 events:
v) access x1
w) access x2
x) access x3
y) new value = x1 * x2 * x3
z) set! x to new value

significant event sequences and their results:
** (P1) refers to event sequence "abcd", (P2) refers to event sequence "vwxyz"
1. (P1)(P2) => 1,000,000
2. (P2)(P1) => 1,000,000
3. a(P2)bcd => 10,000
4. v(P1)wxyz => 100,000
5. vw(P1)xyz => 10,000
6. abc(P2)d => 100
7. vwxy(P1)z => 1,000

Unique results:
[100, 1000, 10000, 100000, 1000000]

After serializing P1 and P2:
Only event sequence 1 and 2 remain:
[1000000]