;;;;;; #1 ;;;;;; 3.38 ;initial balance 100 ;(set! balance (+ balance 10)) ;(set! balance (- balance 20)) ;(set! balance (- balance (/ balance 2))) ;;; a. ;six possible sequences to compute: ;(set! balance (+ balance 10)) ;(set! balance (- balance 20)) ;(set! balance (- balance (/ balance 2))) ;balance will ends up with (100+10-20)/2 which is 45 ;(set! balance (- balance 20)) ;(set! balance (+ balance 10)) ;(set! balance (- balance (/ balance 2))) ;balance will ends up with (100-20+10)/2 which is 45 ;(set! balance (- balance (/ balance 2))) ;(set! balance (- balance 20)) ;(set! balance (+ balance 10)) ;balance will ends up with (100/2)-20+10 which is 40 ;(set! balance (- balance (/ balance 2))) ;(set! balance (+ balance 10)) ;(set! balance (- balance 20)) ;balance will ends up with (100/2)+10-20 which is 40 ;(set! balance (+ balance 10)) ;(set! balance (- balance (/ balance 2))) ;(set! balance (- balance 20)) ;balance will ends up with ((100+10)/2-20) which is 35 ;(set! balance (- balance 20)) ;(set! balance (- balance (/ balance 2))) ;(set! balance (+ balance 10)) ;balance will ends up with ((100-20)/2+10) which is 50 ;therefore, four possible ending balance: 50, 45, 40, and 35 ;;; b. ;please refer to the separate sheet turned in ;;;;;; 3.39 ;four values will remain: 101, 121, 11, and 100 ;110 is eliminated because (lambda () (* x x)) and (lambda () (set! x (+ x 1))) cannot ;occur concurently, but (lambda () (set! x ((s (lambda () (* x x)))))) and ;(s (lambda () (set! x (+ x 1)))) can occur concurently, so 11 and 100 will remain. ;101 and 121 will remain because the order of evaluation ;;;;;; 3.40 ;possible ways of execution are: ;(10*10)^3 = 1000000 ;(10^3)^2 = 1000000 ;(10*(10*10)*(10*10)) = 100000 ;(10*10*(10*10)) = 10000 ;(10*(10^3)) = 10000 ;(10*10) = 100 ;(10^3) = 1000 ;possible values are: 100, 1000, 10000, 100000, 1000000 ;if we serialized procedures, only two ways remain ;(10*10)^3 = 1000000 ;(10*10*10)^2 = 1000000 ;but they only produce one value, that is 1000000 ;;;;;; 3.42 ;it is a safe change to make, but there are subtle differences. ;one is that withdraw and deposit are serialized, but not the protected-withdraw ;or protected-deposit. Yet, it makes no difference at this point, because there ;is no set! statements in protected-withdraw and protected-deposit. ;;;;;; 3.44 ;Ben Bitdiddle's code works fine, and Louis Reasoner's statement is incorrect ;The difference between transfer and exchange is the local variable difference ;in the exchange procedure. difference is calculated based on both account, but ;the variable amount in transfer has nothing to do with the two account. Once ;amount is defind, it's ok to do withdraw from the from-account and modify the ;to-account. As long as we don't modify the SAME account concurrently, there won't ;be any conflict. And the definition of make-account guarantee that no procdure can ;modify the SAME account concurrently. So, we say that Louis Reasoner's worry is ;not neccessary ;;;;;; 3.46 ;please refer to the separate sheet turned in ;;;;;; 3.50 (define (stream-map proc . argstreams) (if (stream-null? (car argstreams)) the-empty-stream (cons-stream (apply proc (map stream-car argstreams)) (apply stream-map (cons proc (map stream-cdr argstreams)))))) ;;;;;; 3.51 ;copy the code from book for testing purpose (define stream-null? null?) (define the-empty-stream '()) (define (cons-stream a b) (cons a (delay b))) (define (stream-car s) (car s)) (define (stream-cdr s) (force (cdr s))) (define (delay arg) (lambda () arg)) (define (force arg) (arg)) (define (display-line x) (newline) (display x)) (define (show x) (display-line x) x) (define (stream-ref s n) (if (= n 0) (stream-car s) (stream-ref (stream-cdr s) (- n 1)))) (define (stream-enumerate-interval low high) (if (> low high) the-empty-stream (cons-stream low (stream-enumerate-interval (+ low 1) high)))) (define (stream-map proc s) (if (stream-null? s) the-empty-stream (cons-stream (proc (stream-car s)) (stream-map proc (stream-cdr s))))) ;STk> (define x (stream-map show (stream-enumerate-interval 0 10))) ; ;0 ;1 ;2 ;3 ;4 ;5 ;6 ;7 ;8 ;9 ;10x ;STk> (stream-ref x 5) ;5 ;STk> (stream-ref x 7) ;7 ;;;;;; 3.52