Code: Select all
(defun uniq (L)
(if (= (length L) 0) NIL
(if (> (length L) 1)
(uniq (delete (first L) L))))
L)
Code: Select all
(defun uniq (L)
(if (= (length L) 0) NIL
(if (> (length L) 1)
(uniq (delete (first L) L))))
L)
It seems you are trying to destructively modify the list. If you want to modify the list, you should explicitly say so using setf:blerta12 wrote:Hello guys! I'm trying to write my own remove-duplicates function. The code I have written so far works fine when the repeating elements of the list are non-adjacent, but it doesn't work at all when they are. Any suggestions? Thank you in advance.
Code: Select all
(defun uniq (L) (if (= (length L) 0) NIL (if (> (length L) 1) (uniq (delete (first L) L)))) L)
Code: Select all
(defun uniq (L)
(if (= (length L) 0)
NIL
(when (> (length L) 1)
(setf (cdr L) (delete (first L) L))
(uniq (cdr L))))
L)
Code: Select all
(defun uniq (L)
(if (null L)
NIL
(when (not (null (cdr L)))
(setf (cdr L) (delete (first L) L))
(uniq (cdr L))))
L)