On Thu, 23 Oct 2008, Ray Dillinger wrote:
>
> Just a note....
>
> Comparison procedures returning #f (or throwing exceptions) for
> single and zero-arity cases would cause errors in code I've written
> that sorts directories of files into order based on their timestamps.
>
> The basic logic is, "while unsorted, do this." If one-file and
> zero-file directories are never considered to be sorted, then the
> code goes all infinite-loopey.
>
> I've assumed that the predicates are true *unless* there is a pair
> in sequence that violates the condition, because that seems to me
> the obvious and only reasonable way for them to be defined. My
> personal principle of least surprise is such that I've been
> utterly amazed to hear people seriously arguing for different
> views.
>
sorting (ordering) and comparison are separate concepts. a list is ordered if
every pair of elements is #t under some comparison operator. a list is also
ordered in the trivial case (no elements can be compared due to no pairs
existing). previous post notwithstanding (which shows a recurrence with a
one-element base case), by definition comparison can only be performed on two
(or more) items.
methinks much of the confusion on this subject may be due to the (incorrect)
intuition of the equality of the concepts of ordering and comparison.
(< a b c d) can be viewed as either:
a) an ordering of the elements a b c d
b) a sequence of comparisons between a and b, b and c, c and d
if < is viewed as equivalent to 'ordered?', then a minimum arity of 0 makes
sense.
;; ordered less-than
(define (less-than? . rest)
(if (null? rest)
#t
(if (null? (cdr rest))
(if (number? (car rest))
#t
(error <not a number>))
(and (< (car rest) (cadr rest))
(apply less-than? (cdr rest))))))
if < is viewed as a comparison predicate, then a minimum arity of 2 makes
sense (although allowing 1 as a base case, as in the previous post, also could
make sense.)
;; comparison less-than
(define (less-than? a1 a2 . rest)
(if (null? rest)
(< a1 a2)
(and (< a1 a2)
(apply less-than? a2 rest))))
both would be correct, depending on which way one wishes to view the op.
i think the language of r5rs and prior is a bit confusing on this count;
the procedures are defined as ordering tests, but are referred to as
comparisons. this is compounded by the definitions of <, <=, =, >=, and >
used elsewhere.
so, which view of these procedures should be followed?
-elf
_______________________________________________
r6rs-discuss mailing list
[email protected]
http://lists.r6rs.org/cgi-bin/mailman/listinfo/r6rs-discuss