Re: [Rd] [R] Semantics of sequences in R

From: Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk_at_idi.ntnu.no>
Date: Mon, 23 Feb 2009 10:09:44 +0100

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:

    sort(1:10)
    # fine

    sort(as.list(1:10))
    # error: no comparator specified

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

vQ

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



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 23 Feb 2009 - 08:12:01 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 23 Feb 2009 - 10:30:45 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive