1

Finding list elements based on single atoms seems to work fine:

CL-USER> (find 1 (list 5 4 3 2 1))
1

But what about finding a list in a list?

CL-USER> (find (list 1 2) (list (list 3 4) (list 1 2)))
NIL

How to do it?

johnbakers
  • 24,158
  • 24
  • 130
  • 258
  • Try `(find (list 1 2) (list (list 3 4) (list 1 2)))`? – halfbit Nov 17 '13 at 10:10
  • possible duplicate of [Test if array is inside a list in lisp](http://stackoverflow.com/questions/19287777/test-if-array-is-inside-a-list-in-lisp); especially the explanation of the choice of the default `:test` predicate in the answer – sds Nov 18 '13 at 06:10

1 Answers1

4

FIND by default uses EQL for testing for an element. This test however for lists returns true only if the two objects are the same (i.e. if they are EQ) and not if they have equal elements.

Thus:

(find (list 1 2) (list (list 1 2) (list 1 2 3))) ==> NIL

(let ((L1 (list 1 2))
      (L2 (list 1 2 3)))
  (find L1 (list L1 L2))) ==> (1 2)

but you can also specify a different test function

(find (list 1 2) (list (list 1 2) (list 1 2 3))
      :test #'EQUAL) ==> (1 2)
6502
  • 112,025
  • 15
  • 165
  • 265