Next: srfi compare-procedures spec using, Previous: srfi compare-procedures spec def, Up: srfi compare-procedures spec [Index]
An important goal of this SRFI is to provide a mechanism for defining new compare procedures as conveniently as possible. The syntactic extensions defined in this section are the primary utilities for doing so.
Syntax. The ?C1 are expressions.
Semantics: The arguments ?C1 ... are evaluated from left to right until a non–zero value is found (which then is the value) or until there are no more arguments to evaluate (in which case the value is 0). It is allowed that there are no arguments at all.
NOTE This macro is the preferred way to define a compare procedure as a refinement. Example:
(define (compare-rectangle r s) (refine-compare (compare-length (width r) (width s)) (compare-length (height r) (height s))))
Syntax. Each ?clause, with the possible exception of the last, is of the form:
(?type? ?C1 ...)
where ?type? is an expression evaluating to a predicate procedure,
and ?C are expressions evaluating to an exact integer in
{-1, 0, +1}
. The last ?clause may be an “else clause”,
which has the form:
(else ?C1 ...)
Semantics: select-compare
is a conditional for defining
hierarchical extensions and refinements of compare procedures. It
compares the values of ?X1 and ?X2 by trying the type tests
in order, and applies an implict refine-compare
on the
consequences upon a match.
In more detail, evaluation proceeds as follows:
(?type? ?C1 ...)
first ?type? is evaluated resulting in a predicate procedure
type? and then the expressions (type? X1)
and
(type? X2)
are evaluated and interpreted as
booleans.
(refine-compare ?C1 ...)
-1
, if only the second is
true the result is +1
, and if neither is true the next clause is
considered.
0
.
select-compare
evaluates ?X1 and ?X2 exactly once,
even in the absence of any clauses. Moreover, each ?type? is
evaluated at most once and the resulting procedure ?type? is
called at most twice.
NOTE An example of
select-compare
is the definition ofdefault-compare
given above.
Syntax. Each ?clause, with the possible exception of the last, is of the form:
((?T1 ?T2) ?C1 ...)
where ?T1 and ?T2 are expressions evaluating to booleans,
and ?C are expressions evaluating to an exact integer in
{-1, 0, +1}
. The last ?clause may be an “else clause”,
which has the form:
(else ?C1 ...)
Semantics: cond-compare
is another conditional for defining
hierarchical extensions and refinements of compare procedures.
Evaluation proceeds as follows:
((?T1 ?T2) ?C1 ...)
first ?T1 and ?T2 are evaluated and the results are interpreted as boolean values.
(refine-compare ?C1 ...)
-1
, if only the second is
true the result is +1
, and if neither is true the next clause is
considered.
0
.
cond-compare
evaluates each expression at most once.
RATIONALE
cond-compare
andselect-compare
only differ in the way the type tests are specified. Both ways are equivalent, and each way is sometimes more convenient than the other.
Next: srfi compare-procedures spec using, Previous: srfi compare-procedures spec def, Up: srfi compare-procedures spec [Index]