Page 1 of 1

Detele duplicates from list

Posted: Sun Apr 18, 2010 6:01 am
by misha1988
Hi , I am new to LISP and on my exam I had to write a function that I would pass a list as an argument and it would return a list without duplicates.


please help me :roll:

Re: Detele duplicates from list

Posted: Sun Apr 18, 2010 7:00 am
by nuntius
Here are three approaches to identifying duplicates.

- Sort the list (like the old unix `sort file | uniq` trick)
- Search the output list (there's a CL command to only push new items)
- Use a hashtable

Re: Detele duplicates from list

Posted: Sun Apr 18, 2010 7:27 am
by misha1988
we didn't learn hashtable.

we just learned elementary lisp an in this function we must use recursion only.

please help me and write a simple function.

Re: Detele duplicates from list

Posted: Sun Apr 18, 2010 9:28 am
by Jasper
misha1988 wrote:we didn't learn hashtable.
You've gotta learn using the documentation, 'i haven't learned it yet' is kindah a defeatist position, go learn it then :)

Admittedly, i don't understand all parts, and not everything is as well described. hash table at lispdoc. Essentially, make the hashtable with make-hash-table and then get/set elements with gethash, really straightforward, assuming you know how to make a 'global' variable (DEFVAR, DEFPARAMETER), and how to use SETF.

Re: Detele duplicates from list

Posted: Sun Apr 18, 2010 10:16 am
by nuntius
misha1988 wrote:please help me and write a simple function.
First write a recursive function that can print each element of the list. (output is useful for testing)
Then add an optional parameter called output; have it initialize to an empty list, and push each element onto it.
Then augment your function to only push when the element is new.
Then remove, comment out, or (when nil ...) the debug output.

If you get stuck, ask detailed questions or post what you have in blocks so we can comment.

Re: Detele duplicates from list

Posted: Wed Apr 21, 2010 5:51 am
by misha1988
nuntius wrote:
misha1988 wrote:please help me and write a simple function.
First write a recursive function that can print each element of the list. (output is useful for testing)
Then add an optional parameter called output; have it initialize to an empty list, and push each element onto it.
Then augment your function to only push when the element is new.
Then remove, comment out, or (when nil ...) the debug output.

If you get stuck, ask detailed questions or post what you have in blocks so we can comment.


Sorry but I am new to LISP. I do know much of it . I just need to have the code of the function to show it to a lector :lol:
can you help me ?

Re: Detele duplicates from list

Posted: Wed Apr 21, 2010 8:13 am
by Jasper
(send-to-lector #'remove-duplicates)

Anyway, send us your attempts..

Re: Detele duplicates from list

Posted: Wed Apr 21, 2010 11:57 pm
by Paul
Jasper wrote:(send-to-lector #'remove-duplicates)

Anyway, send us your attempts..
Or, if you want to be a smart-arse, (send-to-lector (constantly nil))...the specification only asked for the returned list have no duplicates, not that it have the same elements as the input otherwise, so there's no point wasting time looking at the argument: just return NIL -- that's a list with no duplicates! ;)

Re: Detele duplicates from list

Posted: Thu Apr 22, 2010 4:59 am
by misha1988
Jasper wrote:(send-to-lector #'remove-duplicates)

Anyway, send us your attempts..



These functions are already writen :D I can't just send this link to lector :ugeek: :lol:


I should give him scource of a function that uses recursion :S


can anyone write it :?:


help me, the deadline is in 3 days :oops:

Re: Detele duplicates from list

Posted: Thu Apr 22, 2010 6:40 am
by Jasper
Try it.. post the attempt. Psuedocode will do.

Here is a hint: UNLESS the list is empty(NULL), put together(as in CONS) the FIRST of with: the REST of the list after the duplicates removed(this is the same function you're making at this point.) and the elements being the same to the FIRST of the list REMOVEd.

Where uppercase words are all the functions/macros used except the function in use. Anyway if that confuses you, put it out of your mind and write something you came up with. If you can't do it recursively.. then don't do it that way yet.