<< Previous exercise (1.38) | sicp-solutions | Next exercise (1.40) >>
(define (tan-cf x k)
(cont-frac (lambda (i)
(if (= i 1) x (- (* x x))))
(lambda (i)
(- (* i 2) 1))
k))
Another version calculating the square only once using let.
(define (tan-cf x k)
(let ((a (- (* x x))))
(cont-frac (lambda (i) (if (= i 1) x a))
(lambda (i) (- (* i 2) 1))
k)))
Dividing once by -x outside the call to cont-frac obviates the need for the if statement and allows a static return value of -(x^2) for the n argument to cont-frac.
(define(tan-cf x k)
(let ((X (- (* x x))))
(- (/ (cont-frac (lambda (i) X)
(lambda (i) (- (* i 2) 1))
k)
x))
))
Here's yet another version, not defined in terms of cont-frac.
(define (tan-cf x k)
(define (tan-cf-rec i)
(let ((di (+ i (- i 1)))
(x^2 (* x x)))
(if (= i k)
di
(- di (/ x^2 (tan-cf-rec (+ i 1)))))))
(/ x (tan-cf-rec 1)))