3.74 Exercise 3.74
To detect zero crossings, we can use the generalized form of
stream-map to map the sign-change-detector
procedure over each stream element as well as the previous
stream element. It is unclear whether a stream starting out
with a negative value starts out with a zero crossing (since
0 is considered positive), but I make the assumption
that this is the case. (If one were to disagree, then the
first stream element could be duplicated, to force the first
value in the new stream to be 0).
First, a definition of sign-change-detector, just
for fun:
(define (sign-change-detector previous-value current-value) | (cond ((and (< previous-value 0) (>= current-value 0)) 1) | ((and (>= previous-value 0) (< current-value 0)) -1) | (else 0))) |
|
Now, the definition of zero-crossings:
(define zero-crossings | (stream-map sign-change-detector | sense-data | (cons-stream 0 sense-data))) |
|