aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Antipov2014-08-29 15:02:56 +0400
committerDmitry Antipov2014-08-29 15:02:56 +0400
commitdd958fb246a13c024ce58415c3de99f4390bcd77 (patch)
tree3ef3b5be28a1702e7c5b5276dfd77503b0492061
parentf0e709846e9a5a6b77a2f526fed37624771f19e6 (diff)
downloademacs-dd958fb246a13c024ce58415c3de99f4390bcd77.tar.gz
emacs-dd958fb246a13c024ce58415c3de99f4390bcd77.zip
* doc/lispref/lists.texi (Functions that Rearrange Lists): Remove
description of sort ... * doc/lispref/sequences.texi (Sequence Functions): ... and generalize it for sequences. Add an example. * src/fns.c (Fsort): Use more natural Qsequencep error. * test/automated/fns-tests.el (fns-tests-sort): Minor style rewrite.
-rw-r--r--doc/lispref/ChangeLog7
-rw-r--r--doc/lispref/lists.texi68
-rw-r--r--doc/lispref/sequences.texi92
-rw-r--r--src/fns.c2
-rw-r--r--test/automated/fns-tests.el4
5 files changed, 102 insertions, 71 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog
index 783be72e830..a9954d84658 100644
--- a/doc/lispref/ChangeLog
+++ b/doc/lispref/ChangeLog
@@ -1,3 +1,10 @@
12014-08-29 Dmitry Antipov <dmantipov@yandex.ru>
2
3 * lists.texi (Functions that Rearrange Lists): Remove
4 description of sort ...
5 * sequences.texi (Sequence Functions): ... and generalize
6 it for sequences. Add an example.
7
12014-08-28 Eli Zaretskii <eliz@gnu.org> 82014-08-28 Eli Zaretskii <eliz@gnu.org>
2 9
3 * display.texi (Bidirectional Display): Update the Emacs's class 10 * display.texi (Bidirectional Display): Update the Emacs's class
diff --git a/doc/lispref/lists.texi b/doc/lispref/lists.texi
index f724d5bd902..21be5cca4fc 100644
--- a/doc/lispref/lists.texi
+++ b/doc/lispref/lists.texi
@@ -1124,74 +1124,6 @@ each time you run it! Here is what happens:
1124@end smallexample 1124@end smallexample
1125@end defun 1125@end defun
1126 1126
1127@defun sort list predicate
1128@cindex stable sort
1129@cindex sorting lists
1130This function sorts @var{list} stably, though destructively, and
1131returns the sorted list. It compares elements using @var{predicate}. A
1132stable sort is one in which elements with equal sort keys maintain their
1133relative order before and after the sort. Stability is important when
1134successive sorts are used to order elements according to different
1135criteria.
1136
1137The argument @var{predicate} must be a function that accepts two
1138arguments. It is called with two elements of @var{list}. To get an
1139increasing order sort, the @var{predicate} should return non-@code{nil} if the
1140first element is ``less than'' the second, or @code{nil} if not.
1141
1142The comparison function @var{predicate} must give reliable results for
1143any given pair of arguments, at least within a single call to
1144@code{sort}. It must be @dfn{antisymmetric}; that is, if @var{a} is
1145less than @var{b}, @var{b} must not be less than @var{a}. It must be
1146@dfn{transitive}---that is, if @var{a} is less than @var{b}, and @var{b}
1147is less than @var{c}, then @var{a} must be less than @var{c}. If you
1148use a comparison function which does not meet these requirements, the
1149result of @code{sort} is unpredictable.
1150
1151The destructive aspect of @code{sort} is that it rearranges the cons
1152cells forming @var{list} by changing @sc{cdr}s. A nondestructive sort
1153function would create new cons cells to store the elements in their
1154sorted order. If you wish to make a sorted copy without destroying the
1155original, copy it first with @code{copy-sequence} and then sort.
1156
1157Sorting does not change the @sc{car}s of the cons cells in @var{list};
1158the cons cell that originally contained the element @code{a} in
1159@var{list} still has @code{a} in its @sc{car} after sorting, but it now
1160appears in a different position in the list due to the change of
1161@sc{cdr}s. For example:
1162
1163@example
1164@group
1165(setq nums '(1 3 2 6 5 4 0))
1166 @result{} (1 3 2 6 5 4 0)
1167@end group
1168@group
1169(sort nums '<)
1170 @result{} (0 1 2 3 4 5 6)
1171@end group
1172@group
1173nums
1174 @result{} (1 2 3 4 5 6)
1175@end group
1176@end example
1177
1178@noindent
1179@strong{Warning}: Note that the list in @code{nums} no longer contains
11800; this is the same cons cell that it was before, but it is no longer
1181the first one in the list. Don't assume a variable that formerly held
1182the argument now holds the entire sorted list! Instead, save the result
1183of @code{sort} and use that. Most often we store the result back into
1184the variable that held the original list:
1185
1186@example
1187(setq nums (sort nums '<))
1188@end example
1189
1190@xref{Sorting}, for more functions that perform sorting.
1191See @code{documentation} in @ref{Accessing Documentation}, for a
1192useful example of @code{sort}.
1193@end defun
1194
1195@node Sets And Lists 1127@node Sets And Lists
1196@section Using Lists as Sets 1128@section Using Lists as Sets
1197@cindex lists as sets 1129@cindex lists as sets
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi
index 8f17862d427..d3a6792c1ba 100644
--- a/doc/lispref/sequences.texi
+++ b/doc/lispref/sequences.texi
@@ -327,6 +327,98 @@ encouraged to treat strings as immutable.
327 327
328@end defun 328@end defun
329 329
330@defun sort sequence predicate
331@cindex stable sort
332@cindex sorting lists
333@cindex sorting vectors
334This function sorts @var{sequence} stably. Note that this function doesn't work
335for all sequences; it may be used only for lists and vectors. If @var{sequence}
336is a list, it is modified destructively. This functions returns the sorted
337@var{sequence} and compares elements using @var{predicate}. A stable sort is
338one in which elements with equal sort keys maintain their relative order before
339and after the sort. Stability is important when successive sorts are used to
340order elements according to different criteria.
341
342The argument @var{predicate} must be a function that accepts two
343arguments. It is called with two elements of @var{sequence}. To get an
344increasing order sort, the @var{predicate} should return non-@code{nil} if the
345first element is ``less than'' the second, or @code{nil} if not.
346
347The comparison function @var{predicate} must give reliable results for
348any given pair of arguments, at least within a single call to
349@code{sort}. It must be @dfn{antisymmetric}; that is, if @var{a} is
350less than @var{b}, @var{b} must not be less than @var{a}. It must be
351@dfn{transitive}---that is, if @var{a} is less than @var{b}, and @var{b}
352is less than @var{c}, then @var{a} must be less than @var{c}. If you
353use a comparison function which does not meet these requirements, the
354result of @code{sort} is unpredictable.
355
356The destructive aspect of @code{sort} for lists is that it rearranges the
357cons cells forming @var{sequence} by changing @sc{cdr}s. A nondestructive
358sort function would create new cons cells to store the elements in their
359sorted order. If you wish to make a sorted copy without destroying the
360original, copy it first with @code{copy-sequence} and then sort.
361
362Sorting does not change the @sc{car}s of the cons cells in @var{sequence};
363the cons cell that originally contained the element @code{a} in
364@var{sequence} still has @code{a} in its @sc{car} after sorting, but it now
365appears in a different position in the list due to the change of
366@sc{cdr}s. For example:
367
368@example
369@group
370(setq nums '(1 3 2 6 5 4 0))
371 @result{} (1 3 2 6 5 4 0)
372@end group
373@group
374(sort nums '<)
375 @result{} (0 1 2 3 4 5 6)
376@end group
377@group
378nums
379 @result{} (1 2 3 4 5 6)
380@end group
381@end example
382
383@noindent
384@strong{Warning}: Note that the list in @code{nums} no longer contains
3850; this is the same cons cell that it was before, but it is no longer
386the first one in the list. Don't assume a variable that formerly held
387the argument now holds the entire sorted list! Instead, save the result
388of @code{sort} and use that. Most often we store the result back into
389the variable that held the original list:
390
391@example
392(setq nums (sort nums '<))
393@end example
394
395For the better understanding of what stable sort is, consider the following
396vector example. After sorting, all items whose @code{car} is 8 are grouped
397at the beginning of @code{vector}, but their relative order is preserved.
398All items whose @code{car} is 9 are grouped at the end of @code{vector},
399but their relative order is also preserved:
400
401@example
402@group
403(setq
404 vector
405 (vector '(8 . "xxx") '(9 . "aaa") '(8 . "bbb") '(9 . "zzz")
406 '(9 . "ppp") '(8 . "ttt") '(8 . "eee") '(9 . "fff")))
407 @result{} [(8 . "xxx") (9 . "aaa") (8 . "bbb") (9 . "zzz")
408 (9 . "ppp") (8 . "ttt") (8 . "eee") (9 . "fff")]
409@end group
410@group
411(sort vector (lambda (x y) (< (car x) (car y))))
412 @result{} [(8 . "xxx") (8 . "bbb") (8 . "ttt") (8 . "eee")
413 (9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")]
414@end group
415@end example
416
417@xref{Sorting}, for more functions that perform sorting.
418See @code{documentation} in @ref{Accessing Documentation}, for a
419useful example of @code{sort}.
420@end defun
421
330@node Arrays 422@node Arrays
331@section Arrays 423@section Arrays
332@cindex array 424@cindex array
diff --git a/src/fns.c b/src/fns.c
index 8845a43fc4b..a454341fce6 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -1958,7 +1958,7 @@ if the first element should sort before the second. */)
1958 else if (VECTORP (seq)) 1958 else if (VECTORP (seq))
1959 seq = sort_vector (seq, predicate); 1959 seq = sort_vector (seq, predicate);
1960 else if (!NILP (seq)) 1960 else if (!NILP (seq))
1961 wrong_type_argument (Qarrayp, seq); 1961 wrong_type_argument (Qsequencep, seq);
1962 return seq; 1962 return seq;
1963} 1963}
1964 1964
diff --git a/test/automated/fns-tests.el b/test/automated/fns-tests.el
index a6c45443db6..9f55a873ea4 100644
--- a/test/automated/fns-tests.el
+++ b/test/automated/fns-tests.el
@@ -113,8 +113,8 @@
113 (should (equal 113 (should (equal
114 (sort 114 (sort
115 (vector 115 (vector
116 (cons 8 "xxx") (cons 9 "aaa") (cons 8 "bbb") (cons 9 "zzz") 116 '(8 . "xxx") '(9 . "aaa") '(8 . "bbb") '(9 . "zzz")
117 (cons 9 "ppp") (cons 8 "ttt") (cons 8 "eee") (cons 9 "fff")) 117 '(9 . "ppp") '(8 . "ttt") '(8 . "eee") '(9 . "fff"))
118 (lambda (x y) (< (car x) (car y)))) 118 (lambda (x y) (< (car x) (car y))))
119 [(8 . "xxx") (8 . "bbb") (8 . "ttt") (8 . "eee") 119 [(8 . "xxx") (8 . "bbb") (8 . "ttt") (8 . "eee")
120 (9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")]))) 120 (9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")])))