开发者

Comparing lists in Lisp

开发者 https://www.devze.com 2023-01-27 08:47 出处:网络
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 de

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 开发者_如何学JAVAcomparison of the lists to return T.

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


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))
0

精彩评论

暂无评论...
验证码 换一张
取 消