## SICP – cap. 1 – procedure come metodo generale – esercizio – 47

Continuo da qui a fare gli esercizi, oggi sono qui.

Exercise 1.37.  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/ϕ`, where 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/ϕ` 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?

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.

Uhmmm… non è difficile ma –al solito– c’è Bill the Lizard che spiega bene e produce un codice über (semplice una volta che è scritto):

OK, ed ecco la versione iterativa

OK, ovviamente –non si sa mai…

Tra i nerd che seguo è da vedere il post di sicp-ex 😀
Ah, sì, la seconda risposta non è 42 ma 10.

Annunci