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.
It is a list of cons cells called associations: the CAR of each cons cell is the key, and the CDR is the associated value. Here is an example of an alist. The key pine is associated with the value cones ; the key oak is associated with acorns ; and the key maple is associated with seeds .
Assuming this is about Common Lisp, there is a function last which returns a list containing the last item of a list. If you use this function with mapcan, which applies a given function to each element of a list and returns the concatenated results, you'll get what you want.
The symbol nil is an atom and is also a list; it is the only Lisp object that is both. This function returns t if object is a cons cell or nil .
This special form is just like set except that the first argument is quoted automatically, so you don't need to type the quote mark yourself. Also, as an added convenience, setq permits you to set several different variables to different values, all in one expression.
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
).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With