my attempt to do the exercises in sicp.

Monday, June 30, 2008

sicp exercise 1.37

;  Exercise 1.37. a. An infinite continued fraction is an expression of the form
;  As an example, one can show that the infinite continued fraction expansion with the Ni and the Di
;  all equal to 1 produces 1/phi , where phi is the golden ratio (described in section 1.2.2). One way
;  to approximate an infinite continued fraction is to truncate the expansion after a given number of
;  terms. Such a truncation -- a so-called k-term finite continued fraction -- has the form
;  Suppose that n and d are procedures of one argument (the term index i) that return the Ni and Di of the
;  terms of the continued fraction. Define a procedure cont-frac such that evaluating (cont-frac n d k)
;  computes the value of the k-term finite continued fraction. Check your procedure by approximating
;  1/phi using
;   (cont-frac (lambda (i) 1.0)
;              (lambda (i) 1.0)
;              k)
;  for successive values of k. How large must you make k in order to get an approximation that is accurate
;  to 4 decimal places?
;  b. If your cont-frac procedure generates a recursive process, write one that generates an iterative
;  process. If it generates an iterative process, write one that generates a recursive process.

(define (cont-frac-recur n d k)
    (define (iter i)
       (if (> i k)
           (/ (n i) (+ (d i) (iter (+ i 1))))))
    (iter 1))

(display (cont-frac-recur (lambda(i) 1.0) (lambda(i) 1.0) 100))(newline)

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

(display (cont-frac-iter (lambda(i) 1.0) (lambda(i) 1.0) 1000))(newline)

1 comment:

Rahul said...

Rahul Here. Its better if we try to solve this problem using acculuation abstraction.