previous | index | next

The filter Procedure

Q: What's the result of filtering an empty list?

A: The empty list.

Q: If the head of a nonempty list satisfies the predicate, what should filter return?
(Hint: Answer in terms of the head and the tail.)

A: The result of consing the head onto the result of filtering the tail.

Q: If the head of a nonempty list does not satisfy the predicate, what should filter return?

A: The result of filtering the tail.

     (define filter
       (lambda (ok? lst)
         (cond ((null? lst) '())
               ((ok? (car lst)) 
                (cons (car lst)
                      (filter ok? (cdr lst))))
               (else (filter ok? (cdr lst))))))
This procedure will be useful for the chapter 7 application (movie database query system).

previous | index | next