Exercise 3.64. Write a procedure stream-limit that takes as arguments a stream and a number (the tolerance). It should examine the stream until it finds two successive elements that differ in absolute value by less than the tolerance, and return the second of the two elements. Using this, we could compute square roots up to a given tolerance by
(define (sqrt x tolerance) (stream-limit (sqrt-stream x) tolerance))
I wrote this answer:
(define (stream-limit s tol) (if (stream-null? s) (error "End of stream found before limit reached:" tol) (let* ((a (stream-car s)) (b (stream-car (stream-cdr s))) (diff (abs (- a b)))) (if (> tol diff) b (stream-limit (stream-cdr s) tol)))))
Can it be improved?