[mercury-users] String concatenation

Richard A. O'Keefe ok at atlas.otago.ac.nz
Thu Apr 22 09:18:21 AEST 1999


Ralph Becket wrote:

	Just a thought, but it'd be very nice to have +/2 overloaded for
	strings.
	
Argh!  Please, NO!
There is an excellent rule of thumb for operator overloading:
for each operator there should be a useful set of axioms which
are satisfied by _all_ overloadings.

For +, the axioms include
 - x + y = y + x
 - there is a zero, such that Z + x = x + Z = x
and these are true for integers, rational numbers, floating point
numbers, complex numbers formed from them, polynomials with coefficients
from the preceding, matrices with elements from the preceding, ...
Unfortunately, the associative law does _NOT_ hold for floating
point numbers.

Now string concatenation, spelled "||" in PL/I and Icon, "//" in
Fortran, <empty> in SNOBOL and Awk, "&" in Ada, "," in APL, "+++" in
Clean, "++" in Haskell, and "^" in SML, _does_ have a zero (the empty
string) and _is_ associative, but it is _NOT_ commutative.  When do
algebraists use a "+" sign for an operation and when a "x"? They use "x"
for a binary operation that is not or might not be commutative, and "+"
for an operation that must be commutative.

There are grounds for having _some_ infix operator for string
concatenation (although using it is a spectacularly inefficient
way to build strings; Mercury programmers should be using DCGs
to build as well as to parse strings), but it should be almost
anything except "+".  The usual mathematical sign for it appears
to be a "tie" (like a raised "frown"), which is rather apt.
SML's "^" is the closest ASCII approximation to the "tie".
If Mercury were to have an infix operator for string concatenation,
"^" or "^^" would be far better candidates than "+".

(Note that the overloading of "+" in Java has proved *EXTREMELY*
confusing.)






More information about the users mailing list