[Rd] [R] Semantics of sequences in R

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Mon Feb 23 10:09:44 CET 2009

Wacek Kusnierczyk wrote:
> btw. it's interesting that in revision 38438 (2006) Prof Brian Ripley
> introduced (or so does the commit message say) sorting complex numbers,
> and now you have things like:
>     1i > 0i
>     # Error in 0+0i > 0+1i : invalid comparison with complex values
>     sort(c(1i, 0i))
>     # 0i 1i

it's interesting also because one of the arguments for why sort does not
operate on lists is that it is not clear how to compare their elements
(see, e.g., [1]).  so why would sort sort complex numbers, if r cannot
compare them?

arguing that sort cannot operate on lists because it does not know how
to compare the elements is based on a misconception.  algorithms for
sorting are in essence ignorant of what the elements of the sequences to
be sorted are;  sorting works equally well with numbers, strings,
functions, plants, waste, etc., provided that an appropriate comparator
is specified.

i think that the right design for sort and sort.list would be to have
the former operate on atomic vectors (``real'' vectors, those that can
be considered lists but they're not) with defaults for the respective
types, and the latter operate on lists with no default for the
comparator, even when all elements happen to be of the same type:

    # fine

    # error: no comparator specified

    sort(as.list(1:10), `<`)
    # fine


[1] http://tolstoy.newcastle.edu.au/R/e4/help/08/07/16231.html

More information about the R-devel mailing list