SICP习题解答1.40-1.46

ex1.40

#lang racket

; exercise 1.40
(define (newtons-method g guess)
  (fixed-point (newton-transform g) guess))
(define (newton-transform g)
  (lambda (x) (- x (/ (g x) ((deriv g) x)))))
(define (deriv g)
  (lambda (x) (/ (- (g (+ x dx)) (g x)) dx)))
(define dx 0.00001)
(define tolerance 0.00001)
(define (fixed-point f first-guess)
  (define (close-enough? v1 v2)
    (< (abs (- v1 v2)) tolerance))
  (define (try guess)
    (let ((next (f guess)))
      (if (close-enough? guess next)
          next
          (try next))))
  (try first-guess))
(define (cubic a b c)
  (lambda (x) (+ (* x x x) (* a x x) (* b x) c)))

(newtons-method (cubic 1 1 1) 1)  ;; -0.9999999999997796


ex1.41

; exercise 1.41
(define (double f)
  (lambda (x) (f (f x))))
(define (inc x) (+ x 1))
((double inc) 1) ;; 3(=1+2)
(((double (double double)) inc) 5) ;; 21

我们把double简记为D,进行代入扩展:

        (D (D D)) inc

==> (D (D (D))) inc

==> (D(D(D(D)))) inc

==> (D(D(D(D inc))))                                                 ; 即对inc做4次double操作

==> (D(D(D(inc(inc)))))                                             ; inc*2

==> (D(D(inc(inc(inc(inc))))))                                   ; inc*2*2

==> (D(inc(inc(inc(inc(inc(inc(inc(inc)))))))))         ; inc*2*2*2

==> (inc(inc....))                                                           ; inc*2*2*2*2=16次inc  

所以(((double (double double)) inc) 5) = 5+16 = 21

ex1.42-1.44

; exercise 1.42
(define (square x) (* x x))
(define (inc x) (+ x 1))
(define (compose f g)
  (lambda (x) (f (g x))))
((compose square inc) 6) ;; 49

; exercise 1.43
(define (repeated f n)
  (if (< n 1)
      (lambda (x) x)
      (compose f (repeated f (- n 1)))))
((repeated square 2) 5)

; exercise 1.44
(define (smooth f)
  (lambda (x)
    (/ (+ (f (- x dx)) (f x) (f (+ x dx))) 3)))
(define dx 0.00001)
(define (repeated-smooth f n)
  ((repeated smooth n) f))

ex1.45

#lang racket

; exercise 1.45
(define (n-root x n)
  (fixed-point
   ((repeated average-damp (repeat-times n))
    (lambda (y) (/ x (pow y (- n 1)))))
   1.0))

(define (repeat-times n)   ;; 做log2(n)次平均阻尼
   (floor (/ (log n) (log 2))))

(define tolerance 0.00001)
(define (fixed-point f first-guess)
  (define (close-enough? v1 v2)
    (< (abs (- v1 v2)) tolerance))
  (define (try guess)
    (let ((next (f guess)))
      (if (close-enough? guess next)
          next
          (try next))))
  (try first-guess))

(define (repeated f n)
  (if (< n 1)
      (lambda (x) x)
      (compose f (repeated f (- n 1)))))
(define (compose f g)
  (lambda (x) (f (g x))))

(define (average-damp f)
  (lambda (x) (average x (f x))))
(define (average x y) (/ (+ x y) 2))

(define (pow b p) 
   (define (even? x) 
     (= (remainder x 2) 0))    
   (define (square x) 
     (* x x)) 
   (define (iter res a n) 
     (if (= n 0) 
         res 
         (if (even? n) 
             (iter res (square a) (/ n 2)) 
             (iter (* res a) a (- n 1))))) 
   (iter 1 b p))

(n-root 128 7)  ;; 2.0000035538623377

ex1.46

#lang racket

; exercise 1.46
(define (iterative-improve good-enough? improve)
  (lambda (last-guess)
    (let ((cur-guess (improve last-guess)))
      (if (good-enough? last-guess cur-guess)
          cur-guess
          ((iterative-improve good-enough? improve) cur-guess)))))

(define (good-enough? v1 v2)
  (define tolerance 0.00000001) 
  (< (/ (abs (- v1 v2)) v2)  tolerance))

(define (sqrt x)
  (define (improve guess)
    (/ (+ (/ x guess) guess) 2))
  ((iterative-improve good-enough? improve) 1.0))

(define (fixed-point f first-guess)
  ((iterative-improve good-enough? f) first-guess))


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值