; defining stream-for-each
(define (stream-for-each proc s)
(if (stream-null? s)
'done
(begin (proc (stream-car s))
(stream-for-each (stream-cdr s)))))
; Defining a method to display the stream
(define (display-stream s)
(stream-for-each display-line s))
(define (display-line x)
(newline)
(display x))
; Creating a stream that uses Newton-Raphson
; method to find the Square Root of 2
(define (sqrt-improve guess x)
(avg guess (/ x guess)))
(define (sqrt-stream x)
(define guesses
(cons-stream 1.0
(stream-map (lambda (guess)
(sqrt-improve guess x))
guesses)))
guesses)
Now testing it..
=> (stream-ref (sqrt-stream 2) 11)
Value: 1.414213562373095
So it seems the stream is working correctly; however when I try and display it:
=> (display-stream (sqrt-stream 2))
1.
;The procedure #[compound-procedure 13 stream-for-each]
has been called with 1 argument; it requires exactly 2 arguments.
From what I see, stream-for-each is being called with 2 args, but I must be missing something. I would appreciate any clarification. The code is taken from: http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html#%_sec_3.5.3
You’re just missing an argument in the recursive call
(stream-for-each (stream-cdr s)). Your functionstream-for-eachhas the contractprocedure? stream? -> 'donebut you’ve only provided it a stream (which is what the error says).