Re: Having trouble deleting first item in list
- From: Kent M Pitman <pitman@xxxxxxxxxxx>
- Date: Tue, 28 Jun 2005 14:49:32 GMT
"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.
.
- Follow-Ups:
- Re: Having trouble deleting first item in list
- From: Paul F. Dietz
- Re: Having trouble deleting first item in list
- References:
- Having trouble deleting first item in list
- From: jonathon
- Having trouble deleting first item in list
- Prev by Date: Re: I can't find which line is causing the error
- Next by Date: Re: Having trouble deleting first item in list
- Previous by thread: Having trouble deleting first item in list
- Next by thread: Re: Having trouble deleting first item in list
- Index(es):