[comp.lang.lisp] is "destructive" function really destructive?

kwong@polyslo.CalPoly.EDU (Ka Chin Wong) (12/06/89)

Someone brought up this two s-expressions:

  1. (delete item huge-list :key #'my-key)
  2. (setq huge-list (delete item huge-list :key #'my-key))

I would say only number 2 is correct.

According to COMMON LISP by GUY L. STEELE JR Delete returns a sequence
which all occurances of item in the sequence is removed.  Argument
sequence MAYBE destroyed and used to construct the result.

Notice that after return from Delete, the argument sequence is not 
necessary the result.  What you want is the sequence returned by
function Delete.  It makes sense if you think of LISP as a functional
language.  Remove works simularly, only that it does not modify
argument sequence (maybe it calls copy-tree internally to operate
on the copied sequence).  In short, you should count on function
return value in LISP most of the time since it is a functional 
programming language.  An exception may be function Format.  You
don't really care what it returns.  But well, LISP is not a pure
functional language anyway.

Below is an example of how Delete works.

>(setq foo '(nil a nil b nil c))
>(nil a nil b nil c)
>(delete nil foo)
>(a b c)
>foo
>(nil a b c)

Rick