Lecture overview -- Keyboard shortcut: 'u'  Previous page: Filtering -- Keyboard shortcut: 'p'  Next page: Examples of filtering -- Keyboard shortcut: 'n'  Lecture notes - all slides together  Annotated slide -- Keyboard shortcut: 't'  Alphabetic index  Help page about these notes  Course home    Recursion and Higher-order Functions - slide 24 : 35

The filtering function
For practical purposes it is important to have a memory efficient filter function
(define (filter pred lst)
  (reverse (filter-help pred lst '())))

(define (filter-help pred lst res)
  (cond ((null? lst) res)
        ((pred (car lst)) 
           (filter-help pred (cdr lst)  (cons (car lst) res)))
        (else 
           (filter-help pred (cdr lst)  res))))
Go to exercise
A straightforward filter function