Re: vector compare Donovan Kolbly 09 Jun 2005 22:11 UTC

On Thu, 9 Jun 2005, Jens Axel Søgaard wrote:

> Per Bothner wrote:
>> Sebastian Egner wrote:
>
>>>  > The default compare for vectors is unusual, and more critically it is
>>>  > incompatible with the default compare for lists and strings.
>>>  > The latter are both lexicographic compare.
>>>
>>> Correct. In this SRFI, vectors are compared first by length and then
>>> lexicographically by default.
>
>> Do you have any examples or use cases or algorithms that are simpler
>> when using this "natural order"?  What prior art is there for
>> using this order?
>
> As Sebastian argues, there not 1 natural order of vectors, but the
> ordering in the srfi is /a/ natural order. A concrete
> example is the sorting of polynomials:
>
>     x^3 > x^2 + 1 > x^2 > 42
>
> A concrete representation in terms of vectors yields:
>
>  #(3 0 0 0) > #(2 0 1) > #(2 0 0) > #(42)

It is clear that there are some uses of vectors for which the SRFI's
default-compare is more useful.  But there are also uses of vectors (e.g.,
as a sequence representation) for which using the same default-compare as
for lists is more useful (For example, I occasionally find myself flipping
a string into a vector-of-characters representation.  If I were to
sometime sort them in that representation, I doubt I'd remember why they
weren't matching up with the string-sorted ones!  I don't sort vectors
often enough to remember that they compare strangely.)

In sum, I expect the extra cognitive load of explaining why and
remembering that:

    (vector-compare x y)

is so much different than:

    (list-compare (vector->list x) (vector->list y))

is just not worth it.

Hence, I, too, would prefer using lexicographic comparison for all
sequence types.  It's easier to explain and to remember.

-- Donovan Kolbly                    (  xxxxxx@rscheme.org
 				     (  http://www.rscheme.org/~donovan/