练习1.40-练习1.46

1.练习1.40

(define (cubic a b c)
  (define (cube x)
    (* x x x))
  (lambda (x)
    (+ (cube x)
       (* a (square x))
       (* b x)
       c)))

(define (newtons-method g guess)
  (fixed-point (newton-transform g) guess))

(newtons-method (cubic 1 0 0) 1)

2.练习1.41

(define (double f)
  (lambda (x) (f (f x))))

(((double (double double)) (lambda (x) (+ x 1))) 5)

结果是21,真的很不想解释。

(double double)展开是(f (f  (f (f x))))

(double (double double))展开是(f’ (f‘ (f’ (f‘ x))))))),每个f‘为(f (f  (f (f x)))),一共是16个f,所以结果是5+16=21

3.练习1.42

(define (compose f g)
  (lambda (x) (f (g x))))
4.练习1.43

(define (repeated f n)
  (if (= n 1)
      (lambda (x) (f x))
      (lambda (x) (f ((repeated f (- n 1)) x)))))
5.练习1.44

这个有点儿直接了,没有简化

(define (smooth f)
  (let ((dx 0.0000001))
    (lambda (x) (/
		 (+
		  (f (- x dx))
		  (f x)
		  (f (+ x dx)))
		 3))))

(((repeated smooth 6) square) 2)
6.练习1.45

也是没有简化

(define (average-dump-time n)
  (floor (/ (log n) (log 2))))

(define (cifanggen x n)
  (define (product term a next b)
    (if (> a b)
	1
	(* (term a)
	   (product term (next a) next b)))) 
  (define (cifang x n)
    (product (lambda (i) x)
	     1
	     (lambda (i) (+ i 1))
	     n))
  (fixed-point ((repeated average-dump (average-dump-time n)) (lambda (i) (/ x (cifang i (- n 1)))))
	       n))
7.练习1.46
(define (iterative-improve good-enough? improve)
   (lambda (a) (if (good-enough? a (improve a))
		   (improve a)
		   ((iterative-improve good-enough? improve) (improve a)))))

(define (fixed-point f guess)
  (define (good-enough1? a b)
    (let ((tolerance 0.000001))
      (< (abs (- a b)) tolerance)))
  ((iterative-improve good-enough1? f) guess))

(define (sqrt n)
  (fixed-point (lambda (y) (/ (+ y (/ n y)) 2))
	       1.0))

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页