Next: srfi compare-procedures spec def, Previous: srfi compare-procedures spec seq, Up: srfi compare-procedures spec [Index]
In this section, compare procedures for Scheme pairs and (possibly) improper lists are defined.
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)
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