CS212 Exams
Spring 1999 -
Final

Solution to Filtering a Stream

WARNING: You should seriously try to answer this question before looking at the solution -- it's just a good study skill. Also, some answers on this page may be more brief than you would want to put down in an actual exam. If you want any type of partial credit, you should write more than just the solution.


The given definition of filters enters into an infinite loop, because the expression (filters p (tails s)) in the let statement will not return a value if the stream is infinite. The correct definition for filters is:

(define (filters p s)
  (cond ((null-stream? s) s)
        ((p (heads s))
         (cons-stream (heads s) (filters p (tails s))))
        (else (filters p (tails s))))

Question

Return to CS 212 Final - Spring 1999