<< 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))
The above is incorrect since cont-frac adds the previous step's result to our value, and we need to subtract them instead. A correct procedure looks very similar to cont-frac, though, e.g.:
(define (tan-cf x k) (define (iter i result) (if (= i 0) result (iter (-1+ i) (/ (if (= i 1) x (square x)) (- (- (* 2 i) 1) result))))) (iter k 0))
The first example of "tan-cf" is in fact correct because of the minus-sign before the squared x in the first lambda-procedure.