From Waclaw.Marcin.Kusnierczyk at idi.ntnu.no  Mon Feb 23 10:09:44 2009
From: Waclaw.Marcin.Kusnierczyk at idi.ntnu.no (Wacek Kusnierczyk)
Date: Mon, 23 Feb 2009 10:09:44 +0100
Subject: [Rd] [R] Semantics of sequences in R
In-Reply-To: <49A25FC8.2060903@idi.ntnu.no>
References: <8b356f880902221242r47cdb138w9b1ac05cc5e5e317@mail.gmail.com>	<49A1BFC7.9060503@stats.uwo.ca>	<8b356f880902221350q4a976bfr19b0d5b01a989250@mail.gmail.com>	<20090223113422.76cc1494@absentia>	<8b356f880902222121taee8506sf21a0a532345dfb0@mail.gmail.com>
	<49A25FC8.2060903@idi.ntnu.no>
Message-ID: <49A267D8.4060108@idi.ntnu.no>

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



[Rd] [R] Semantics of sequences in R
Wacek Kusnierczyk