Subject: [clisp-list] Remove duplicates from a complex list

From: "Justice, Randy -CONT(DYN)" <[email-masked]>
Date: Sat Oct 27 07:28:02 2001

Hi.

I'm trying to remove duplicates from a list.  

I have defined the following function:

(defun noduplicates (list)
 (let ((set ()))
   (mapc #'(lambda (x)
         (setq set (adjoin x set :test #'equal)))
      list)
    set))

[17]> (setq b '( 1 2 3 4 5 1 2 3))
(1 2 3 4 5 1 2 3)
[18]> (noduplicates b)
(5 4 3 2 1)

[19]> (setq b '( (1) (2) (3) (4) (5) (1) (2) (3)))
((1) (2) (3) (4) (5) (1) (2) (3))
[20]> (noduplicates b)
((5) (4) (3) (2) (1))

[21]> (setq b '( (1 1) (2 1) (3 1) (4 1) (5 1) (1 1) (2 1) (3 1)))
((1 1) (2 1) (3 1) (4 1) (5 1) (1 1) (2 1) (3 1))
[22]> (noduplicates b)
((5 1) (4 1) (3 1) (2 1) (1 1))

As you can see it works for "simple" lists...
When it comes to a more complex lists it does not work...

(setq z  '((#3A(((3 4 0) (1 3 2) (1 3 2))
       ((1 0 4) (4 0 0) (4 3 1))
       ((2 1 0) (0 1 1) (0 3 3)))
  (0 1915))
 (#3A(((0 2 1) (3 3 4) (3 4 0))
      ((1 3 2) (3 3 1) (0 2 2))
      ((3 1 4) (0 2 2) (1 4 1)))
  (0 1917))
 (#3A(((2 4 0) (4 3 1) (4 1 0))
      ((2 2 1) (4 3 0) (0 3 3))
      ((1 2 0) (0 1 0) (0 2 2)))
  (0 2017))
 (#3A(((3 4 0) (1 3 2) (1 3 2))
      ((1 0 4) (4 0 0) (4 3 1))
      ((2 1 0) (0 1 1) (0 3 3)))
  (0 1915))
 (#3A(((3 4 0) (1 3 2) (1 3 2))
      ((1 0 4) (4 0 0) (4 3 1))
      ((2 1 0) (0 1 1) (0 3 3)))
  (0 1915)))
)

[74]> (noduplicates z)
((#3A(((3 4 0) (1 3 2) (1 3 2))
      ((1 0 4) (4 0 0) (4 3 1))		<== Duplicate	
      ((2 1 0) (0 1 1) (0 3 3)))
  (0 1915))
 (#3A(((3 4 0) (1 3 2) (1 3 2))
      ((1 0 4) (4 0 0) (4 3 1))		<== Duplicate
      ((2 1 0) (0 1 1) (0 3 3)))
  (0 1915))
 (#3A(((2 4 0) (4 3 1) (4 1 0))
      ((2 2 1) (4 3 0) (0 3 3))
      ((1 2 0) (0 1 0) (0 2 2)))
  (0 2017))
 (#3A(((0 2 1) (3 3 4) (3 4 0))
      ((1 3 2) (3 3 1) (0 2 2))
      ((3 1 4) (0 2 2) (1 4 1)))
  (0 1917))
 (#3A(((3 4 0) (1 3 2) (1 3 2))
      ((1 0 4) (4 0 0) (4 3 1))		<== Duplicate
      ((2 1 0) (0 1 1) (0 3 3)))
  (0 1915)))
[75]>

What's going on??  How do I remove the duplicates from the list?

Randy

---

From: Sam Steingold <[email-masked]>
Date: Sat Oct 27 08:37:02 2001

> * In message <[email-masked]>
> * On the subject of "[clisp-list] Remove duplicates from a complex list"
> * Sent on Sat, 27 Oct 2001 09:27:40 -0500
> * Honorable "Justice, Randy -CONT\(DYN\)" <[email-masked]> writes:
>

CLHS has DELETE-DUPLICATES and REMOVE-DUPLICATES

> (setq z  '((#3A(((3 4 0) (1 3 2) (1 3 2))

read about EQ, EQL, EQUAL and EQUALP in CLHS.

(equal #(1 2 3) #(1 2 3))
 ==> nil
(equalp #(1 2 3) #(1 2 3))
 ==> t

-- 
Sam Steingold (http://www.podval.org/~sds)
Keep Jerusalem united! <http://www.onejerusalem.org/Petition.asp>
Read, think and remember! <http://www.iris.org.il> <http://www.memri.org/>
Parachute for sale, once used, never opened, small stain.