DELETE: An unexpected behavior

Discussion of Common Lisp

DELETE: An unexpected behavior

Postby 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)
?
filfil
 
Posts: 12
Joined: Mon Aug 06, 2012 3:21 pm

Re: DELETE: An unexpected behavior

Postby 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
edgar-rft
 
Posts: 160
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: DELETE: An unexpected behavior

Postby filfil » Sat Mar 29, 2014 8:47 am

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


Return to Common Lisp

Who is online

Users browsing this forum: Bing [Bot] and 3 guests