Next: , Previous: , Up: srfi compare-procedures spec   [Index]


2.28.5.5 Constructing compare procedures

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: refine-compare ?C1 ...

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: select-compare ?X1 ?X2 ?clause1 ...

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:

  1. First ?x1 and ?x2 are evaluated in unspecified order, resulting in values X1 and X2, respectively. Then the clauses are evaluated one by one, from left to right.
  2. For clause:
    (?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.

  3. If both booleans are true then the overall value is:
    (refine-compare ?C1 ...)
    
  4. If only the first is true the result is -1, if only the second is true the result is +1, and if neither is true the next clause is considered.
  5. An else clause is treated as if both tests where true.
  6. If there are no clauses left, the result is 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 of default-compare given above.

Syntax: cond-compare ?clause1 ...

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:

  1. The clauses are evaluated one by one, from left to right.
  2. For clause:
    ((?T1 ?T2) ?C1 ...)
    

    first ?T1 and ?T2 are evaluated and the results are interpreted as boolean values.

  3. If both booleans are true then the overall value is:
    (refine-compare ?C1 ...)
    
  4. If only the first is true the result is -1, if only the second is true the result is +1, and if neither is true the next clause is considered.
  5. An else clause is treated as if both booleans where true.
  6. If there are no clauses left (or there are no clauses to begin with), the result is 0.

cond-compare evaluates each expression at most once.

RATIONALE cond-compare and select-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: , Previous: , Up: srfi compare-procedures spec   [Index]