Previous: , Up: Lists   [Contents][Index]


7.9 Miscellaneous List Operations

SRFI 1 procedure: circular-list object …
procedure: make-circular-list k [element]

circular-list returns a circular list containing the given objects. make-circular-list returns a circular list of length k; if element is given, the returned list is filled with it, otherwise the elements are unspecified.

This procedure is like list except that the returned list is circular. circular-list could have been defined like this:

(define (circular-list . objects)
  (append! objects objects))

circular-list is compatible with SRFI 1, but extended so that it can be called with no arguments.

standard procedure: reverse list

Returns a newly allocated list consisting of the top-level elements of list in reverse order.

(reverse '(a b c))                  ⇒ (c b a)
(reverse '(a (b c) d (e (f))))      ⇒ ((e (f)) d (b c) a)
SRFI 1 procedure: reverse! list

Returns a list consisting of the top-level elements of list in reverse order. reverse! is like reverse, except that it destructively modifies list. Because the result may not be eqv? to list, it is desirable to do something like (set! x (reverse! x)).

procedure: sort sequence procedure [key]
procedure: merge-sort sequence procedure [key]
procedure: quick-sort sequence procedure [key]

Sequence must be either a list or a vector. Key, if specified, must be a procedure of one argument that maps an element of sequence to a key fit for comparison by procedure; by default, key is the identity. Procedure must be a procedure of two arguments that defines a total ordering on the keys of sequence. In other words, if x and y are two distinct elements of sequence, then it must be the case that

(and (procedure (key x) (key y))
     (procedure (key y) (key x)))
     ⇒ #f

If sequence is a list (vector), sort returns a newly allocated list (vector) whose elements are those of sequence, except that they are rearranged to be sorted in the order defined by procedure and key. So, for example, if the elements of sequence are numbers, and procedure is <, then the resulting elements are sorted in monotonically nondecreasing order. Likewise, if procedure is >, the resulting elements are sorted in monotonically nonincreasing order. To be precise, if x and y are any two adjacent elements in the result, where x precedes y, it is the case that

(procedure (key y) (key x))
     ⇒ #f

Two sorting algorithms are implemented: merge-sort and quick-sort. The procedure sort is an alias for merge-sort. Merge-sort is stable, meaning that it preserves the order in sequence of elements which are equivalent under procedure and key; quick-sort is not stable, so it does not guarantee this.

See also the definition of sort!.

(merge-sort '((2 . foo) (2 . bar) (1 . baz) (3 . quux)) < car)
⇒ ((1 . baz) (2 . foo) (2 . bar) (3 . quux))

(quick-sort '((2 . foo) (2 . bar) (1 . baz) (3 . quux)) < car)
⇒ ((1 . baz) (2 . bar) (2 . foo) (3 . quux))

Previous: Folding of Lists, Up: Lists   [Contents][Index]