next up previous contents index
Next: Searching for an element Up: Higher Order Functions Previous: Converting the `map' function:   Contents   Index


Selecting elements from a given list: `filter'

This abstraction takes a predicate function and a list as its two arguments. The result returned by `filter' is a new list containing all elements of the original list, for which the predicate function returns the value `true'.


\begin{lstlisting}[language=Scm]{}
(define filter (lambda(p l)
(if (nullp l)
n...
... l))
(cons (car l) (filter p (cdr l)))
(filter p (cdr l))))))
\end{lstlisting}


\begin{lstlisting}[language=Scm]{}
(define l1 (cons one
(cons two
(cons thre...
... nil)))))
;
(ldisp! (filter (lambda(x) (gtp x two)) l1))
--> 3
\end{lstlisting}



domain access counter Georg P. Loczewski 2004-03-05