sicp-ex-3.64



<< Previous exercise (3.63) | Index | Next exercise (3.65) >>


meteorgan

  
  
 (define (stream-limit stream tolerance) 
   (if (< (abs (- (stream-ref stream 1) (stream-ref stream 0))) 
          tolerance) 
       (stream-ref stream 1) 
       (stream-limit (stream-cdr stream) tolerance))) 

luckykoala

  
  
 (define (stream-limit stream tolerance) 
   (define (good-enough? a b) 
     (< (abs (- a b)) tolerance)) 
   (define (loop s) 
     (let ((a (stream-ref s 0)) 
           (b (stream-ref s 1))) 
       (if (good-enough? a b) 
           b 
           (loop (stream-cdr s))))) 
   (loop stream)) 

Sphinxsky

  
  
 (define (sub-streams stream1 stream2) 
     (stream-map - stream1 stream2)) 
  
 (define (one-order-difference stream) 
     (sub-streams (stream-cdr stream) stream)) 
  
 (define (stream-limit stream tolerance) 
     (define (iter tolerance-stream stream) 
         (if (<= (abs (stream-car tolerance-stream)) tolerance) 
             (stream-car stream) 
             (iter (stream-cdr tolerance-stream) (stream-cdr stream)))) 
     (iter (one-order-difference stream) (stream-cdr stream)))