DELETE: An unexpected behavior

Discussion of Common Lisp
Post Reply
filfil
Posts: 12
Joined: Mon Aug 06, 2012 3:21 pm

DELETE: An unexpected behavior

Post by filfil » Sat Mar 29, 2014 6:32 am

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)
? 

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: DELETE: An unexpected behavior

Post by edgar-rft » Sat Mar 29, 2014 7:02 am

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

filfil
Posts: 12
Joined: Mon Aug 06, 2012 3:21 pm

Re: DELETE: An unexpected behavior

Post by filfil » Sat Mar 29, 2014 8:47 am

Thank you

Post Reply