ex1-32 要求利用过程返回值给出一个用于计算 f(f (x))这样的函数过程
ex1-33要求计算一个函数的平滑函数g(x)=(f(x-dx)+f(x)+f(x+dx))/3
并利用ex1-32给出的过程进行多次的平滑运算以下,给出代码
(define (smooth f)
(lambda (x)
(/ (+ (f (- x .001)) (f x) (f (+ x .001))) 3)))
(define (repeated f x)
(lambda (times)
(if (= times 0) x
((repeated f (f x)) (- times 1)))))
(define (n-folds f x times)
((repeated (smooth f) x)times))
ex1-34要求利用newton求根方式 给出(newton (cubic a b c) 1) 来计算x^3+a*x^2+b*x+c的根
以下是代码
(define (f x) (- (* x x x) 3))
(define (deriv f dx)
(lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))
(define (Newton f guess)
(let
((fx (f guess)))
(let
((new (- guess (/ fx ((deriv f .001) guess)))))
(if (< (abs fx) .001)
guess
(Newton f new)))))
(define (cubic a b c)
(lambda (x)
(+ (* x x x) (* a (* x x)) (* b x) c)))
ex1-35 要求利用high-order process的形式进行抽象
将newton 以及之前的fixed-point进行 抽象化
(iterative test improve x) 以下给出代码
(define (iterative test improve x)
(if (test x) x (iterative test improve (improve x))))
(define (newton f x)
(iterative
(lambda (x) (< (abs (f x)) .001))
(lambda (x) (- x (/ (f x) (/ (- (f (+ x .001)) (f x)) .001))))
x))
(define (fixed-point f x)
(iterative
(lambda (x) (< (abs (- (f x) x)) .001))
f
x))
顺便热烈庆祝一下完成了第一单元的任务(才发现1985版的和中文译本那个2nd习题都不一样,难怪我都找不到答案)