<< Previous exercise (3.75) | Index | Next exercise (3.77) >>


 (define (smooth s) 
         (stream-map (lambda (x1 x2) (/ (+ x1 x2) 2)) 
                             (cons-stream 0 s) 
 (define (make-zeor-crosssings input-stream smooth) 
         (let ((after-smooth (smooth input-stream))) 
             (stream-map sign-change-detector after-smooth (cons-stream 0 after-smooth)))) 


Again no need to cons-stream 0 in this case.

This had to be done in the non-modular (i.e. without using stream-map) 3.74 and 3.75 cases because the function had to be provided with an (arbitrary) initial value of 0 (and presumably the first couple of elements of output stream discarded)

With stream-map, there is no need for an arbitrary initial value for the sense-data: Thus:

 (define (smooth input-stream) 
   (stream-map (lambda(x y)(/ (+ x y) 2)) input-stream (stream-cdr input-stream))) 
 (define (zero-crossings input-stream) 
   (stream-map sign-change-detector input-stream (stream-cdr input-stream))) 
 (define (smoothed-zero-crossing sense-data) 
   (zero-crossings (smooth sense-data))) 


 (define (average a b) (/ (+ a b) 2)) 
 (define (smooth stream proc) (stream-map proc (stream-cdr stream) stream)) 
 (define (sign-change-detector now before) 
     (cond ((and (> before 0) (< now 0)) (- 1)) 
         ((and (< before 0) (> now 0)) 1) 
         (else 0))) 
 (define (make-zero-crossings input-stream) 
         (stream-cdr input-stream) 
 (define zero-crossings (make-zero-crossings (smooth sense-data average))) 

joe w

I disagree that we should leave off the initial zero, without it you lose the zero crossing of the first two proper elements of the stream.

 (define sense-data (cons-stream 5 (cons-stream -8 (cons-stream -7 (cons-stream -1 sense-data))))) 
 (define (smooth input-stream) 
   (define (avg x y) 
     (/ (+ x y) 2)) 
   (s-map avg input-stream (tail input-stream))) 
 (display-stream-until 6 (smooth sense-data)) 
 (define (make-zero-crossings-modular input-stream) 
   (let ((smoothed (cons-stream (head input-stream )(smooth input-stream)))) 
         (s-map sign-change-detector (tail smoothed) smoothed))) 
 (define zero-crossings-modular  
   (make-zero-crossings-modular sense-data)) 
 (display-stream-until 6 zero-crossings-modular) 

Using the sense data above we get: -1 0 0 1 -1 0 0

If you drop the cons-stream you don't get the zero crossing for 5 to -8; the result stream starts at the first 0 for the sensing data provided.

This assumes the stream starts in an off state, so we start with a zero, if it were an on stream that we just picked up at our first sensing data we could cons the head of the sensing data onto itself.