newLISP Fan Club

Forum => newLISP in the real world => Topic started by: tom on August 04, 2009, 05:44:31 AM

Title: partial unique?
Post by: tom on August 04, 2009, 05:44:31 AM
How can I remove all duplicates from a list except one?



(111222333)  becomes (12333) or whatever...
Title:
Post by: HPW on August 04, 2009, 06:10:14 AM
Is this a list with sepearate one-digit integers?


(unique '(2 3 4 4 6 7 8 7))  → (2 3 4 6 7 8)
Title:
Post by: tom on August 04, 2009, 10:49:11 AM
its strings, and I want to keep specified duplicates.



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



to



("name" "nobody" "nobody" "nobody")
Title:
Post by: cormullion on August 05, 2009, 10:23:15 AM
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:


(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:



(define (keep? e) (find e '("nobody"))) ;
(append (unique (clean keep? l)) (filter keep? l))


which returns the same thing.