码迷,mamicode.com
首页 > 其他好文 > 详细

SICP 1.34-1.39习题体会

时间:2014-11-05 23:11:53      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:sicp

1.34 代入法, 报错, 2不能作为操作符

1.35 证明比较简单,因为它为方程的根。代码也就是将书本代码重写一遍。

(define (fix-point f first-guess)
    (define tolerance 0.00001)
    (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))


1.36 在上面的try下面加打印语句就行了。

测试了下迭代速度, 不用平均大概30多次,用平均10次左右。

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


(define (xx1000 guess)
    (define (funX x)
(/ (log 1000) (log x)))
  (fix-point funX guess))
(define (xx1000-avg guess)
    (define (funX x)
(/ (log 1000) (log x)))
    (define (avg-funX x)
(/ (+ x (funX x)) 2))
  (fix-point avg-funX guess))


1.37  1.38 1.39是一个系列的题目, 比较简单。

1.37

(define (cont-frac n d k)
    (define (cont-frac-r index)
(if (= index k)
   0
   (/ (n index)
      (+ (d index)
 (cont-frac-r (+ index 1))))))
  (cont-frac-r 1))


(define (cont-frac-new n d k)
    (define (cont-frac-i index result)
(if (= index 0)
   result
   (cont-frac-i (- index 1)
(/ (n k)
   (+ (d k) result)))))
  (cont-frac-i k 0.0))


1.38

(define (e-D index)
    (cond ((= 1 (remainder index 3))
  1)
 ((= 0 (remainder index 3))
  1)
 (else (* 2 (/ (+ index 1) 3)))))
(define (calc-e k)
    (+ 2 (cont-frac (lambda (i) 1.0) e-D k)))


1.39

(define (tan-cf x k)
    (define (n index)
(if (= index 1)
   x
   (- (square x))))
  (define (d index)
      (- (* 2 index) 1))
  (cont-frac n d k))

SICP 1.34-1.39习题体会

标签:sicp

原文地址:http://blog.csdn.net/levinjoe/article/details/40834275

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!