Sicp Exercise 1.39

Exercise 1.39.  A continued fraction representation of the tangent function was published in 1770 by the German mathematician J.H. Lambert:

where x is in radians. Define a procedure (tan-cf x k) that computes an approximation to the tangent function based on Lambert’s formula. K specifies the number of terms to compute, as in exercise 1.37.

Solution:

(define (cont-frac
n
d
k)
(if
(= k 1)
(/
(n k)
(d k))
(/
(n k)
(+
(d k)
(cont-frac
n
d
(- k 1))))))
(define (reverse-nd n k)
(lambda (x)
(n (- (+ k 1) x))))
(define (cont-frac-rec n d k)
(cont-frac
(reverse-nd n k)
(reverse-nd d k)
k))
(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))
(define (dfun i)
(- (* i 2) 1))
(define d dfun)
;; (cont-frac-rec n d 60)
;; (cont-frac-iter n d 60)
(define (tan-cf k x)
(define (n k)
(-
0
(* x x)))
(*
-1
(/
(cont-frac-iter n d k)
x)))
(tan-cf 60 (/ 3.1415926535 4))

 

Leave a Reply

Your email address will not be published. Required fields are marked *