Next: , Previous: , Up: comparisons   [Index]


1.17.8 Constructing compare procedures

The syntactic extensions defined in this section are the primary utilities for defining new compare procedures as conveniently as possible.

Syntax: refine-compare expr ...

The arguments, if any, must be expressions; they 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’). If no arguments are supplied, the return value is ‘0’.

Syntax: select-compare expr1 expr2 clause ...

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 ?ci 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 ...)

select-compare is a conditional for defining hierarchical extensions and refinements of compare procedures. It compares the values of expr1 and expr2 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: First expr1 and expr2 are evaluated in unspecified order, resulting in values x1 and x2, respectively. Then the clauses are evaluated one by one, from left to right.

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:

  1. If both booleans are true then the overall value is:
    (refine-compare ?c1 ...)
    
  2. If only the first is true the result is ‘-1’.
  3. If only the second is true the result is ‘+1’.
  4. If neither is true the next clause is considered.

An else clause is treated as if both tests where true. If there are no clauses left, the result is ‘0’.

select-compare evaluates expr1 and expr2 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.

Syntax: cond-compare ?clause ...

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 ?ci 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 ...)

cond-compare is another conditional for defining hierarchical extensions and refinements of compare procedures.

Evaluation proceeds as follows: The clauses are evaluated one by one, from left to right. For clause:

((?t1 ?t2) ?c1 ...)

first ?t1 and ?t2 are evaluated and the results are interpreted as boolean values:

  1. If both booleans are true then the overall value is:
    (refine-compare ?c1 ...)
    
  2. If only the first is true the result is ‘-1’.
  3. If only the second is true the result is ‘+1’.
  4. If neither is true the next clause is considered.

An else clause is treated as if both booleans where true. 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.


Next: , Previous: , Up: comparisons   [Index]