Next: , Previous: , Up: srfi comparators   [Index]


2.38.7 The default comparator

Function: default-comparator

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:

Function: comparator-register-default! comparator

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: , Previous: , Up: srfi comparators   [Index]