sicp-ex-1.39


 (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)) 

<< Previous exercise (1.38) | Next exercise (1.40) >>