2

How do i remove duplicated lists inside a list in common-lisp? I tried this:

(remove-duplicates '( (1 2 3) (1 2 3)))                           

But it evaluates to ((1 2 3) (1 2 3)), not ((1 2 3)).

Thanks.

ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
BufBills
  • 8,005
  • 12
  • 48
  • 90

2 Answers2

10

Use the keyword argument :test to specify the function that defines whether or not two items are duplicates of each other. Most lisp functions, including remove-duplicates, use eql to test for equality by default. eql is much stricter than equal, which is what you probably want to be using.

 (remove-duplicates '((1 2 3) (1 2 3)) :test #'equal)

This evaluates to '((1 2 3)).

See this post for more detail about the difference between eql and equal.

Community
  • 1
  • 1
ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
  • 2
    That's not the "second optional argument"; `test` is a keyword argument. The order of the keyword arguments doesn't matter. E.g., you could do `(remove-duplicates list :test 'equal :from-end t)` or `(remove-duplicates list :from-end t :test 'equal)` and the results would be the same. Optional arguments in Common Lisp are _position_ based, but keyword arguments are not. – Joshua Taylor Feb 05 '14 at 23:09
  • @JoshuaTaylor Good point. Editing. – ApproachingDarknessFish Feb 06 '14 at 00:04
2

Try:

(remove-duplicates '((1 2 3) (1 2 3)) :test #'equal)
uselpa
  • 18,732
  • 2
  • 34
  • 52