An infinite continued fraction is an expression of the form
The infinite continued fraction expansion with the Ni and the Di all equal to 1 produces 1/
, where
is the golden ratio.
, where
is the golden ratio.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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define constant1 (lambda (i) 1.0)) | |
| (define n constant1) | |
| (define d constant1) | |
| (define (cont-frac | |
| n | |
| d | |
| k) | |
| (if | |
| (= k 1) | |
| (/ | |
| (n k) | |
| (d k)) | |
| (/ | |
| (n k) | |
| (+ | |
| (d k) | |
| (cont-frac | |
| n | |
| d | |
| (- k 1)))))) | |
| (define (cont-frac-iter-helper | |
| result | |
| n | |
| d | |
| k) | |
| (if | |
| (= k 1) | |
| result | |
| (cont-frac-iter-helper | |
| (/ | |
| (n (- k 1)) | |
| (+ | |
| (d (- k 1)) | |
| result)) | |
| n | |
| d | |
| (- k 1)))) | |
| (define (cont-frac-iter n d k) | |
| (cont-frac-iter-helper | |
| (/ (n k) (d k)) | |
| n | |
| d | |
| k)) | |
| (cont-frac n d 12) | |
| (cont-frac-iter n d 12) | |
