(display "ex-1.11\n") (define (f_rec n) (cond ((< n 3) n) (else (+ (f_rec (- n 1)) (* 2 (f_rec (- n 2))) (* 3 (f_rec (- n 3))))))) (display "(f_rec 10) = ") (display (f_rec 10)) (newline) (define (f_iter_step n a b c count) (cond ((= n count) c) (else (f_iter_step n b c (+ (* 3 a) (* 2 b) c) (+ count 1))))) (define (f_iter n) (cond ((< n 3) n) (else (f_iter_step n 0 1 2 2)))) (display "(f_iter 10) = ") (display (f_iter 10)) (newline) (newline) (display "ex-1.12") (newline) (define (pascal row col) (cond ((= row 1) 1) ((= col 1) 1) ((= col row) 1) (else (+ (pascal (- row 1) (- col 1)) (pascal (- row 1) col))))) (display "(pascal 6 2) = ") (display (pascal 6 3)) (newline) (newline) (display "ex-1.13") (newline) (display "I was not able to prove this.\n") (newline) (display "ex-1.14") (newline) (display "I did that on paper. See page 92 Bullet Journal 2018.") (newline) (newline) (display "ex-1.15") (newline) (define (cube x) (* x x x)) (define (p x) (- (* 3 x) (* 4 (cube x)))) (define (sine angle) (if (not (> (abs angle) 0.1)) angle (p (sine (/ angle 3.0))))) ; a. (/ 12.5 3) -> 4.16 -> 1.38888 -> 0.462 -> 0.154 -> 0.051 (count = 5) ; 12.5 / 3^n < 0.1 <=> 125 < 3^n <=> 4.39 < n (define (count-sine-calls value count) (if (> (abs value) 0.1) (count-sine-calls (/ value 3.0) (+ 1 count)) count)) (display "a) Calls for 12.5 = ") (display (count-sine-calls 12.5 0)) (newline) (display (sine (* 3.14 0.5))) (newline) ; b. What is the order of growth in space and number of steps ; (as a function of a) used by the process generated by the sine procedure when (sine a) is evaluated? ; O(log_3 a) (newline) (display "ex-1.16") (newline) (define (expt-rec x n) (cond ((= n 0) 1) (else (* x (expt-rec x (- n 1)))))) (define (expt-iter b counter product) (cond ((= counter 0) product) (else (expt-iter b (- counter 1) (* product b))))) (define (expt b n) (expt-iter b n 1)) (define (fast-expt b n) (cond ((= n 0) 1) ((even? n) (square (fast-expt b (/ n 2)))) (else (* b (fast-expt b (- n 1)))))) (define (even? n) (= (remainder n 2) 0)) (define (expt-fast b n) (expt-iter-fast b n 1)) (define (expt-iter-fast b n a) (cond ((= n 1) (* b a)) ((even? n) (expt-iter-fast (* b b) (/ n 2) a)) (else (expt-iter-fast b (- n 1) (* a b))))) (display "expt-fast 2 37 = ") (display (expt-fast 2 37)) (newline) (newline) (display "ex-1.17") (newline) (define (mul a b) (if (= b 0) 0 (+ a (mul a (- b 1))))) (define (double x) (+ x x)) (define (half x) (/ x 2)) (define (mul-rec a b) (cond ((= b 0) 0) ((even? b) (double (mul-rec a (half b)))) (else (+ a (mul-rec a (- b 1)))))) (display "mul-rec 17 53 = ") (display (mul-rec 17 53)) (newline) (newline) (display "ex-1.18") (newline) (define (mul-fast-iter b n a) (cond ((= n 0) a) ((even? n) (mul-fast-iter (double b) (half n) a)) (else (mul-fast-iter b (- n 1) (+ a b))))) (define (mul-fast a b) (mul-fast-iter a b 0)) (display "mul-fast 17 53 = ") (display (mul-fast 17 53)) (newline) (newline) (display "ex-1.19") (newline) (define (fib n) (fib-iter 1 0 0 1 n)) (define (fib-iter a b p q count) (cond ((= count 0) b) ((even? count) (fib-iter a b (+ (* q q) (* p p)) ; compute p' (+ (* q q) (* 2 p q)) ; compute q' (/ count 2))) (else (fib-iter (+ (* b q) (* a q) (* a p)) (+ (* b p) (* a q)) p q (- count 1))))) (display "fib 19 = ") (display (fib 19)) (newline) (newline) (display "ex-1.20") (newline) (define (gcd-naiv a b) (cond ((= a b) a) ((> a b) (gcd-naiv (- a b) b)) (else (gcd-naiv a (- b a))))) (display "gcd-naiv 60 14 = ") (display (gcd-naiv 60 14)) (newline) (define (gcd a b) (if (= b 0) a (gcd b (remainder a b)))) (display "gcd 60 14 = ") (display (gcd 60 14)) (newline) ; normaler order ; gcd 206 40 ; gcd 40 (r 206 40) ; zero? (r 206 40) ; zero? 6 ; gcd (r 206 40) (r 40 (r 206 40)) ; zero? (r 40 (r 206 40)) ; gcd (r 40 (r 206 40)) (r (r 206 40) (r 40 (r 206 40)))) ; etc. ; applicative order ; gcd 206 40 ; gcd 40 (r 206 40) ; gcd 40 6 ; gcd 6 (r 40 6) ; gcd 6 4 ; gcd 4 (r 6 4) ; gcd 4 2 ; gcd 2 (r 4 2) ; gcd 2 0 -> 2