From: Bob Riemenschneider
Subject: CL sort functions
Date: 
Message-ID: <9006162223.AA04736@kronos.ads.com>
=>   From: ยทยทยทยท@edsr.eds.com (Pete Humphrey)
=>
=>   The Common Lisp definition of the sort function allows it to
=>   destructively modify the sequence, but does it allow it delete items
=>   from the sequence?  For example, is the following behavior in Allegro
=>   Common Lisp a bug?
=>
=>       <cl> (setq xxx (list '(1 a) '(0 b) '(1 c) '(0 e) '(1 f)))
=>
=>       ((1 A) (0 B) (1 C) (0 E) (1 F)) 
=>
=>       <cl> (sort xxx #'< :key #'first)
=>
=>       ((0 B) (0 E) (1 A) (1 C) (1 F)) 
=>
=>       <cl> xxx
=>
=>       ((1 A) (1 C) (1 F)) 

Nope, this is the way it should behave.  Before the sort switched some
pointers around, xxx's value cell contained a pointer to the (1 A); after
the sort, xxx's value cell contains the same pointer to that same cons
cell.  (Why shouldn't it?)

[PICTORIAL ASIDE]
When you're surprised by the result of a destructive operation, it
often helps to draw a "boxes and arrows" diagram.  Here's what the
relevant chunk of memory looks like before the sort:



"xxx's
 value 
 cell"
 |
 |
 v
[o|o]---------->[o|o]---------->[o|o]---------->[o|o]---------->[o|/]
 |               |               |               |               |
 |               |               |               |               |
 v               v               v               v               v
[o|o]--->[o|/]  [o|o]--->[o|/]  [o|o]--->[o|/]  [o|o]--->[o|/]  [o|o]--->[o|/]
 |        |      |        |      |        |      |        |      |        |
 |        |      |        |      |        |      |        |      |        |
 v        v      v        v      v        v      v        v      v        v
 1        A      0        B      1        C      0        E      1        F



And here's what it looks like after the sort:



"xxx's
 value 
 cell"
 |
 | -------------------------------------------------
 | |                                               |
 | |               ------------------------------- |
 | |               |                             | |
 | |   ------------+--------------     ----------+-+--------------
 | |   |           |             |     |         | |             |
 v v   |           |             v     |         v |             v
[o|o]---        [o|o]           [o|o]---        [o|o]---------->[o|/]
 |               |               |               |               |
 |               |               |               |               |
 v               v               v               v               v
[o|o]--->[o|/]  [o|o]--->[o|/]  [o|o]--->[o|/]  [o|o]--->[o|/]  [o|o]--->[o|/]
 |        |      |        |      |        |      |        |      |        |
 |        |      |        |      |        |      |        |      |        |
 v        v      v        v      v        v      v        v      v        v
 1        A      0        B      1        C      0        E      1        F



Note that after the sort, xxx ==> ((1 A) (1 C) (1 F)).  Nothing's been deleted.
[END OF PICTORIAL ASIDE]

As you no doubt observed, sort returned the desired value.  If you want xxx
to return that value, set it accordingly.

   (setq xxx (sort xxx #'< :key #'first))


							-- rar