Filtering Lists



-----maxima-bounces at math.utexas.edu wrote: -----


>Here's a naive attempt at implementing FILTER:
>
>    :lisp (defmfun $filter (p l) (cons '(mlist) (remove-if-not p (cdr
>l))))
>
>It even appears to work:
>
>    (%i66) filter(evenp, [1, 2, 3]);
>    (%o66) [2]
>
>Am I missing anything?

Your filter function works OK (I think) for a predicate that is a CL
function,
but it fails when the predicate is a Maxima function. Example:

(%i13) f(x) := is(mod(x,3) = 2)$

(%i14) filter(f,[1,2,3,4,5]);
Maxima encountered a Lisp error:

Also, the function 'sublist' might do what you want

(%i16) sublist([1,2,3,4,5],'f);
(%o16) [2,5]

BW