Re: Having trouble deleting first item in list



"jonathon" <j_mckitrick@xxxxxxxxxxx> writes:

> This one has me confused. I have a list of objects. Here is the
> function to delete one of them:
>
> (defun delete-transaction (index)
> :documentation "Delete a transaction."
> (decf index)
> (with-tm *tm*
> (setf transactions (delete (nth index transactions) transactions)))
> ; (delete (nth index transactions) transactions))
> (format t "(tm) Transactions left: ~A~%" (count-transactions)))
>
> '*tm*' is an object that holds transaction objects.
> 'transactions' is the list of transaction objects internal to *tm*.
>
> This version of the function works as expected. But when I uncomment
> the one line and comment out the setf line, it will never delete the
> first (zeroth) entry. Why is this?

You're experiencing the "DELETE bug".

You need to do
(setf transactions (delete (nth index transactions) transactions))
DELETE does not receive a pointer to the location that holds the list and
is unable to update it. You _must_ use the return value from DELETE and
not rely on DELETE for side-effect, since in the case that the element to
be deleted is the first element, DELETE will just _return_ the next tail of
the list that doesn't contain the to-be-deleted elements and will not
have a side-effect--there is no side-effect it could possibly have.


.