检查项目是否在列表中(Lisp) [英] Check if item is in a list (Lisp)

查看:89
本文介绍了检查项目是否在列表中(Lisp)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查项目是否在列表中的简单方法是什么?

类似

(in item list)

如果item=1list=(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆