sicp-ex-1.39



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

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