Next: srfi comparators default, Previous: srfi comparators predicates, Up: srfi comparators [Index]
The following comparators are analogous to the standard compare procedures of SRFI-67. They all provide appropriate hash functions as well.
Compare booleans using the total order #f < #t
.
(import (vicare) (srfi :114)) (define C boolean-comparator) (comparator-test-type C #f) ⇒ #t (comparator-test-type C #t) ⇒ #t (comparator-test-type C 123) ⇒ #f (comparator-check-type C #f) ⇒ #t (comparator-check-type C #t) ⇒ #t (try (comparator-check-type C (void)) (catch E ((&comparator-type-error) #t) (else #f))) ⇒ #t (comparator-equal? C #f #f) ⇒ #t (comparator-equal? C #f #t) ⇒ #f (comparator-compare C #f #f) ⇒ 0 (comparator-compare C #f #t) ⇒ -1 (comparator-compare C #t #f) ⇒ +1
Compare characters using the total order implied by char<?
. On
R6RS and R7RS systems, this is Unicode code point order.
(import (vicare) (srfi :114)) (define C char-comparator) (comparator-test-type C #\a) ⇒ #t (comparator-test-type C #\b) ⇒ #t (comparator-test-type C 123) ⇒ #f (comparator-check-type C #\a) ⇒ #t (comparator-check-type C #\b) ⇒ #t (try (comparator-check-type C (void)) (catch E ((&comparator-type-error) #t) (else #f))) ⇒ #t (comparator-equal? C #\a #\a) ⇒ #t (comparator-equal? C #\a #\b) ⇒ #f (comparator-compare C #\a #\a) ⇒ 0 (comparator-compare C #\a #\b) ⇒ -1 (comparator-compare C #\b #\a) ⇒ +1 (comparator-hash C #\a) ⇒ 97 (comparator-hash C #\b) ⇒ 98
Compare characters using the total order implied by char-ci<?
.
On R6RS and R7RS systems, this is Unicode code point order after
the characters have been folded to lower case.
(import (vicare) (srfi :114)) (define C char-ci-comparator) (comparator-test-type C #\A) ⇒ #t (comparator-test-type C #\b) ⇒ #t (comparator-test-type C 123) ⇒ #f (comparator-check-type C #\A) ⇒ #t (comparator-check-type C #\b) ⇒ #t (try (comparator-check-type C (void)) (catch E ((&comparator-type-error) #t) (else #f))) ⇒ #t (comparator-equal? C #\A #\A) ⇒ #t (comparator-equal? C #\A #\b) ⇒ #f (comparator-compare C #\A #\A) ⇒ 0 (comparator-compare C #\A #\b) ⇒ -1 (comparator-compare C #\b #\A) ⇒ +1 (comparator-hash C #\A) ⇒ 97 (comparator-hash C #\b) ⇒ 98
Compare strings using the total order implied by string<?
. Note
that this order is implementation–dependent.
(import (vicare) (srfi :114)) (define C string-comparator) (comparator-test-type C "a") ⇒ #t (comparator-test-type C "b") ⇒ #t (comparator-test-type C 123) ⇒ #f (comparator-check-type C "a") ⇒ #t (comparator-check-type C "b") ⇒ #t (try (comparator-check-type C (void)) (catch E ((&comparator-type-error) #t) (else #f))) ⇒ #t (comparator-equal? C "a" "a") ⇒ #t (comparator-equal? C "a" "b") ⇒ #f (comparator-compare C "a" "a") ⇒ 0 (comparator-compare C "a" "b") ⇒ -1 (comparator-compare C "b" "a") ⇒ +1 (comparator-hash C "a") ⇒ 1798195 (comparator-hash C "b") ⇒ 36222037
Compare strings using the total order implied by string-ci<?
.
Note that this order is implementation–dependent.
(import (vicare) (srfi :114)) (define C string-ci-comparator) (comparator-test-type C "A") ⇒ #t (comparator-test-type C "b") ⇒ #t (comparator-test-type C 123) ⇒ #f (comparator-check-type C "A") ⇒ #t (comparator-check-type C "b") ⇒ #t (try (comparator-check-type C (void)) (catch E ((&comparator-type-error) #t) (else #f))) ⇒ #t (comparator-equal? C "A" "A") ⇒ #t (comparator-equal? C "A" "b") ⇒ #f (comparator-compare C "A" "A") ⇒ 0 (comparator-compare C "A" "b") ⇒ -1 (comparator-compare C "b" "A") ⇒ +1 (comparator-hash C "A") ⇒ 1798195 (comparator-hash C "b") ⇒ 36222037
Compare symbols using the total order implied by applying
symbol->string
to the symbols and comparing them using the total
order implied by string<?
. It is not a requirement that the hash
function of symbol-comparator
be consistent with the hash
function of string-comparator
.
(import (vicare) (srfi :114)) (define C string-comparator) (comparator-test-type C 'a) ⇒ #t (comparator-test-type C 'b) ⇒ #t (comparator-test-type C 123) ⇒ #f (comparator-check-type C 'a) ⇒ #t (comparator-check-type C 'b) ⇒ #t (try (comparator-check-type C (void)) (catch E ((&comparator-type-error) #t) (else #f))) ⇒ #t (comparator-equal? C 'a 'a) ⇒ #t (comparator-equal? C 'a 'b) ⇒ #f (comparator-compare C 'a 'a) ⇒ 0 (comparator-compare C 'a 'b) ⇒ -1 (comparator-compare C 'b 'a) ⇒ +1 (comparator-hash C 'a) ⇒ 1798195 (comparator-hash C 'b) ⇒ 36222037
These comparators compare fixnums, exact integers, integers, rational
numbers, real numbers, complex numbers, and any numbers using the total
order implied by <
.
They must be compatible with the R5RS numerical tower in the
following sense: if S
is a subtype of the numerical type T
and the two objects are members of S
, then the equality predicate
and comparison procedures (but not necessarily the hash function) of
S-comparator
and T-comparator
compute the same results on
those objects.
Since non–real numbers cannot be compared with <
, the following
least–surprising ordering is defined: if the real parts are <
or
>
, so are the numbers; otherwise, the numbers are ordered by
their imaginary parts. This can still produce surprising results if one
real part is exact and the other is inexact.
fixnum-comparator
is a Vicare extension.
(import (vicare) (srfi :114)) (define C exact-integer-comparator) (comparator-test-type C 1) ⇒ #t (comparator-test-type C 2) ⇒ #t (comparator-test-type C 123) ⇒ #f (comparator-check-type C 1) ⇒ #t (comparator-check-type C 2) ⇒ #t (try (comparator-check-type C (void)) (catch E ((&comparator-type-error) #t) (else #f))) ⇒ #t (comparator-equal? C 1 1) ⇒ #t (comparator-equal? C 1 2) ⇒ #f (comparator-compare C 1 1) ⇒ 0 (comparator-compare C 1 2) ⇒ -1 (comparator-compare C 2 1) ⇒ +1 (comparator-hash C 1) ⇒ 1 (comparator-hash C 2) ⇒ 2
Compare pairs using default-comparator
on their cars.
If the cars are not equal, that value is returned. If they are equal,
default-comparator
is used on their cdrs and that value is
returned.
(import (vicare) (srfi :114)) (define C pair-comparator) (comparator-test-type C '(1 . 2)) ⇒ #t (comparator-test-type C '(3 . 4)) ⇒ #t (comparator-test-type C 123) ⇒ #f (comparator-check-type C '(1 . 2)) ⇒ #t (comparator-check-type C '(3 . 4)) ⇒ #t (try (comparator-check-type C (void)) (catch E ((&comparator-type-error) #t) (else #f))) ⇒ #t (comparator-equal? C '(1 . 2) '(1 . 2)) ⇒ #t (comparator-equal? C '(1 . 2) '(3 . 4)) ⇒ #f (comparator-compare C '(1 . 2) '(1 . 2)) ⇒ 0 (comparator-compare C '(1 . 2) '(3 . 4)) ⇒ -1 (comparator-compare C '(3 . 4) '(1 . 2)) ⇒ +1 (comparator-hash C '(1 . 2)) ⇒ 3 (comparator-hash C '(3 . 4)) ⇒ 7
Compare lists lexicographically, as follows:
default-comparator
,
then the result is the result of that comparison. Otherwise, the cdrs
are compared using list-comparator
.
(import (vicare) (srfi :114)) (define C list-comparator) (comparator-test-type C '(1 2)) ⇒ #t (comparator-test-type C '(3 4)) ⇒ #t (comparator-test-type C 123) ⇒ #f (comparator-check-type C '(1 2)) ⇒ #t (comparator-check-type C '(3 4)) ⇒ #t (try (comparator-check-type C (void)) (catch E ((&comparator-type-error) #t) (else #f))) ⇒ #t (comparator-equal? C '(1 2) '(1 2)) ⇒ #t (comparator-equal? C '(1 2) '(3 4)) ⇒ #f (comparator-compare C '(1 2) '(1 2)) ⇒ 0 (comparator-compare C '(1 2) '(3 4)) ⇒ -1 (comparator-compare C '(3 4) '(1 2)) ⇒ +1 (comparator-hash C '(1 2)) ⇒ 617064 (comparator-hash C '(3 4)) ⇒ 617132
Compare vectors and bytevectors by comparing their lengths. A shorter
argument is always less than a longer one. If the lengths are equal,
then each element is compared in turn using default-comparator
until a pair of unequal elements is found, in which case the result is
the result of that comparison. If all elements are equal, the arguments
are equal.
NOTE Vicare supports bytevectors, other Scheme implementations might not support them. If the implementation does not support bytevectors,
bytevector-comparator
has a type testing procedure that always returns#f
.
(import (vicare) (srfi :114)) (define C vector-comparator) (comparator-test-type C '#(1 2)) ⇒ #t (comparator-test-type C '#(3 4)) ⇒ #t (comparator-test-type C 123) ⇒ #f (comparator-check-type C '#(1 2)) ⇒ #t (comparator-check-type C '#(3 4)) ⇒ #t (try (comparator-check-type C (void)) (catch E ((&comparator-type-error) #t) (else #f))) ⇒ #t (comparator-equal? C '#(1 2) '#(1 2)) ⇒ #t (comparator-equal? C '#(1 2) '#(3 4)) ⇒ #f (comparator-compare C '#(1 2) '#(1 2)) ⇒ 0 (comparator-compare C '#(1 2) '#(3 4)) ⇒ -1 (comparator-compare C '#(3 4) '#(1 2)) ⇒ +1 (comparator-hash C '#(1 2)) ⇒ 177575 (comparator-hash C '#(3 4)) ⇒ 177577
Next: srfi comparators default, Previous: srfi comparators predicates, Up: srfi comparators [Index]