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