Page 1 of 1

partial unique?

Posted: Tue Aug 04, 2009 12:44 pm
by tom
How can I remove all duplicates from a list except one?

(111222333) becomes (12333) or whatever...

Posted: Tue Aug 04, 2009 1:10 pm
by HPW
Is this a list with sepearate one-digit integers?

Code: Select all

(unique '(2 3 4 4 6 7 8 7))  → (2 3 4 6 7 8)

Posted: Tue Aug 04, 2009 5:49 pm
by tom
its strings, and I want to keep specified duplicates.

("name" "name" "name" "nobody" "nobody" "nobody")

to

("name" "nobody" "nobody" "nobody")

Posted: Wed Aug 05, 2009 5:23 pm
by cormullion
Hi Tom. That's a rather quirky task, so I think you might have to write a bit more code. The exact answer might depend on whether you want to keep elements in the same order, etc.

Here's a simple list traversal:

Code: Select all

(set 'l '("name" "name" "name" "nobody" "nobody" "nobody"))

(set 'keeplist '("nobody"))

(dolist (e l)
   (if (find e keeplist) 
       (push e results -1))
   (unless (find  e results)
       (push e results -1)))

;-> ("name" "nobody" "nobody" "nobody")
A non-iterative solution is easy too:

Code: Select all

(define (keep? e) (find e '("nobody"))) ; 
(append (unique (clean keep? l)) (filter keep? l)) 
which returns the same thing.