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


2.28.5.3 Comparing pairs and improper lists

In this section, compare procedures for Scheme pairs and (possibly) improper lists are defined.

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

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

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

RATIONALE 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 obj1 obj2
Function: pair-compare compare obj1 obj2

Compare two pairs, or (possibly improper) lists.

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

The 3-ary 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 2-ary form uses default-compare for compare.

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