检查项目是否在列表中(Lisp) [英] Check if item is in a list (Lisp)
问题描述
检查项目是否在列表中的简单方法是什么?
类似
(in item list)
如果item=1
,list=(5 9 1 2)
和false
如果item=7
可能返回true
通用Lisp
FIND
不是一个好主意:
> (find nil '(nil nil))
NIL
以上表示NIL
不在列表(NIL NIL)
中-这是错误的.
FIND
的目的不是检查成员资格,而是查找满足测试要求的元素(在上面的示例中,测试功能是通常的默认EQL
). FIND
返回这样的元素.
使用MEMBER
:
> (member nil '(nil nil))
(NIL NIL) ; everything non-NIL is true
或POSITION
:
> (numberp (position nil '()))
NIL
What's a simple way to check if an item is in a list?
Something like
(in item list)
might return true
if item=1
and list=(5 9 1 2)
and false
if item=7
Common Lisp
FIND
is not a good idea:
> (find nil '(nil nil))
NIL
Above would mean that NIL
is not in the list (NIL NIL)
- which is wrong.
The purpose of FIND
is not to check for membership, but to find an element, which satisfies a test (in the above example the test function is the usual default EQL
). FIND
returns such an element.
Use MEMBER
:
> (member nil '(nil nil))
(NIL NIL) ; everything non-NIL is true
or POSITION
:
> (numberp (position nil '()))
NIL
这篇关于检查项目是否在列表中(Lisp)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!