Page 1 of 1

DELETE: An unexpected behavior

Posted: Sat Mar 29, 2014 6:32 am
by filfil
Hello,

I've found a strange behavior in Clozure Common Lisp (but it's the same in Lispworks): if I apply "delete" function on a variable, the function returns the correct value, but, wherever the deleting item is the list first element, in the new variable value the item is deleted but not on the first index. Why?

Code: Select all

? (setf a '(1 2 3 2 1 2))
(1 2 3 2 1 2)
? a
(1 2 3 2 1 2)
? (delete 1 a)
(2 3 2 2)
? a
(1 2 3 2 2)
? (delete 2 a)
(1 3)
? a
(1 3)
? 

Re: DELETE: An unexpected behavior

Posted: Sat Mar 29, 2014 7:02 am
by edgar-rft
If you want the variable a to contain the list with all 1s deleted you need to write:

Code: Select all

(setf a (delete 1 a))
CLHS DELETE says "delete, delete-if, and delete-if-not ... may modify sequence", what means that it's only the return value of DELETE that counts, while the old contents of a is unreliable afterwards, it may be modified or not.

- edgar

Re: DELETE: An unexpected behavior

Posted: Sat Mar 29, 2014 8:47 am
by filfil
Thank you