17.05.2015 Views

zl:1 - FTP

zl:1 - FTP

zl:1 - FTP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

493 sort<br />

For a table of related items: See the section "Predicates That Operate on<br />

Sequences" in Symbolics Common Lisp: Language Concepts.<br />

sort sequence predicate &key key Function<br />

sort destructively modifies sequence by sorting it according to an order<br />

determined by predicate. predicate should take two arguments and return a<br />

non-nil value if and only if the first argument is strictly less than the<br />

second (in some appropriate sense). If the first argument is greater than<br />

or equal to the second (in the appropriate sense), then predicate should<br />

return nil.<br />

The sort function determines the relationship between two elements by<br />

giving keys extracted from the elements to predicate. The :key argument,<br />

when applied to an element, should return the key for that element. It<br />

defaults to the identity function, thereby making the element itself the key.<br />

sequence can be either a list or a vector (one-dimensional array). Note that<br />

nil is considered to be a sequence, of length zero.<br />

The :key function should not have any side effects. A useful example of a<br />

:key function would be a component selector function for a defstruct structure,<br />

used in sorting a sequence of structures.<br />

If the :key and predicate functions always return, then the sorting operation<br />

will always terminate, producing a sequence containing the same elements<br />

as the original sequence (that is, the result is a permutation of<br />

sequence). This is guaranteed even if predicate does not really consistently<br />

represent a total order (in which case the elements will be scrambled in<br />

some unpredictable way, but no element will be lost). If the :key function<br />

consistently returns meaningful keys, and the predicate does reflect some<br />

total ordering criterion on those keys, then the elements of the result sequence<br />

will be properly sorted according to that ordering.<br />

For example:<br />

(sort #(1 3 2 4 3 5) #'» => #(5 4 3 3 2 1)<br />

•<br />

(sort '«up 2) (down 1) (west 4) (south 3)) #'< :key #'cadr)<br />

=> «DOWN 1) (UP 2) (SOUTH 3) (WEST 4))<br />

The sorting operation performed by sort is not guaranteed stable. Elements<br />

considered equal by predicate mayor may not be stay in their<br />

original order. predicate is assumed to consider two elements x and y to be<br />

equal if (funcall predicate x y) and (funcall predicate y x) are both false.<br />

The function stable-sort guarantees stability, but may be slower than sort<br />

in some situations.<br />

The sorting operation is destructive, so in the cases where the argument<br />

should not be destroyed, you must sort a copy of the argument. When the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!