[R] R as a programming language

Duncan Murdoch murdoch at stats.uwo.ca
Thu Nov 8 21:02:15 CET 2007


On 11/8/2007 2:44 PM, Peter Dalgaard wrote:
> Duncan Murdoch wrote:
>> On 11/8/2007 11:51 AM, Thomas Lumley wrote:
>>   
>>> On Wed, 7 Nov 2007, Duncan Murdoch wrote:
>>>
>>>     
>>>> At first I thought you were complaining about the syntax, which I find
>>>> ugly.  There was a proposal last year to overload + to do concatenation
>>>> of strings, so you'd type cat("x=" + x + "y=" + y + "\n"), but there was
>>>> substantial resistance, on the grounds that + should be commutative.
>>>>
>>>>       
>>> My objection, at least, was that + should be *associative*.  I don't think 
>>> anyone would expect a + b and b+a to be the same for strings, but I do 
>>> think the fact that (a+b)+c and a+(b+c) would be different (if some of a, 
>>> b,c were strings) has real potential for ugliness.
>>>     
>>
>> Sorry, I forgot about that. I think there were complaints about both 
>> commutativity and associativity.
>>
>> I do think lack of associativity is a less impressive complaint, because
>> it doesn't even hold for floating point addition without mixing types:
>>
>>  > x <- .Machine$double.eps/2
>>  > A <- (1 + x) + x
>>  > B <- 1 + (x + x)
>>  > A == B
>> [1] FALSE
>>
>> As far as I can see, string concatenation would only lose associativity 
>> when some of the operands were automatically converted to strings. 
>> Mixed type operations often give surprising results.
>>
>>   
> Exactly. But the issue in that case is not a difference on the order of 
> Machine$double.eps but between "24" and "222" (from "2"+2+2).
> 
> Still, even numbers act up once you get into recycling rules: Try
> 1:2+1:3+1:4

My example gave a tiny difference, yours gave a big difference.  So 
addition in R is not associative.  Anyone who expects addition in R to 
be associative is going to get into trouble eventually.

> (We've been here before, haven't we?)

Yes, we have.

Duncan Murdoch



More information about the R-help mailing list