Re: Can not (delete) last remaining element from a list
Camille Troillard <camille@osculator.net> writes:
> On 31 mai 2011, at 15:18, Nico de Jager wrote:
>
>> Destructive functions are not meant to be called for their side-effects.
>> They are destructive for reasons of efficiency. That means you should
>> use the return value of a destructive function and not count on any
>> modification of the argument(s). E.g.
>>
>> (setf (streams server) (delete stream (streams server)))
>>
>> Ansi Common Lisp by Paul Graham have a nice section about this (12.4 on
>> p. 201) and incidentally uses delete as an example.
>
> Thank you very much Nico for the reference to the book.
> I'll read that.
>
> Actually I got confused because I believed that delete was updating a
> "place", in the spirit of what (push) does.
Also, delete is a function, not a macro.
> Someone kindly pointed out that I've misinterpreted the spec, and that
> delete updates the given argument, not a place.
It is important to understand that delete is not required to modify the
argument, and even if it does, the argument (after the call) and the
result may not be the same. The CLHS entry for delete states (emphasis
added by me):
"delete item sequence &key from-end test test-not start end count key
=> result-sequence"
"delete, delete-if, and delete-if-not are like remove, remove-if, and
remove-if-not respectively, but they _may_ modify sequence."
and
"... delete, delete-if, and delete-if-not return a sequence of the same
type as sequence that has the same elements except that those in the
subsequence bounded by start and end and satisfying the test have been
deleted. Sequence may be destroyed and used to construct the result;
however, the result _might_ or _might_ _not_ be identical to sequence."
Nico