Gsong's Blog

SICP 연습문제 풀이 1.37


a.

[CODE]
(define (fixed-point f first-guess)
  (define (close-enough? v1 v2)
    (display v1)
    (newline)
    (< (abs (- v1 v2)) tolerance))
  (define (try guess)
    (let ((next (f guess)))
      (if (close-enough? guess next)
          next
          (try next))))
  (try first-guess))

(define tolerance 0.00001)

(define (average x y) (/ (+ x y) 2))

(define (cont-frac n d k)
  (define (cont-frac-iter i)
    (if (= i k) 0
        (/ (n i) (+ (d i) (cont-frac-iter (+ i 1))))))
  (cont-frac-iter 1))
[/CODE]

> (/ 1 (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 10))
1.6176470588235294

> (/ 1 (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 12))
1.6179775280898876
> (/ 1 (cont-frac (lambda (x) 1.0) (lambda (x) 1.0) 13))
1.6180555555555558

b.
[CODE]
(define (cont-frac n d k)
  (define (cont-frac-iter i result)
    (if (= i 0) result
        (cont-frac-iter (- i 1) (/ (n i) (+ (d i) result)))))
  (cont-frac-iter k 0))

[/CODE]
> (/ 1 (cont-frac (lambda (i) 1.0) (lambda (i) 1.0) 13))
1.6180257510729614

크리에이티브 커먼즈 라이센스
Creative Commons License
2008/01/31 17:37 2008/01/31 17:37
top

TRACKBACK ADDRESS :: http://www.gsong.pe.kr/tc/trackback/308

  1. Tracked from 1.6180 2009/03/30 16:58 DELETE

    Subject: 연습문제 1.37

    (define (cont-frac n d k)  (define (cont-rep i)    (if (= i k) 0        (/ (n i) (+ (d i) (cont-rep (+ i 1))))))  (cont-rep 1)) (define (cont-frac2 n d k)  (define (cont-iter answer i)    (if (= i 1) answer        (cont-iter (/ (n i