Next: comparisons minmax, Previous: comparisons branch, Up: comparisons [Index]
If the values x and y are given, test if x and y
are in the relation specified by the name of the procedure rel?
,
with respect to compare procedure compare; otherwise construct a
predicate procedure.
In the forms:
(rel? [compare] x y)
the result is a boolean depending on (compare x
y)
and the test rel?
as specified for if<?
etc. If
compare is not supplied, default-compare
is used.
In the form:
(rel? [compare])
the predicate procedure:
(lambda (x y) (rel? compare x y))
is constructed. Again, if compare is not supplied,
default-compare
is used.
A few examples for illustration:
(>? "laugh" "LOUD") ⇒ #t (<? string-compare-ci "laugh" "LOUD") ⇒ #t (define char<=? (<=? char-compare)) (sort-by-less '(1 a "b") (<?)) ⇒ '("b" a 1) (sort-by-less '(1 a "b") (>?)) ⇒ '(1 a "b")
Warning: A common mistake is writing
(<=? x y z)
where(<=/<=? x y z)
is meant; this will most likely manifest itself at the time the expression(x y z)
is evaluated.
Test if x, y, and z form a chain with the two
relations specified by the name of the procedure rel1/rel2?
, with
respect to the compare procedure compare.
If compare is not provided, default-compare
is used.
If x, y and z are not provided, a predicate procedure of three arguments is constructed. The order in which the values are compared is unspecified, but each value is compared at least once.
NOTE
(<=/<? real-compare 0 x 1)tests if x is a real number in the half open interval
[0, 1)
.
Test if the values x1 … (zero or more values) form a chain with respect to the relation specified by the name of the procedure, and with respect to the compare procedure compare. The result is a boolean.
The order in which the values are compared is unspecified, but each value is compared at least once (even if there is just one).
A sequence of values x1, …, xn forms a chain with
respect to the relation rel?
if:
(rel? compare xi xj)
for all 1 < i < j < n. In particular, this is the case for n \in {0, 1}.
Since the relations =
, <
, >
, <
, and >
are transitive, it is sufficient to test:
(rel? compare xi xi+1)
for 1 < i < n.
NOTE The reason every xi participates in at least one comparison is type–checking: After testing if the values form a chain, these value may be assumed to be of the type comparable by compare— and this holds irrespectively of the number of values, or whether they form a chain.
Tests if the values x1 ... (zero or more values) are pairwise unequal with respect to the compare procedure compare. The result is a boolean.
The order in which the values are compared is unspecified, but each value is compared at least once (even if there is just one).
The values x1, ..., xn are pairwise unequal if:
(not=? compare xi xj)
for all i different from j. In particular, this is the case for n \in {0, 1}.
Since compare defines a total ordering on the values, the property can be checked in time O(n log n).
If optional arguments x and y are present then these are compared with respect to the total order defined by the predicate(s) given; the result is in {-1, 0, 1}. If x and y are not present then a procedure comparing its two arguments using the predicate(s) given is constructed and returned.
The predicate procedures mean the following:
Test if x < y.
Test for <=.
Test for >.
Test for >=.
Test if x and y are equivalent.
The result returned by a predicate procedure is interpreted as a Scheme
truth value (i.e. #f
is false and non–#f
is true).
The purpose of the procedures compare-by-predicate(s) is to define a compare procedure from an order predicate, and possibly an additional equivalence predicate. If an equivalence predicate eq-pred is given, it is called before the order predicate because the equivalence may be coarser than the total ordering, and it may also be cheaper.
NOTE
char-compare
could be defined in terms ofchar<=?
as:(define char-compare (compare-by<= char<=?))
Next: comparisons minmax, Previous: comparisons branch, Up: comparisons [Index]