Detele duplicates from list

Discussion of Common Lisp
Post Reply
misha1988
Posts: 4
Joined: Sun Apr 18, 2010 5:49 am

Detele duplicates from list

Post by misha1988 » Sun Apr 18, 2010 6:01 am

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:

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Detele duplicates from list

Post by nuntius » Sun Apr 18, 2010 7:00 am

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

misha1988
Posts: 4
Joined: Sun Apr 18, 2010 5:49 am

Re: Detele duplicates from list

Post by misha1988 » Sun Apr 18, 2010 7:27 am

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.

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: Detele duplicates from list

Post by Jasper » Sun Apr 18, 2010 9:28 am

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.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Detele duplicates from list

Post by nuntius » Sun Apr 18, 2010 10:16 am

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.

misha1988
Posts: 4
Joined: Sun Apr 18, 2010 5:49 am

Re: Detele duplicates from list

Post by misha1988 » Wed Apr 21, 2010 5:51 am

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 ?

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: Detele duplicates from list

Post by Jasper » Wed Apr 21, 2010 8:13 am

(send-to-lector #'remove-duplicates)

Anyway, send us your attempts..

Paul
Posts: 106
Joined: Tue Jun 02, 2009 6:00 am

Re: Detele duplicates from list

Post by Paul » Wed Apr 21, 2010 11:57 pm

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! ;)

misha1988
Posts: 4
Joined: Sun Apr 18, 2010 5:49 am

Re: Detele duplicates from list

Post by misha1988 » Thu Apr 22, 2010 4:59 am

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:

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: Detele duplicates from list

Post by Jasper » Thu Apr 22, 2010 6:40 am

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.

Post Reply