chickadee » srfi-228 » make-sum-comparator

make-sum-comparator comparator ...procedure

Returns a comparator which compares values satisfying the type-tests of any of the given comparators, such that values which satisfy the type-test of a given comparator are ordered before any values satisfying the type-tests of any comparators appear to the right of it, and values satisfying the same comparator’s type-test are tested for ordering and equality according that comparator.

The sum comparator is formally defined as follows. The domain of the sum comparator is the union of the domains of all the given comparators. The relevant comparator for a given value is the leftmost of the given comparators whose type test answers true for that value. The sum comparator is then a comparator whose

  • type test returns #t if there exists a relevant comparator for the given value, or otherwise #f;
  • equality predicate finds the relevant comparators for both of the values it is given. If the values have two different relevant comparators, it returns #f; otherwise, it returns the value of the equality predicate of the relevant comparator applied to the two values.
  • ordering predicate finds the relevant comparators for both of the values (a b) it is given. If the relevant comparator for a is to the left of the relevant comparator for b, it returns #t. If the two values have the same relevant comparator, it returns the value returned by the ordering predicate of that comparator applied to the two values. In all other cases, returns #f.
  • hash function returns the value returned by the hash function of the relevant comparator for the value it is given.

If the ordering predicate or the hash function of any of the given comparators does not exist, the corresponding procedure in the sum comparator will also not exist.

If there are no comparators given, this procedure returns the comparator-zero, or a new comparator with identical behaviour to it.