Created
November 30, 2010 04:18
-
-
Save polgfred/721145 to your computer and use it in GitHub Desktop.
SICP Exercise 1.37
This file contains 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
;; Suppose that `n` and `d` are procedures of one argument (the term index `i`) | |
;; that return the N(i) and D(i) 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: | |
;; | |
;; N(1) | |
;; f = -------------------- | |
;; N(2) | |
;; D(1) + ------------ | |
;; . N(K) | |
;; . + ------ | |
;; . D(K) | |
(define (cont-frac n d k) | |
(if (= k 1) | |
(/ (n k) (d k)) | |
(/ (n k) (+ (d k) (cont-frac n d (- k 1)))))) | |
;; If your cont-frac procedure generates a recursive process, write one that | |
;; generates an iterative process. | |
(define (cont-frac n d k) | |
(define (cf-iter a k) | |
(if (= k 0) | |
a | |
(cf-iter (/ (n k) (+ (d k) a)) (- k 1)))) | |
(cf-iter 0 k)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment