read

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

Blog Logo

Ki Sung Bae


Published

Image

Gsong's Blog

Developer + Entrepreneur = Entreveloper

Back to Overview