Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing lists in Lisp

I could figure out some way to do this myself but I have a feeling there's a simpler, perhaps built-in way to do this. I want to see if any two lists share an element. These are the two lists I'm dealing with at the moment:

((0 1 2) (3 4 5) (6 7 8) (0 3 6) (1 3 7) (2 4 8) (0 4 8) (2 4 6))

((0 1 7) (0 1 6) (0 1 3) (0 3 7) (0 3 6) (0 6 7) (1 3 7) (1 3 6) (1 6 7) (3 6 7)) 

Since both lists contain (1 3 7), I'd like a comparison of the lists to return T.

Is there a better way to do this than just setting up a couple DOLISTs?

like image 788
Jason Swett Avatar asked Nov 25 '10 03:11

Jason Swett


1 Answers

How about INTERSECTION?

(defvar a '((0 1 2) (3 4 5) (6 7 8) (0 3 6) (1 3 7) (2 4 8) (0 4 8) (2 4 6)))
=> A
(defvar b '((0 1 7) (0 1 6) (0 1 3) (0 3 7) (0 3 6) (0 6 7) (1 3 7) (1 3 6) (1 6 7) (3 6 7)))
=> B
(intersection a b :test 'equal)
=> ((1 3 7) (0 3 6))
like image 103
Ken Avatar answered Sep 28 '22 06:09

Ken