Re: Having trouble deleting first item in list



"Paul F. Dietz" <dietz@xxxxxxx> writes:

> Kent M Pitman wrote:
>
> > 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.
>
> Well, not necessarily. DELETE might have been implemented like this:
>
> (defun delete (item list)
> (cond
> ((null list) nil)
> ((eql (car list) item)
> (cond
> ((null (cdr list)) nil)
> (t
> (setf (car list) (cadr list))
> (setf (cdr list) (cddr list))
> (delete item list)))
> (t (setf (cdr list) (delete item (cdr list)))
> list)))

Yes, I've long known this. But
(a) this is makes predictable structure sharing unpredictable
(b) doesn't fix this bug:

> The bug is unavoidable if there is nothing in the list except
> the item to be deleted.

Given this, the above is an exercise in futility.
You still need the SETQ.
.



Relevant Pages