Newsgroups: comp.lang.lisp
Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!hellgate.utah.edu!cdr.utah.edu!moore
From: moore%cdr.utah.edu@cs.utah.edu (Tim Moore)
Subject: Re: Are "destructive" functions really destructive?
Date: 29 Nov 89 11:49:23 MST
Message-ID: <1989Nov29.114923.1843@hellgate.utah.edu>
Organization: University of Utah CS Dept
References: <20991@mimsy.umd.edu> <1989Nov29.173139.26165@Neon.Stanford.EDU>
Distribution: usa

In article <1989Nov29.173139.26165@Neon.Stanford.EDU> michaelg@Neon.Stanford.EDU (Michael Greenwald) writes:
>folta@tove.umd.edu (Wayne Folta) writes:
>
>>I take it from the Common LISP Reference that functions marked "(destructive)"
>>are destructive in the sense that they *may be* destructive in some way.
>>Is it thus true that I cannot count on destructive behavior when I want it?
>
>Yes. 

>>In the Reference, it says, "..., or sequence may be unchanged and another
>>sequence returned.", which would seem to make "delete" different from "nconc"
>>in this respect. So destructiveness is an option for the convenience of LISP
>>implementation creators, not users?
>
>No, this behavior is dictated by the semantics of delete.  To wit, 
>
>(let ((x (copy-list '(a b c d e f))))
>  (setq y (delete 'a x)))
>
>There is no reasonable way to destructively "modify" x so that delete
>will work and you maitain EQness, since in this case DELETE == CDR.

There are unreasonable ways to do this, but michaelg correctly points
out that it's far more efficient to return the cdr of the original
list as the new sequence.

But consider the case of 

(let ((x (copy-list '(a a a))))
  (setq x (delete 'a x)))

Here, there's no way that (delete 'a x) could by itself modify the
variable x to be NIL because delete gets a copy of x's value, not of
x's location. 

If delete's behavior really bothers you, you can construct a deletef
macro like:

(defmacro deletef (item place)
  `(setf ,place (delete ,item ,place)))

Obviously this isn't completely equivalent to delete; caveat emptor
and all that. 

Tim Moore                     moore@cs.utah.edu {bellcore,hplabs}!utah-cs!moore
"Ah, youth. Ah, statute of limitations."
		-John Waters
