lisp 从列表中过滤掉不匹配谓词的结果 [英] lisp filter out results from list not matching predicate
问题描述
我正在尝试使用 emacs 方言学习 lisp,但我有一个问题.假设 list 有一些成员,其谓词评估为假.如何在没有这些成员的情况下创建新列表?类似于 { A in L: p(A) is true }
.在python中有过滤器功能,lisp中是否有等价的东西?如果没有,我该怎么做?
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?
谢谢
推荐答案
这些函数都在 CL 包中,你需要 (require 'cl)
来使用它们:
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.
还要查找delete-if-not
,它的作用相同,但修改了它的参数列表.
Also look up delete-if-not
, which does the same, but modifies its argument list.
这篇关于lisp 从列表中过滤掉不匹配谓词的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!