Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!uunet!samsung!usc!wuarchive!udel!rochester!koomen From: koomen@cs.rochester.edu (Hans Koomen) Newsgroups: comp.lang.lisp Subject: Re: Are "destructive" functions really destructive? Message-ID: <1989Nov29.191106.10329@cs.rochester.edu> Date: 29 Nov 89 19:11:06 GMT References: <20991@mimsy.umd.edu> <1989Nov29.173139.26165@Neon.Stanford.EDU> Reply-To: koomen@cs.rochester.edu (Hans Koomen) Distribution: usa Organization: University of Rochester Computer Science Department Lines: 28 Michael Greenwald writes: ... >(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. >In other words, you lose the first cons cell, and the rest of the list >is untouched. > >(I'm ignoring the pathological implementation of altering the CAR of >every sublist, and knocking off the last cons.) > You don't need to walk down the entire list to knock off the last cons. When deleting the first element of a 2+ element list, the Interlisp equivalent of Common Lisp delete, dremove, effectively does (progn (rplaca x (cadr x)) (rplacd x (cddr x))) i.e., splices out the second cons after stuffing value of the second car into the first car. No pathology required here to maintain eq-ness. The real problem lies in removing the first element of a 1-element list, e.g. if x is bound to '(a) then (delete 'a x) returns nil, but there's no way for the delete function to know that its second argument, (a), was the binding of the variable x, and that x should be set to nil! -- - - - /-/ a n s