Next: comparisons default, Previous: comparisons sequence, Up: comparisons [Index]
This section describes comparison procedures for Scheme pairs and (possibly) improper lists.
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)
.
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