inconsistent definition of "sort" (and what about stability?)
Subject: inconsistent definition of "sort" (and what about stability?)
From: Robert Dodier
Date: Sat, 19 Nov 2011 14:14:16 -0700
I've pushed the following changes to the implementation and documentation
of sort in an attempt to address the questions raised a few days ago.
Hope this helps. If further discussion is needed, well, have at it.
best
Robert Dodier
PS.
diff --git a/doc/info/Lists.texi b/doc/info/Lists.texi
index 861f280..81d25bc 100644
--- a/doc/info/Lists.texi
+++ b/doc/info/Lists.texi
@@ -889,7 +889,7 @@ See @mref{first} for more details.
@deffnx {Function} sort (@var{L})
Sorts a list @var{L} according to a predicate @code{P} of two arguments, such
-that @code{@var{P} (@var{L}[k], @var{L}[k + 1])} is @code{true} for any two
+that @code{@var{P}(@var{L}[k + 1], @var{L}[k])} is @code{false} for any two
successive elements. The predicate may be specified as the name of a function
or binary infix operator, or as a @code{lambda} expression. If specified as
the name of an operator, the name is enclosed in "double quotes".
@@ -900,11 +900,16 @@ the elements of @var{L}.
@c DUNNO IF WE NEED TO GO INTO THE IMPLICATIONS OF SHALLOW COPY HERE ...
@c MIGHT CONSIDER A REF FOR TOTAL ORDER HERE
-If the predicate @var{P} is not a total order on the elements of @var{L}, then
- at code{sort} might run to completion without error, but the result is undefined.
+It is assumed the predicate @var{P} is a strict total order on the
elements of @var{L}.
+If not, @code{sort} might run to completion without error, but the
result is undefined.
@code{sort} complains if the predicate evaluates to something other than
@code{true} or @code{false}.
+ at code{sort} is a stable sort:
+if two elements @var{x} and @var{y} are equivalent in the sense that
+ at code{@var{P}(@var{x}, @var{y})} and @code{@var{P}(@var{y}, @var{x})}
are both @code{false},
+then the relative order of @var{x} and @var{y} in @var{L} is
preserved by @code{sort}.
+
@code{sort (@var{L})} is equivalent to @code{sort (@var{L}, orderlessp)}.
That is, the default sorting order is ascending, as determined by
@mrefdot{orderlessp} All Maxima atoms and expressions are comparable under
diff --git a/src/mstuff.lisp b/src/mstuff.lisp
index 2dbba30..82b4184 100644
--- a/src/mstuff.lisp
+++ b/src/mstuff.lisp
@@ -21,7 +21,7 @@
(mfunction1 (setq bfun (getopr f))))
(when (member bfun '(lessthan great) :test #'eq)
(setq llist (mapcar #'ratdisrep llist)))
- (cons '(mlist) (sort llist comparfun))))
+ (cons '(mlist) (stable-sort llist comparfun))))
;; cmulisp does not like the closure version. Clisp insists on the
;; closure version. Gcl likes either... For the moment we will