COM1340 - Winter 2003 New lists from old January 22, 2003 |
All the code here is functional. That is, each procedure
returns a new list, without mutating the input list.
Removing elements that satisfy a predicate
; rem-by-pred ; takes a list, returns a list with elements that satisfy a predicate ; removed (define (rem-by-pred lst) (cond [(null? lst) null] [(pred? (car lst)) (rem-by-pred (cdr lst))] [else (cons (car lst) (rem-by-pred (cdr lst)))]))
Example (where pred? is number?):
(rem-by-pred '(cat dog 42 hamster 57 raven)) => (cat dog hamster raven)