SICP Exercise 1.37
Question
1. An infinite continued fraction is an expression of the form
As an example, one can show that the infinite continued fraction expansion with the
Suppose that n
and d
are procedures of one argument (the term index cont-frac
such that evaluating (cont-frac n d k)
computes the value of the
(cont-frac (λ (i) 1.0)
(λ (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?
2. 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.
Answer
(define (cont-frac n d k)
(define (iterate counter)
(if (= k counter) (/ (n k) (d k))
(/ (n counter) (+ (d counter) (iterate (+ counter 1))))))
(iterate 1))
(cont-frac (λ (i) 1.0)
(λ (i) 1.0)
12)
Results:
0.6180257510729613
Next, an iterative version of the same function. This gives the exact same results as the function defined above.
(define (cont-frac-iter n d k)
(define (iterate counter accumulator)
(cond ((= counter 0) accumulator)
((= counter k) (iterate (- counter 1) (/ (n counter) (d counter))))
(else (iterate (- counter 1) (/ (n counter) (+ (d counter) accumulator))))))
(iterate k 1))
Actually this is a little awkward since on our first call of iterate
, we need to pass some value for accumulator
, even though it will be overwritten on the very first iteration anyways.
I used 1
here, but we could just as well have written (iterate k 459384)
.