johnbakers
johnbakers

Reputation: 24760

Can find be used to find a list in another list?

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?

Upvotes: 1

Views: 105

Answers (1)

6502
6502

Reputation: 114491

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)

Upvotes: 4

Related Questions