Check if item is in a list (Lisp)

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

Leave a Comment

tech