Skip to content

Instantly share code, notes, and snippets.

@polgfred
Created November 30, 2010 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save polgfred/721145 to your computer and use it in GitHub Desktop.
Save polgfred/721145 to your computer and use it in GitHub Desktop.
SICP Exercise 1.37
;; 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