srfi.114
- 比較器 ¶このモジュールはSRFI-114を使っているコードとの互換性のために提供されています。 新規コードはSRFI-128を使ってください。そちらは組み込みになっています。
SRFI-114のmake-comparator
は組み込み(SRFI-128)のmake-comparator
と異なるインタフェースを持つことに注意してください。
単に(use srfi.114)
や(import (srfi 114))
とすると、
組み込みのmake-comparator
がシャドウされます。
SRFI-114のいくつかの手続きだけを使いたいなら、それらを選択的にインポートするのが良いでしょう。
SRFI-114手続きで作られる比較器は、SRFI-128で作られる比較器と同じ型で、 互いにどちらの手続きにも渡すことができます。
以下の手続きは組み込みで提供されます。
説明は基本的な比較器を参照してください。
互換性のために、これらはsrfi.114
モジュールからもエクスポートされます。
comparator?
,
boolean-comparator
,
char-comparator
,
char-ci-comparator
,
string-comparator
,
string-ci-comparator
,
symbol-comparator
,
exact-integer-comparator
,
integer-comparator
,
rational-comparator
,
real-comparator
,
complex-comparator
,
number-comparator
,
pair-comparator
,
list-comparator
,
vector-comparator
,
bytevector-comparator
,
uvector-comparator
default-comparator
eq-comparator
,
eqv-comparator
,
equal-comparator
comparator-equality-predicate
,
comparator-comparison-procedure
,
comparator-hash-function
comparator-test-type
,
comparator-check-type
,
comparator-compare
,
comparator-hash
=?
,
<?
,
<=?
,
>?
,
>=?
[SRFI-114+]{srfi.114
}
SRFI-114形式の比較器コンストラクタです。省略可能引数nameはGaucheの拡張です。
この手続きは組み込みのmake-comparator/compare
と同じです。
詳しくは基本的な比較器を参照してください。
組み込み (SRFI-128) のmake-comparator
と混同しないようにしてください。
単に(use srfi.114)
とすると、組み込みのそれがSRFI-114でシャドウされます。
比較器はどちらで作られたかにかかわらず、SRFI-114の手続きにもSRFI-128の手続きにも 渡すことができます。
[SRFI-114]{srfi.114
}
Returns true iff a comparator c can be used to order objects
or to hash them, respectively. These are aliases of
built-in comparator-ordered?
and comparator-hashable?
.
[SRFI-114]{srfi.114
}
Returns type test predicate of a comparator c.
This is an alias of built-in comparator-type-test-predicate
.
[SRFI-114]{srfi.114
}
Checks equality of a and b using the equality predicate
of a comparator c. This can be also written in =?
, which
is built-in (see 比較器にまつわる述語とアクセサ).
(=? c a b)
[SRFI-114]{srfi.114
}
Returns a comparator for inexact real numbers, taking into
account of errors and NaNs.
The basic idea is that we compare two finite real numbers after rounding them to epsilon interval, which must be a nonnegative real number. (Note that it’s not to compare two numbers “close enough”, as often being done to compare inexact numbers. “Close enough” scheme won’t be transitive.)
The rounding mode is specified by the rounding argument.
It can be either one of the symbols round
, ceiling
,
floor
or truncate
, or a procedure that takes
two arguments, a real number and an epsilon, and returns
the rounded result of the first argument according to the
given epsilon.
The nan-handling argument determines how to handle the case to compare NaN and non-NaN numbers. (If both are NaNs, this comparator regards them as equal). It can be either one of the followings:
min
If it’s a symbol min
, NaN is compared as smaller than
all other real numbers, even than -inf.0
.
max
If it’s a symbol min
, NaN is compared as greater than
all other real numbers, even than +inf.0
.
error
If it’s a symbol error
, an error is signaled.
The procedure is invoked with the real number which is not NaN. If it ever returns, it must return either 1, 0 or -1, for it’s used as the result of the comparison procedure of the comparator. However, since the procedure doesn’t know which argument is non-NaN, it’s hard to have consistent semantics; the best bet is to throw a custom error.
(define c (make-inexact-real-comparator 0.1 'round 'error)) (comparator-compare c 0.112 0.098) ⇒ 0 (comparator-compare c 0.131 0.172) ⇒ -1
Note: Rounding to the nearest epsilon interval would involve
scaling inexact numbers, and that may reveal small difference
between the actual number and its notation. For example,
an inexact real number denoted as 0.15
is actually
slightly smaller than 15/100
, and rounding with epsilon
0.1
would result 0.1
, not 0.2
.
[SRFI-114]{srfi.114
}
ペアを受け取り、そのcar
のみまたはcdr
のみをcmprで
比較するような比較器を返します。
make-key-comparator
を使えばこれらは以下の通りに書けます。
(make-key-comparator
については比較器を組み合わせるを参照)。
(define (make-car-comparator cmpr) (make-key-comparator cmpr pair? car)) (define (make-cdr-comparator cmpr) (make-key-comparator cmpr pair? cdr))
[SRFI-114]{srfi.114
}
Returns a new comparator that compares lists, vectors and
bytevectors element-wise using element-comparator,
respectively. These are more general versions of
list-comparator
, vector-comparator
and
bytevector-comparator
, which use default-comparator
as element-comparator.
For a list comparator, it is an error to pass improper lists.
Note that comparing sequences of different lengths is slightly
different between lists and vector/bytevectors. List comparator
uses “dictionary” order, so (1 3)
comes after (1 2 3)
,
assuming elements are compared numerically.
For vectors and bytevectors, shorter one always precedes the other,
so #(1 3)
comes before #(1 2 3)
.
[SRFI-114]{srfi.114
}
More general version of make-list-comparator
.
Returns a comparator that compares structures which can be
traversed using three procedures, empty?, head and tail.
Each of those procedure receives a structure to be compared, and empty?
must return #t
iff the structure is empty, head
must return the first element in the structure, and tail
must return the same type of structure containing all the elements
but the head. The type-test predicate checks if the arguments
passed to the comparator to be a suitable structure.
That is, make-list-comparator
can be written in
make-listwise-comparator
as follows.
(make-list-compartator element-comparator) ≡ (make-listwise-comparator list? element-comparator null? car cdr)
This can be used to compare list-like structures. For example,
the following call returns a comparator that compares elements
of two lazy streams (see util.stream
- ストリームライブラリ).
(make-listwise-comparator stream? element-comparator stream-null? stream-car stream-cdr)
[SRFI-114]{srfi.114
}
More general version of make-vector-comparator
.
Returns a comparator that compares structures which can be
traversed using two procedures, length and ref.
The length procedure must return the number of elements in the
structure. The ref procedure receives a structure and a nonnegative
exact integer index k, and must return k-th element
of the structure.
That is, the following equivalence holds:
(make-vector-comparator element-comparator) ≡ (make-vectorwise-comparator vector? element-comparator vector-length vector-ref) (make-bytevector-comparator element-comparator) ≡ (make-vectorwise-comparator u8vector? element-comparator u8vector-length u8vector-ref)
[SRFI-114]{srfi.114
}
Creates a comparator that compares pairs, with their cars
by car-comparator and their cdrs by cdr-comparator.
[SRFI-114]{srfi.114
}
This may be understood as recursive pair comparator; if objects
to be compared are pairs, we recurse their cars then their cdrs.
If objects to be compared are not pairs, we use element-comparator
to compare them.
[SRFI-114]{srfi.114
}
This creates a comparator that works any one of the given comparators;
the objects to be compared are type-tested with each of the comparators
in order, and the first comparator that accepts all objects will be used.
[SRFI-114]{srfi.114
}
This is similar to make-selecting-comparator
, except that
if the first comparator that accepts given objects to compare finds
they are equal (or 0 by the comparison procedure), it tries other
comparators down the list, if any.
[SRFI-114]{srfi.114
}
Returns a comparator that just reverses the comparison order of
comparator.
[SRFI-114]{srfi.114
}
[SRFI-114]{srfi.114
}
Utility procedures to create a comparison procedure (the one returns
-1, 0, or 1) from the given predicate. For example,
make-comparison<
can be defined as follows:
(define (make-comparison< pred) (^[a b] (cond [(pred a b) -1] [(pred b a) 1] [else 0])))
[SRFI-114]{srfi.114
}
Three-way if
: Evaluates expr, and then evaluates
either one of less, equal, or greater,
depending on the value of expr is either less than zero,
equal to zero, or greater than zero, respectively.
[SRFI-114]{srfi.114
}
Conditional evaluation according to comparison expression expr;
that is, ifOP?
evaluates consequent if
(OP expr 0)
is true, otherwise it evaluates alternate
when provided.
(if<? (compare 10 20) 'yes) ⇒ yes (if>=? (compare 10 20) 'yes 'no) ⇒ no
[SRFI-114]{srfi.114
}
((make=? comparator) obj1 obj2 obj3 ...) ≡ (=? comparator obj1 obj2 obj3 ...)
[SRFI-114]{srfi.114
}
Check if obj1, obj2 and obj3 has the following
relationships:
(and (op1 obj1 obj2) (op2 obj2 obj3))
Where each of op1 and op2 can be
(make<? comparator)
(if that end is open),
or (make<=? comparator)
(if that end is closed).
When comparator is omitted, the default comparator is used.
(use srfi.42) (list-ec (: x 0 5) (list x (in-closed-open-interval? 1 x 3))) ⇒ ((0 #f) (1 #t) (2 #t) (3 #f) (4 #f))
comparator-min
and comparator-max
are the
same as srfi.162
. See srfi.162
- 比較器のサブライブラリ.