Next: , Up: comparisons   [Index]


1.17.1 Introduction

The (vicare language-extensions comparisons) library defines a mechanism for comparing Scheme values with respect to a total order (aka linear order). The mechanism provides operations for:

  1. Comparing objects of the built-in types.
  2. Using a total order in situations that arise in programs.
  3. Facilitating the definition of a new total order.

Traditionally, a total order is represented in Scheme by an order predicate, like < or char<?. In the context of (vicare language-extensions comparisons), however, a total order is represented by a Scheme procedure comparing its two arguments and returning either -1, 0, or 1 depending on whether the first argument is considered smaller, equal, or greater than the second argument respectively. Examples of such compare procedures include:

(lambda (x y)
  (sign (- x y)))

for comparing real numbers, but also:

(lambda (x y) 0)

comparing anything.

The primary reason for using 3–valued compare procedures (instead of 2–valued order predicates) is efficiency: When comparison is computationally expensive, it is wasteful if two predicates are evaluated where a single 3–valued comparison would suffice.

But dealing directly with 3–valued comparisons in the application program is inconvenient and obscures intention: For testing x < y one would have to write:

(= (compare x y) -1)

for this reason, an operation <? is supplied which allows to phrase the same test as:

(<? compare x y)

This is an example of mapping the three possible outcomes of a comparison into the two boolean values #t and #f. Since <? takes the total order as an explicit parameter, a comfortably large arsenal of tests can be made available for each and every total order. This deviates from the approach of R6RS, in which there are only five operations (=, <, >, <=, >=) and for each total order (real/number, char, char–ci, string, string–ci) a complete set of these five operations is provided.

But still, using <? would be inconvenient if the compare procedure would have to be supplied explicitly every time. For this reason, the argument compare is often made optional and the procedure default-compare is used whenever no compare procedure is passed explicitly. default-compare defines some resonable total order on the builtin types of R6RS.

For the definition of comparison procedures, special control structures (macros) are provided. These control structures can be used in the definition of a (potentially recursive) comparison procedure.


Next: , Up: comparisons   [Index]