Smooth procedure

Eva Lu Ator has a criticism of Louis's approach in exercise 3.75 . The program he wrote is not modular, because it intermixes the operation of smoothing with the zero-crossing extraction. For example, the extractor should not have to be changed if Alyssa finds a better way to condition her input signal. Help Louis by writing a procedure smooth that takes a stream as input and produces a stream in which each element is the average of two successive input stream elements. Then use smooth as a component to implement the zero-crossing detector in a more modular style.


There are no comments yet.

Authentication required

You must log in to post a comment.

Login
(define (stream-car stream) (car stream))

(define (stream-cdr stream) (force (cdr stream)))

(define (stream-ref s n)
  (if (= n 0)
      (stream-car s)
      (stream-ref (stream-cdr s) (- n 1))))

(define sense-data (cons-stream 1
   (cons-stream 2 (cons-stream 1.5  (cons-stream 1  (cons-stream 0.5
   (cons-stream -0.1  (cons-stream -2  (cons-stream -3  (cons-stream -2
   (cons-stream -0.5  (cons-stream 0.2  (cons-stream 3 4)))))))))))))


(define s (smooth sense-data))

(check-equal? (stream-ref s 0) 3/2)
(check-equal? (stream-ref s 1) 1.75)
(check-equal? (stream-ref s 2) 1.25)
(check-equal? (stream-ref s 3) 0.75)