Elisp List Contains a Value

半城伤御伤魂 提交于 2019-12-03 07:27:02

问题


How do you check, in elisp, if a list contains a value? so the following would return t:

(contains 3 '(1 2 3))

but

(contains 5 '(1 2 3))

would return nil.


回答1:


The function you need is member

For example:

(member 3 '(1 2 3))

It will return the tail of list whose car is element. While this is not strictly t, any non-nil value is equivalent to true for a boolean operation. Also, member uses equal to test for equality, use memq for stricter equality (using eq).




回答2:


freiksenet's answer is good and idiomatic. If you are using dash.el, you could also call function -contains?, which does exactly the same—checks if some list contains an element:

(-contains? '(1 2 3) 2) ; t


来源:https://stackoverflow.com/questions/1408974/elisp-list-contains-a-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!