Search code examples
common-lisp

Compare/Intersection List of Lists


I wanted to use intersection to see the intersection of two lists.

While

(intersection (list 1 1) (list 1 1))

works perfectly fine and returns (1 1),

 (intersection (list (list 1 2) (list 1 4)) (list (list 1 2) (list 1 5)))

doesnt return me (1 2) but NIL.

Where is my mistake?


Solution

  • intersection takes an optional :test keyword argument to specify what definition of "equality" to use. The default is eql, which compares lists for pointer equality. Since your two lists are actually distinct lists in memory, they're not eql. Use equal to compare lists element-wise.

      (intersection (list (list 1 2) (list 1 4)) (list (list 1 2) (list 1 5)) :test #'equal)