[Rd] "+" for character method...

Duncan Murdoch murdoch at stats.uwo.ca
Mon Aug 28 01:40:07 CEST 2006


Thomas Lumley wrote:
> On Sat, 26 Aug 2006, John Chambers wrote:
>
>   
>> Well, two comments, in two non-compatible directions.
>>
>> 1.  I have to say that I find the idea of using "+" to paste character
>> strings together aesthetically ugly.
>>     
>
> Hear, hear!
>
> In a language where you can define new binary operators easily making the 
> same operator do addition and concatenation really doesn't seem a good 
> idea. It is done in some other languages, but so is using the same 
> operator for printing and multiplying by powers of two.
>   
The argument isn't that there is one language that does this, there are 
lots.  This is why we should not define "-" or "*" or "^" for strings; 
those definitions might be convenient, but are not widespread enough to 
increase the clarity of source code.

And it's not a matter of defining "+" to do concatenation in some cases, 
addition in others:  it's defining addition of strings to *be* 
concatenation.  That's an important difference.
> Lack of commutativity is obvious, but lack of associativity seems much 
> more dangerous. Suppose x, y, and z are a mixture of strings and numbers. 
> Hands up everyone who knows (without cheating) whether x+y+z would be 
> interpreted the same way in R (with the current parser) as in Java or 
> Delphi or Python (and are these all the same?).  Which of
>     "two + two = " + 2 + 2
>     2 + 2 + " = two + two"
> would be correct?
>   

I would say that no reasonable language should define whether a+b+c is 
equal to (a+b)+c
or a+(b+c). 

This isn't a problem particular to the proposed string addition, in 
general floating point addition isn't associative either, and in R, even 
addition of integers isn't associative.

People who write a+b+c should be willing to accept either result.

Duncan Murdoch
>  	-thomas
>
> Thomas Lumley			Assoc. Professor, Biostatistics
> tlumley at u.washington.edu	University of Washington, Seattle
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



More information about the R-devel mailing list