Archived

This topic is now archived and is closed to further replies.

More lisp agony

This topic is 5091 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m trying to create a uniqueize function- a function that makes a list have all unique data pieces.(for about 3 hours now, I think) Uniqueize will return a list which has the property that (mathspeak time) L is list; for all l, l[j] in L, no l[i] != l[j] for all i,j not equal each other. The best way seems to me to go through the original list, put each piece in the new list if and only if it is not in the new list already. I''ve written a findinlist already. (existinlist is a better name really) ;0 = no, 1 = yes (defun findinlist (ele lis) (let ((yes 0)) (dolist (next lis yes) (if (equal ele next) (setf yes 1))))) Any ideas on how to code uniqueize? (No, I don''t know lisp, I''m a c++ guy learning it.) ~V''lion Bugle4d

Share this post


Link to post
Share on other sites
You can use a built-in function:

(remove-duplicates x)

Or you can do it imperatively:

(defun uniquize (x)
(let ((y nil))
(dolist (item x)
(setf y (adjoin item y)))
y))

Or you can do it recursively:

(defun uniquize (x)
(unless (endp x)
(adjoin (car x) (uniquize (cdr x)))))

Share this post


Link to post
Share on other sites
adjoin is a built in function, but you could code it as follows:

(defun adjoin (x list)
(if (find x list) list (cons x list)))

Share this post


Link to post
Share on other sites