Next: , Previous: , Up: srfi compare-procedures spec   [Index]


2.28.5.2 Comparing lists and vectors

In this section compare procedures are defined for Scheme lists and vectors—and for objects that can be accessed like lists or like vectors.

An object x can be accessed like a vector if there are procedures size and ref such that (size x) is a non–negative integer N indicating the number of elements, and (ref x i) is the i-th element of x for i \in {0, ..., N - 1}. The default vector access procedures are vector-length and vector-ref.

An object x can be accessed like a (proper) list if there are procedures empty?, head, and tail such that:

(empty? x)

Is a boolean indicating that there are no elements in x.

(head x)

Is the first element of x.

(tail x)

Is an object representing the residual elements of x.

The default list access procedures are null?, car, and cdr.

Independent of the way the elements are accessed, the natural ordering of vectors and lists differs:

Function: vector-compare x y
Function: vector-compare compare x y
Function: vector-compare compare x y size ref
Function: vector-compare-as-list x y
Function: vector-compare-as-list compare x y
Function: vector-compare-as-list compare x y size ref
Function: list-compare x y
Function: list-compare compare x y
Function: list-compare compare x y empty? head tail
Function: list-compare-as-vector x y
Function: list-compare-as-vector compare x y
Function: list-compare-as-vector compare x y empty? head tail

Compare two sequences x and y, using compare for comparing elements. The result is an exact integer in {-1, 0, +1}. If compare is not supplied, default-compare is used.

The procedure named access-compare-as-order accesses the objects like access and compares them with respect to the order given by order. The names type-compare are abbreviations for type-compare-as-type.

Examples:

(list-compare           '(2) '(1 2))    ⇒  1
(list-compare-as-vector '(2) '(1 2))    ⇒ -1
(vector-compare         '#(2) '#(1 2))  ⇒ -1
(vector-compare-as-list '#(2) '#(1 2))  ⇒  1

Next: , Previous: , Up: srfi compare-procedures spec   [Index]