sicp-ex-3.74



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


meteorgan

  
  
 (define zero-crossings 
   (stream-map sign-change-detector 
               sense-data 
               (cons-stream 0 sense-data))) 

karthikk

I don't think that is right. If the first element of the sense-data stream were negative above solution will show a sign-change crossing immediately when there is none.

 (define (zero-crossings sense-data) 
   (stream-map sign-change-detector 
               sense-data 
               (stream-cdr sense-data))) 

meteorgan's solution complies with the original zero-crossings in the book. The first two arguments to sign-change-detector should be: (stream-car sense-data) and 0


Per the book:

"...the resulting signal should be +1 whenever the signal changes from negative to positive, -1 whenever the input signal changes from positive to negative, and 0 otherwise.
*(Assume that the sign of a 0 input is positive.)*"

The negative sign change would be the expected result.



xdavidliu

I agree with hoonji that meteorgan's solution correctly answers the problem. However I would like to point out that the last-value parameter is completely superfluous when the next value in the stream is always easily accessible. In this manner, we can rewrite make-zero-crossings elegantly to require only a single argument:

 (define (make-zero-crossings input-stream) 
   (stream-map sign-change-detector 
               (stream-cdr sense-data) 
               sense-data)) 

karthikk's solution addresses this issue too, but as pointed out already, that is not what the book is asking for literally in this exercise. In my opinion this seems like a rare example of lapse of judgment on the part of the authors.


Sphinxsky

  
  
 (define zero-crossings 
     (stream-map 
         sign-change-detector 
         sense-data 
         (cons-stream 
             (stream-car sense-data) 
             sense-data))) 
  

Cirno

  
  
 ;; helpers 
 (define (sign-change-detector old new) 
   (cond ((or (positive? old) (zero? old)) 
          (if (negative? new) -1 0)) 
         ((negative? old) 
          (if (negative? new) 0 1)))) 
  
 (define (random-in-range low high) 
   (let ((range (- high low))) 
     (+ low (* (random) range)))) 
  
 (define (random-stream low high) 
   (cons-stream (random-in-range low high) 
                (random-stream low high))) 
  
 (define sense-data (random-stream -10 10)) 
  
 (define (make-zero-crossings input-stream last-value) 
   (cons-stream 
    (sign-change-detector 
     (stream-car input-stream) 
     last-value) 
    (make-zero-crossings 
     (stream-cdr input-stream) 
     (stream-car input-stream)))) 
  
 (define zero-crossings-alysa 
   (make-zero-crossings sense-data 0)) 
  
  
 (define zero-crossings-eva 
   (stream-map sign-change-detector 
               sense-data 
               (stream-cdr sense-data))) 
  
 (display "eva:") 
 (newline) 
 (stream-ref zero-crossings-eva 0) 
 (stream-ref zero-crossings-eva 1) 
 (stream-ref zero-crossings-eva 2) 
 (stream-ref zero-crossings-eva 3) 
 (stream-ref zero-crossings-eva 4) 
 (stream-ref zero-crossings-eva 5) 
 (stream-ref zero-crossings-eva 6) 
 (stream-ref zero-crossings-eva 7) 
 (stream-ref zero-crossings-eva 8) 
 (stream-ref zero-crossings-eva 9) 
 (stream-ref zero-crossings-eva 10) 
  
 (display "alyssa:") 
 (newline) 
 (stream-ref zero-crossings-eva 0) 
 (stream-ref zero-crossings-eva 1) 
 (stream-ref zero-crossings-eva 2) 
 (stream-ref zero-crossings-eva 3) 
 (stream-ref zero-crossings-eva 4) 
 (stream-ref zero-crossings-eva 5) 
 (stream-ref zero-crossings-eva 6) 
 (stream-ref zero-crossings-eva 7) 
 (stream-ref zero-crossings-eva 8) 
 (stream-ref zero-crossings-eva 9) 
 (stream-ref zero-crossings-eva 10)