Sicp练习解答
10 12 8 3 -2 6 19 0 b=4 16 6 16
1.4过程的行为是 a + |b|
a加上b的绝对值
(define (p) (p))
(define (text x y)
(if (= x 0)
0
y))
(text 0 (p))
首先对于 (define (p) (p)) 来说,无论是应用序或者正则序只要调用这个函数都会进入无限循环递归中
应用序会先对函数的各个参数求值,然后应用到函数中,对于 (text 0 (p)) 来说,应用序会先运算 (p) 直接把运算后的 (p) 应用到 (text x y) 的过程中,这样就会导致程序进入无限递归中去。
正则序则不一样,先不求参数的值,直到需要值的时候再求值,因为第一个参数是0,程序不会用到 (p) 所以直接返回0
Scheme 的解释器进入到无限递归中因此属于应用序
由于 Scheme 的解释器属于应用序,所以会先对传入 new-if 的两个参数进行求值,如果第二个参数不是理想值,所以 new-if 会无限递归下去,从而不能出正确值
在该方法中判断求值是否完成的充要条件是 “猜测的数的平方与被开方的数的差小于0.001” 转变为条件表达式
|guess^2 - x| < 0.001
-0.001 < guess^2 - x < 0.001
guess^2 - 0.001 < x < guess^2 + 0.001
0.0009 < x < 1.0001
对于 x > 1.0001
经过 (improve guess x)
之后会趋近于正确值
第一个计算过程:
(+ 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
该过程属于迭代
(A 1 10)
(A 0 (A 1 9))
(A 0 (A 0 (A 1 8)))
(A 0 (A 0 (A 0 (A 1 7))))
(A 0 (A 0 (A 0 (A 0 (A 1 6)))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 1 5))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 4)))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 3))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 2)))))))))
(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 4))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 8)))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 16))))))
(A 0 (A 0 (A 0 (A 0 (A 0 32)))))
(A 0 (A 0 (A 0 (A 0 64))))
(A 0 (A 0 (A 0 128)))
(A 0 (A 0 256))
(A 0 512)
1024
(A 1 10) = 2^10 = 1024
(A 1 n) = 2^n
(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 2^2))
(A 1 2^2^2)
2^2^2^2
65536
(A 2 4) = 2^2^2^2 = 2^16 = 65535
(A 2 n)相当于求2的n次幂
(A 3 3)
(A 2 (A 3 2))
(A 2 (A 2 (A 3 1)))
(A 2 (A 2 2))
(A 2 2^2)
(A 2 4)
2^2^2^2
65536
(A 3 3) = 2^2^2^2 = 2^16 = 65535
所以(f n) = 2 * n
(g n) = 2^n
(h n) = 2^2···^2