From: P. Srinivas
Subject: Unexpected DELETE behaviour
Date: 
Message-ID: <4o3j6d$qod@tribune.usask.ca>
Last nigth I encountered an unexpected behaviour of DELETE in common Lisp.
DELETE is supposed to be a DESTRUCTIVE version fo REMOVE. But when
the element that is being deleted from a sequence is the very first
element, then DELETE behaves like REMOVE.

Lisp> (setq a '(1 2 3 4))
(1 2 3 4)
Lisp> (delete 1 a)
(2 3 4)
Lisp> a
(1 2 3 4)
Lisp> (delete 2 a)
(1 3 4)
Lisp> a
(1 3 4)


Is this a documented feature? It happened in both Allegro and Herlequin 
common Lisp systems.


Srini

--
   URL http://www.cs.usask.ca/homepages/grads/srini/
--------------------
Srinivas Palthepu                             Email: ·····@cs.usask.ca
ARIES laboratory,                            Phones: (306) 966-8654 (lab)
Department of Computer Science,                      (306) 966-4759 (office)
University of Saskatchewan,                          (306) 373-6724 (home)    
57 Campus Dr, Saskatoon, SK, S7N 5A9            Fax: (306) 966-4884

From: Pete Grant
Subject: Re: Unexpected DELETE behaviour
Date: 
Message-ID: <4o3spt$bd1@news2.h1.usa.pipeline.com>
On May 24, 1996 05:58:37 in article <Unexpected DELETE behaviour>,
······@skorpio3.usask.ca (P. Srinivas)' wrote: 
 
 
>Last nigth I encountered an unexpected behaviour of DELETE in common Lisp.

>DELETE is supposed to be a DESTRUCTIVE version fo REMOVE. But when 
>the element that is being deleted from a sequence is the very first 
>element, then DELETE behaves like REMOVE. 
> 
>Lisp> (setq a '(1 2 3 4)) 
>(1 2 3 4) 
>Lisp> (delete 1 a) 
>(2 3 4) 
>Lisp> a 
>(1 2 3 4) 
>Lisp> (delete 2 a) 
>(1 3 4) 
>Lisp> a 
>(1 3 4) 
> 
> 
>Is this a documented feature? It happened in both Allegro and Herlequin  
>common Lisp systems. 
> 
Yes, delete is a function.  To get the expected behavior, you must 
capture its return value, e.g.,  
 
(setf a (delete 1 a)) 
 
To understand why, requires that you are familiar with cons 
cells and what it means to bind a variable to a value.  Roughly, 
the symbol 'a is bound to (points at) the first element of the 
list.  Delete modifies the list itself, but not what 'a points to. 
 
After calling delete, he symbol 'a still points to the same cons 
cell whose cdr still points to the second element in the list --  
unless the second element is also deleted. 
 
The botto line:  always capture the return value of sequence 
functions. 
From: Paul McNamee
Subject: Re: Unexpected DELETE behaviour
Date: 
Message-ID: <4o4gpeINNakh@topdog.cs.umbc.edu>
In article <··········@tribune.usask.ca>,
P. Srinivas <·····@skorpio3.usask.ca> wrote:
>Last nigth I encountered an unexpected behaviour of DELETE in common Lisp.
>DELETE is supposed to be a DESTRUCTIVE version fo REMOVE. But when
>the element that is being deleted from a sequence is the very first
>element, then DELETE behaves like REMOVE.
[example deleted]

This one's in the FAQ, 3-3:
  http://www.cs.cmu.edu/Web/Groups/AI/html/faqs/lang/lisp/part3/faq-doc-4.html

Remember that many destructive operations are not required to modify
input sequences although they are permitted to.  In particular, CLTL2
14.3 says:
  "The argument sequence may be destroyed and used to construct the
result;"                ^^^^^

Cheers,

Paul McNamee
From: Asle Olufsen
Subject: Re: Unexpected DELETE behaviour
Date: 
Message-ID: <4o54o7$1c4@aj.ifi.uio.no>
In article <··········@tribune.usask.ca>, ·····@skorpio3.usask.ca (P. Srinivas) writes:
> Last nigth I encountered an unexpected behaviour of DELETE in common Lisp.
> DELETE is supposed to be a DESTRUCTIVE version fo REMOVE. But when
> the element that is being deleted from a sequence is the very first
> element, then DELETE behaves like REMOVE.
> 
> Lisp> (setq a '(1 2 3 4))
> (1 2 3 4)
> Lisp> (delete 1 a)
> (2 3 4)
> Lisp> a
> (1 2 3 4)
> Lisp> (delete 2 a)
> (1 3 4)
> Lisp> a
> (1 3 4)
> 
> 
> Is this a documented feature? It happened in both Allegro and Herlequin 
> common Lisp systems.
> 

In "ANSI Common Lisp" page 201, Paul Graham says:
 
   Common Lisp includes several functions that are allowed to 
   modify list structure. These functions are destructive for 
   reasons of efficiency. Though they may recycle conses passed
   to them as arguments, they are not ment to be called for their
   side-effects.

   For example, delete is a destructive version of remove. While
   it is allowed to trash the list passed to it as an argument, 
   it doesn't promise to do anything. This is what happens on most
   implementations:

   > (setf lst '(a r a b i a))
   (A R A B I A)
   > (delete 'a lst)
   (R B I)
   > lst
   (A R B I)

   As with remove, if you want side-effects, you should use setf 
   with the return value:

   (setf lst (delete 'a lst))


I strongly recomend the book to all beginners.  

Personally I would have used setq instead of setf, but that's a 
different debate.

Oluf
From: Kevin H. Sewell
Subject: Re: Unexpected DELETE behaviour
Date: 
Message-ID: <psewell-2805960008490001@psewell.easynet.co.uk>
In article <··········@tribune.usask.ca>, ·····@skorpio3.usask.ca (P.
Srinivas) wrote:

> Last nigth I encountered an unexpected behaviour of DELETE in common Lisp.
> DELETE is supposed to be a DESTRUCTIVE version fo REMOVE. But when
> the element that is being deleted from a sequence is the very first
> element, then DELETE behaves like REMOVE.
> 
> Lisp> (setq a '(1 2 3 4))
> (1 2 3 4)
> Lisp> (delete 1 a)
> (2 3 4)
> Lisp> a
> (1 2 3 4)
> Lisp> (delete 2 a)
> (1 3 4)
> Lisp> a
> (1 3 4)
> 
> 
> Is this a documented feature? It happened in both Allegro and Herlequin 
> common Lisp systems.
> 
> 
> Srini
> 

delete is destructive.  The argument MAY be destroyed and used to
construct the result.  Sometimes (as in the first call), it is not worth
it.

Destructive operations are implementation-dependent (some implementations
will modify the argument and some will not), so using them for their
side-effects is not nice.

Try,
(setq a (delete 2 a))

or something.

Kev