Next: srfi compare-procedures refs, Previous: srfi compare-procedures design, Up: srfi compare-procedures [Index]
The use of compare procedures is not new; defining control structures
(if3
, select-compare
etc.) for dealing with them
efficiently, however, seems to be new (at least we have not seen it
before).
Total ordering in R5RS is represented by typed order predicates,
such as <=
, char<=?
etc. Although a “less or
equal”-predicate is sufficient to define a total order, R5RS
defines a complete set of compare predicates (that is =
,
<
, >
, <
, and <) for the sake of convenience and
readability. There are 25 procedures related to total orders in
R5RS.
The traditional approach in Scheme to equivalence (“Are two values
treated as equal?”) is the fixed set of predicates eq?
,
eqv?
, and equal?
. Historically, this approach was
motivated by the desire to compare only pointers and avoid structural
recursion. This SRFI provides the generalization to arbitrary
equivalence relations, provided the equivalence classes are totally
ordered.
The Ruby programming language [4] provides a method <=>
which is
a compare procedure in the sense of this SRFI. By (re-)defining this
method a total order can be defined for the instances of a class, when
compared against other objects. All 2-way comparisons are based on
<=>
, but in Ruby essentially every method can be overloaded.
In the Haskell 98 programming language [6] order predicates and compare
functions coexist. The type Ordering [6, Sect 6.1.8] is an enumeration
of the three symbolic constants LT
, EQ
, GT
. The
type class Ord
[6, Sect 6.3.2] asserts the presence of a total
order for a type, provided the type class Eq
[6, Sect 6.3.1] also
asserts the presence of an equivalence. Since the default definition of
the method compare is in terms of the methods ==
and <=
,
and vice versa, it can be chosen easily how to provide the total order
without affecting its pattern of use.
The C function strcmp()
[7] of the string.h–library acts
as a compare procedure in the sense of this SRFI, although it is
specified to return an integer of which only the sign matters. Python
[5] has a built in function cmp
which is a compare procedure in
the sense of this SRFI.
In SRFI-32 (Sort libraries) [13] the total orders used for sorting are represented by a “less than” procedure. The discussion archive [13] contains a short discussion thread on the use of 3-value comparisons under the aspect whether they can be used to improve the sorting algorithm itself.
In the Galore.plt
library of data structures for PLT Scheme,
total orders are represented by the signature definition
(define-signature order^ (elm= elm< elm<=))
.
Next: srfi compare-procedures refs, Previous: srfi compare-procedures design, Up: srfi compare-procedures [Index]