# SRFI 128 - Comparators The `(srfi 128)` provides comparators, which bundle a type test predicate, an equality predicate, an ordering predicate, and a hash function into a single Scheme object. By packaging these procedures together, they can be treated as a single item for use in the implementation of data structures. See the [SRFI document](http://srfi.schemers.org/srfi-128/srfi-128.html) for more information. ## Predicates - [`comparator? `](#comparator) - [`comparator-ordered? `](#comparator-ordered) - [`comparator-hashable? `](#comparator-hashable) ## Constructors The following comparator constructors all supply appropriate type test predicates, equality predicates, ordering predicates, and hash functions based on the supplied arguments. They are allowed to cache their results: they need not return a newly allocated object, since comparators are pure and functional. In addition, the procedures in a comparator are likewise pure and functional. - [`make-comparator `](#make-comparator) - [`make-pair-comparator `](#make-pair-comparator) - [`make-list-comparator `](#make-list-comparator) - [`make-vector-comparator `](#make-vector-comparator) - [`make-eq-comparator `](#make-eq-comparator) - [`make-eqv-comparator `](#make-eqv-comparator) - [`make-equal-comparator `](#make-equal-comparator) ## Standard Hash Functions These are hash functions for some standard Scheme types, suitable for passing to make-comparator. Users may write their own hash functions with the same signature. However, if programmers wish their hash functions to be backward compatible with the reference implementation of SRFI 69, they are advised to write their hash functions to accept a second argument and ignore it. - [`boolean-hash `](#boolean-hash) - [`char-hash `](#char-hash) - [`char-ci-hash `](#char-ci-hash) - [`string-hash `](#string-hash) - [`string-ci-hash `](#string-ci-hash) - [`symbol-hash `](#symbol-hash) - [`number-hash `](#number-hash) ## Default Comparators - [`make-default-comparator `](#make-default-comparator) - [`default-hash `](#default-hash) - [`comparator-register-default! `](#comparator-register-default) ## Accessors and Invokers - [`comparator-type-test-predicate`](#comparator-type-test-predicate) - [`comparator-equality-predicate `](#comparator-equality-predicate) - [`comparator-ordering-predicate `](#comparator-ordering-predicate) - [`comparator-hash-function `](#comparator-hash-function) - [`comparator-test-type `](#comparator-test-type) - [`comparator-check-type `](#comparator-check-type) - [`comparator-hash `](#comparator-hash) ## Bounds and Salt The following macros allow the callers of hash functions to affect their behavior without interfering with the calling signature of a hash function, which accepts a single argument (the object to be hashed) and returns its hash value. They are provided as macros so that they may be implemented in different ways: as a global variable, a SRFI 39 or R7RS parameter, or an ordinary procedure, whatever is most efficient in a particular implementation. - [`hash-bound `](#hash-bound) - [`hash-salt `](#hash-salt) ## Comparison Predicates These procedures are analogous to the number, character, and string comparison predicates of Scheme. They allow the convenient use of comparators to handle variable data types. These procedures apply the equality and ordering predicates of comparator to the objects as follows. If the specified relation returns `#t` for all `objecti` and `objectj` where `n` is the number of objects and `1 <= i < j <= n`, then the procedures return `#t`, but otherwise `#f`. Because the relations are transitive, it suffices to compare each object with its successor. The order in which the values are compared is unspecified. - [`=? `](#) - [`? `](#-2) - [`<=? `](#-3) - [`>=? `](#-4) ## Syntax - [`comparator-if<=> `](#comparator-if) # comparator? (comparator? obj) Returns `#t` if `obj` is a comparator, and `#f` otherwise. # comparator-ordered? (comparator-ordered? comparator) Returns `#t` if `comparator` has a supplied ordering predicate, and `#f` otherwise. # comparator-hashable? (comparator-hashable? comparator) Returns `#t` if `comparator` has a supplied hash function, and `#f` otherwise. # make-comparator (make-comparator type-test equality ordering hash) Returns a comparator which bundles the `type-test`, `equality`, `ordering`, and `hash` procedures provided. However, if `ordering` or `hash` is `#f`, a procedure is provided that signals an error on application. The predicates `comparator-ordered?` and/or `comparator-hashable?`, respectively, will return `#f` in these cases. Here are calls on `make-comparator` that will return useful comparators for standard Scheme types: * `(make-comparator boolean? boolean=? (lambda (x y) (and (not x) y)) boolean-hash)` will return a comparator for booleans, expressing the ordering `#f < #t` and the standard hash function for booleans. * `(make-comparator real? = < (lambda (x) (exact (abs x))))` will return a comparator expressing the natural ordering of real numbers and a plausible (but not optimal) hash function. * `(make-comparator string? string=? stringstring` to the symbols and comparing them using the total order implied by `string,` so are the numbers; otherwise, the numbers are ordered by their imaginary parts. This can still produce somewhat surprising results if one real part is exact and the other is inexact. * When comparing real numbers, it must use `=` and `<.` * When comparing strings, it must use `string=?` and `string? (>? comparator object1 object2 object3 ...) # <=? (<=? comparator object1 object2 object3 ...) # >=? (>=? comparator object1 object2 object3 ...) # comparator-if<=> *Syntax* (comparator-if<=> [ ] ) It is an error unless `` evaluates to a comparator and `` and `` evaluate to objects that the comparator can handle. If the ordering predicate returns true when applied to the values of `` and `` in that order, then `` is evaluated and its value returned. If the equality predicate returns true when applied in the same way, then `` is evaluated and its value returned. If neither returns true, `` is evaluated and its value returned. If `` is omitted, a default comparator is used.