| TWiki . Cookbook . ListRecipeFilterElements |
;; filter*: (Value -> Boolean) -> List ;; To filter al the elements in the list l that match with the predicate pred (define (filter* predicate l) (let loop ((l l) (acc '())) (cond [(null? l) acc] [(pred (car l)) (loop (cdr l) (append acc (list (car l))))] [else (loop (cdr l) acc)])))
filter function that ships either in the SRFI 1 or in Mzlib. The signature of this filter is the very same that the one for filter*.
;; either (require (lib "list.ss" "srfi" "1.ss" ));; for filter in the SRFI 1 ;; or (require (lib "list.ss" "mzlib")) ;; for the one in Mzlib ;; and You are ready to go
(define (myfilt pred? lst) (cond ((null? lst) '()) ((pred? (car lst)) (cons (car lst) (myfilt pred? (cdr lst)))) (else (myfilt pred? (cdr lst))))) (printf "(myfilt even? '(1 2 3 4 5)) -> ~a\n" (myfilt even? '(1 2 3 4 5)))
| CookbookForm | |
|---|---|
| TopicType: | Recipe |
| ParentTopic: | ListRecipes |
| TopicOrder: | |
----- Revision r1.3 - 26 Mar 2006 - 00:48 GMT - JensAxelSoegaard
|