Previous: Reduction of Lists, Up: Lists


7.9 Miscellaneous List Operations

— procedure: circular-list object ...
— procedure: make-circular-list k [element]

These procedures are like list and make-list, respectively, except that the returned lists are 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.

— 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)
     
— 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
— procedure: merge-sort sequence procedure
— procedure: quick-sort sequence procedure

Sequence must be either a list or a vector. Procedure must be a procedure of two arguments that defines a total ordering on the elements of sequence. In other words, if x and y are two distinct elements of sequence, then it must be the case that

          (and (procedure x y)
               (procedure y 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. 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 y x)
               => #f
     

Two sorting algorithms are implemented: merge-sort and quick-sort. The procedure sort is an alias for merge-sort.

See also the definition of sort!.