Next: srfi comparators constructors, Previous: srfi comparators standard, Up: srfi comparators [Index]
This is a comparator that accepts any two Scheme values (with the exceptions listed in the “Limitations” section, srfi comparators limitations) and orders them in some implementation–defined way, subject to the following conditions:
(void)
object (this is a Vicare
extension),
(eof-object)
object (this is a Vicare
extension),
(would-block-object)
object (this is a
Vicare extension),
this ordering is compatible with SRFI-67.
pair-comparator
, boolean-comparator
,
character-comparator
, string-comparator
,
symbol-comparator
, number-comparator
,
vector-comparator
, and bytevector-comparator
respectively.
The same should be true when applied to an object or objects of a type
for which a standard comparator is defined elsewhere.
A
and B
, one of three conditions must
hold:
A
compare less than all objects of type
B
.
A
compare greater than all objects of type
B
.
A
or type B
compare equal to
each other. This is not permitted for any of the standard types
mentioned above.
Register a new comparator for use by the default comparator.
Extent the default comparator to understand objects that satisfy the
type test of the registered comparator, and to apply it when
comparing two objects that satisfy that type test. Such objects are
said to belong to the “registered types”, whereas all the objects
other than the ones supported by default belong to the “unregistered
types”. When dealing with objects of unregistered types, the default
comparator makes them compare equal and hash to 0
, a safe but
fairly useless default.
The effect of comparing two objects of different registered types is
only partly predictable, as it depends on the order in which
comparator-register-default!
is called. However, the normal
guarantees of the default comparator still apply. In addition, every
object of a registered type compares less than all objects of
unregistered types.
Here is an example of registering comparators for Vicare
structs and R6RS records (notice that struct-hash
and
record-hash
are very poor hash functions):
(import (vicare) (srfi :114)) (define C default-comparator) (define-struct a-struct (a b c)) (define-record-type a-record (fields a b c)) (define (a-struct-comparison a b) (cond ((struct=? a b) 0) ((< (a-struct-a a) (a-struct-a b)) -1) (else +1))) (define (a-record-comparison a b) (cond ((record=? a b) 0) ((< (a-record-a a) (a-record-a b)) -1) (else +1))) (define a-struct-comparator (make-comparator a-struct? struct=? a-struct-comparison struct-hash)) (define a-record-comparator (make-comparator a-record? record=? a-record-comparison record-hash)) (comparator-register-default! a-struct-comparator) (comparator-register-default! a-record-comparator) ;;; type test (comparator-test-type C (make-a-struct 1 2 3)) ⇒ #t (comparator-test-type C (make-a-record 1 2 3)) ⇒ #t ;;; type check (comparator-check-type C (make-a-struct 1 2 3)) ⇒ #t (comparator-check-type C (make-a-record 1 2 3)) ⇒ #t ;;; equality (let ((P (make-a-struct 1 2 3)) (Q (make-a-struct 1 2 3)) (R (make-a-struct 1 2 9))) (comparator-equal? C P P) ⇒ #t (comparator-equal? C P Q) ⇒ #t (comparator-equal? C P R)) ⇒ #f (let ((P (make-a-record 1 2 3)) (Q (make-a-record 1 2 3)) (R (make-a-record 1 2 9))) (comparator-equal? C P P) ⇒ #t (comparator-equal? C P Q) ⇒ #t (comparator-equal? C P R)) ⇒ #f ;;; comparison (let ((P (make-a-struct +1 2 3)) (Q (make-a-struct -1 2 3)) (R (make-a-struct +2 2 9))) (comparator-compare C P P) ⇒ 0 (comparator-compare C P Q) ⇒ +1 (comparator-compare C P R)) ⇒ -1 (let ((P (make-a-record +1 2 3)) (Q (make-a-record -1 2 3)) (R (make-a-record +2 2 9))) (comparator-compare C P P) ⇒ 0 (comparator-compare C P Q) ⇒ +1 (comparator-compare C P R)) ⇒ -1 ;;; hash function (comparator-hash C (make-a-struct 1 2 3)) ⇒ 1 (comparator-hash C (make-a-record 1 2 3)) ⇒ 1
Next: srfi comparators constructors, Previous: srfi comparators standard, Up: srfi comparators [Index]