Next: , Previous: , Up: comparisons   [Index]


1.17.6 Comparing pairs and improper lists

This section describes comparison procedures for Scheme pairs and (possibly) improper lists.

Function: pair-compare-car compare
Function: pair-compare-cdr compare

Construct a compare procedure on pairs which only uses the car (or, respectively, the cdr), and ignores the other. One could define:

(define (pair-compare-car compare)
  (lambda (x y)
    (compare (car x) (car y))))

pair-compare-car can be used to turn a search data structure (e.g. a heap) into a dictionary: Store ‘(key . value)’ pairs and compare them using the compare procedure (pair-compare-car compare-key).

Function: pair-compare compare-car compare-cdr pair1 pair2
Function: pair-compare compare obj1 obj2
Function: pair-compare obj1 obj2

Compare two pairs, or (possibly improper) lists.

The quaternary form compares pair1 and pair2 by comparing their cars using compare-car, and if the cars are equal the cdrs are compared using compare-cdr.

The ternary form compares two objects by type using the ordering of types:

null < pair < neither-null-nor-pair

Two objects of type neither–null–nor–pair are compared using compare. Two pairs are compared by using compare on the cars, and if the cars are equal by recursing on the cdrs.

The binary form uses default-compare as compare.

(pair-compare '() 'foo)
⇒ -1

(pair-compare '() '(1 . 2)))
⇒ -1

(pair-compare '(1 . 2) 'foo)
⇒ -1

(pair-compare 3 4)
⇒ -1