SICP 1.34-1.39习题体会

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))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值