lisp filter out results from list not matching predicate

Anycorn picture Anycorn · Feb 10, 2010 · Viewed 19.4k times · Source

I am trying to learn lisp, using emacs dialect and I have a question. let us say list has some members, for which predicate evaluates to false. how do I create a new list without those members? something like { A in L: p(A) is true }. in python there is filter function, is there something equivalent in lisp? if not, how do I do it?

Thanks

Answer

rootzlevel picture rootzlevel · Feb 10, 2010

These functions are in the CL package, you will need to (require 'cl) to use them:

(remove-if-not #'evenp '(1 2 3 4 5))

This will return a new list with all even numbers from the argument.

Also look up delete-if-not, which does the same, but modifies its argument list.