`
zhangle
  • 浏览: 26017 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

sicp 习题 1.38 ~ 1.41

    博客分类:
  • SICP
阅读更多
1.38
(define (cont-frac n d i k)
  (if (= k 1)
      (/ (n 1) (d 1))
      (/ (n i) (+ (d i) (cont-frac n d (+ i 1) (- k 1))))))

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

(define (cont-frac-i n d k)
  (define (frac i v)
    (if (= i 0)
        v
        (frac (- i 1) (/ (n i) (+ (d i) v)))))
  (frac k 0))

(cont-frac-r (lambda (i) 1.0)
           (lambda (i) 1.0)
           11)

(define (frac n)
  (if (= (remainder (- n 2) 3) 0)
      (* 2 (+ (/ (- n 2) 3) 1))
      1))


(cont-frac-r (lambda (i) 1.0)
           (lambda (i) 
             (if (= (remainder (- i 2) 3) 0)
                 (* 2 (+ (/ (- i 2) 3) 1))
                 1))
           1000)


1.39
(require (lib "math.ss"))

(define (tan-cf x k)
  (define (d i)
    (- (* 2 i) 1))
  (define (n i)
    (if (= i 1)
        x
        (- (* x x))))
  (define (frac i)
    (if (= i (+ k 1))
        0
        (/ (n i) (+ (d i) (frac (+ i 1))))))
  (frac 1))

(tan-cf (/ pi 4) 10)

(tan-cf (- (/ pi 4)) 10)

(tan-cf (/ pi 8) 10)


1.40
(define (square x)
  (* x x))

(define (cube x)
  (* x x x))

(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)))
      ;;(newline)
      ;;(display next)
      (if (close-enough? guess next)
          next
          (try next))
      ))
    (try first-guess))

(define (deriv g)
  (lambda (x)
    (/ (- (g (+ x dx)) (g x))
       dx)))

(define dx 0.00001)

(define (newton-transform g)
  (lambda (x)
    (- x (/ (g x) ((deriv g) x)))))

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

(define (cubic-deriv a b c)
  (lambda (x)
    (+ (* 3 (square x)) (* 2 a x) b)))

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

(newton-method (cubic 1 2 3) 1)
;;((cubic 1 1 1) 1)


1.41
(define (double proc)
  (lambda (x)
    (proc (proc x))))

(define (inc x)
  (+ x 1))

((double inc) 1)

(((double double) inc) 1)

(((double (double double)) inc) 1)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics