Message from discussion Sort is destructive (was Re: how to use nreverse?)
From: Kelly Murray <k...@IntelliMarket.Com>
Subject: Sort is destructive (was Re: how to use nreverse?)
References: <firstname.lastname@example.org> <36C06282.EABBBE59@computer.org> <email@example.com> <36C10342.F8F7EF13@computer.org>
Content-Type: text/plain; charset=us-ascii
Christopher J. Vogt wrote:
> > i see now that '(1 1) was not being copied but it got pointed to by
> > acc. i see i can avoid the problem by using (list 1 1) or (copy-list
> > '(1 1)).
> > hmm that was subtle, but i am learning.
> Indeed it is subtle, and just about every Lisp programmer has been bit
> by this when she was learning Lisp, and will never forget the lesson.
I thought I'd add that even this experienced lisp programmer was
bitten by the destructive operation of #'sort
spending many hours tracking down the bug it caused.
The problem was that AllegroStore copies a list every
time you access an objects slot, so doing a #'sort on the
list didn't have any effect on the persistent data.
But with my OODB, slot-access just returns a pointer to the list,
and thus sorting the list destructively modifies it in the DB.
The problem manifests itself by items disappearing from the list,
as referenced by the old head of the list, which is no longer
the head of the list after the sort.
And of course, the bug doesn't show up if the list was sorted
already, so the pointers don't get reshuffled.
I think #'sort should be renamed #'nsort ...
-Kelly Murray k...@intellimarket.com http://www.intellimarket.com