Re: Having trouble deleting first item in list



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

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

	Paul
.



Relevant Pages

  • Re: ANSI Common Lisp: Chp. 6, #6
    ... the side-effect and return max afterwards, so it is more easy to see ... (defun max-so-far (n) ... (setf max n)) ...
    (comp.lang.lisp)
  • Re: How can prevent defun use global values?
    ... > as a side-effect? ... its value is nil. ... Special documentation: ... This variable controls whether assignments to unknown variables at top-level ...
    (comp.lang.lisp)