<习题>第一章 构造过程抽象
Ch1.1 程序设计的基本元素
习题:
1.1
(+ 5 3 4) | 12 |
(- 9 1) | 8 |
(/ 6 2) | 3 |
(+ (*2 4) (- 4 6 )) | -16 |
(define a 3) | a=3 |
(define b (+ a 1 )) | b=4 |
(if (and (>b a) (< b ( * a b ))) b a) |
4 |
(cond ((= a 4) 6) ((= b 4) ( + 6 7 a)) (else 25)) |
16 |
(+ 2 ( if ( > b a ) b a )) | 6 |
(* (cond (( > a b ) a) (( < a b ) b) (else -1)) (+ a 1)) |
16 |
1.2
(/ (+ 5 (+ 4 (- 2 (- 3 (+ 6 ( / 4 5)))))) (* 3 (- 6 2) (- 2 7))
1.3
(define (min x y)
(if ( < x y ) x y ))
(define (larger-sum a b c)
(- (+ a b c) (min (min a b) c ))
1.4
对a与b的绝对值求和,即a+|b|
1.5
采用应用序,会先对运算对象进行求值,在(test 0 (p))中,即先会对(p)进行求值,然而(define (p) (p)),所以对(p)进行求值会陷入死循环;而采用正则序,知道实际需要运算对象的值的时候才会进行求值,在题目的运算中,先进行if判断后,直接返回0,避免了对(p)进行求值。
1.6
使用自定义的new-if进行求平方根运算时,按照应用序会先对运算对象进行求值,在对第三个参数的求值过程中会出现对其本身的递归调用,同1.5相似,也会陷入死循环。
1.7
按照比值进行判断,如果两者之积为0,则无法进行判断
(define (good-enough? guess x)
(if (= 0 ( * guess x ))
false
(< (abs (- ( abs (/ guess x )) 1)) 0.001)))
(if (= 0 ( * guess x ))
false
(< (abs (- ( abs (/ guess x )) 1)) 0.001)))
1.8
(define (cubt-iter guess x)
(define (cube guess) ( * guess guess guess))
(define (good-enough? guess x)
(if (= 0 ( * guess x ))
false
(< (abs (- ( abs (/ (cube guess) x )) 1)) 0.001)))
(define (improve guess x)
(/ (+ (/ x (* guess guess)) (* 2 guess)) 3))
(if (good-enough? guess x)
guess
(cubt-iter (improve guess x) x)))
(define (cube guess) ( * guess guess guess))
(define (good-enough? guess x)
(if (= 0 ( * guess x ))
false
(< (abs (- ( abs (/ (cube guess) x )) 1)) 0.001)))
(define (improve guess x)
(/ (+ (/ x (* guess guess)) (* 2 guess)) 3))
(if (good-enough? guess x)
guess
(cubt-iter (improve guess x) x)))
Ch1.2 过程与它们所产生的计算
习题:
1.9
(+ 4 5) (inc ( + 3 5 )) (inc ( inc ( + 2 5 ))) (inc ( inc ( inc ( + 1 5 )))) (inc ( inc ( inc ( inc ( + 0 5 ))))) (inc ( inc ( inc ( inc 5 )))) (inc ( inc ( inc 6 ))) (inc ( inc 7 )) (inc 8 ) 9 |
( + 4 5 ) ( + 3 6 ) ( + 2 7 ) ( + 1 8 ) ( + 0 9 ) 9 |
递归计算过程 | 迭代计算过程 |
1.10
( A 1 10 )
( A 0 ( A 1 9 ))
( A 0 ( A 0 ( A 1 8 )))
...
( A 0 ( A 0 ( A 0 ( A 0 ( A 0 ( A 0 ( A 0 ( A 0 ( A 0 ( A 1 1)))))))))))
( A 0 ( A 0 ( A 0 ( A 0 ( A 0 ( A 0 ( A 0 ( A 0 ( A 0 2))))))))))
( A 0 ( A 0 ( A 0 ( A 0 ( A 0 ( A 0 ( A 0 ( A 0 2^2)))))))))
( A 0 ( A 0 ( A 0 ( A 0 ( A 0 ( A 0 ( A 0 2^3))))))))
...
2^10
1024
( A 2 4 )
( A 1 ( A 2 3 ))
( A 1 ( A 1 ( A 2 2 )))
( A 1 ( A 1 ( A 1 ( A 2 1 ))))
( A 1 ( A 1 ( A 1 2 )))
( A 1 ( A 1 ( A 0 ( A 1 1))))
( A 1 ( A 1 ( A 0 2 )))
( A 1 ( A 1 4 ))
( A 1 2^4)
2^(2^4)
65536
( A 3 3 )
( A 2 ( A 3 2 ))
( A 2 ( A 2 ( A 3 1 )))
( A 2 ( A 2 2 ))
( A 2 ( A 1 ( A 2 1 )))
( A 2 ( A 1 2 ))
( A 2 ( A 0 ( A 1 1 )))
( A 2 ( A 0 2 )))
( A 2 4 )
65535
(define ( f n ) ( A 0 n ))
=>2*n
(define ( g n ) ( A 1 n ))
=>2^n
(define ( h n ) ( A 2 n ))
=>2^(2^(2^...(2^2))) 一共n个2
http://bigman.pw