[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Delete bug

Your "bug" is really a "feature" if you read CLtL2 carefully, and has
been true in all Lisps for all time.  When you delete the first
element of a list, you must wind up with your "root" to it pointing to
its (formerly) second pair.  That means you have to actually change
the root, which in your example is the variable x.  Because delete
never sees x (only its value), it has no way of changing it.  What you
want instead is

(setf <root> (delete 13 <root>))
which is
(setf x (delete 13 x))

There is an obscure way to get around this except in the case where
you are trying to delete the last element of a list, but it's gross:
You can copy the car and cdr of the second pair in the list over the
car and cdr of the first pair.  Then, x still points to the actual
first pair but you have effectively eliminated it.  This fails if you
are trying to delete the only element of the list, and has other
problems to boot.  So get used to the idiom above.