Topic: Concatenation operator (Another string question)


Author: kanze@us-es.sel.de (James Kanze)
Date: 6 Jul 93 19:23:52
Raw View
In article <C9Hy3C.Bqv@da_vinci.it.uswc.uswest.com>
tblancha@lookout.mtt.it.uswc.uswest.com (Todd Blanchard) writes:

|> In article <WARSAW.93Jun30134507@anthem.nlm.nih.gov> warsaw@nlm.nih.gov (Barry A. Warsaw) writes:

|> >Too bad we can't overload operator dot.  Then string concatenation
|> >could look Perl-y! :-)

|> > string big = string1 . string2;

|> I think operator+ for concatenation is perfectly natural. operator += for
|> concatenation is ok for adding to the first string.  But, for the math heads
|> who insit on being obtuse, consider operator- for concatenation.  Its not
|> commutative (and remember, for ints, * _is_ commutative) and it looks like
|> hyphenation (which should please all the english types).

I think that the important thing is that for the basic types defined
in C/C++, both '+' and '*' are commutative.  (I'm a C++ type, not a
math type.)  So on one hand, I don't like them for concatenation.



Author: hartman@ulogic.UUCP (Richard M. Hartman)
Date: 7 Jul 93 00:14:54 GMT
Raw View
In article <1993Jul3.193229.27059@unix.brighton.ac.uk> je@unix.brighton.ac.uk (John English) writes:
>I'm slightly amazed at the number of people engaged in earnest discussion
>about whether * is a good concatenation operator or not.  As I recall, the
>original poster had his tongue *firmly* in his cheek at the time.  I wonder
>if he is equally amazed?

I saw no such signs.  I have had some e-mail exchanges with someone
(perhaps not the original poster) who is quite serious about * being
more algebraically correct than +.  Unfortunately my background in
abstract mathematics is not up to following these arguments w/o a
little more backgroupd reading on my part.

  -Richard Hartman
  hartman@ulogic.COM

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Just because you're paranoid doesn't mean they aren't to get you....





Author: djones@megatest.com (Dave Jones)
Date: Wed, 7 Jul 1993 12:53:42 GMT
Raw View


Author: tblancha@lookout.mtt.it.uswc.uswest.com (Todd Blanchard)
Date: Thu, 1 Jul 1993 17:33:12 GMT
Raw View
In article <WARSAW.93Jun30134507@anthem.nlm.nih.gov> warsaw@nlm.nih.gov (Barry A. Warsaw) writes:
>
>Too bad we can't overload operator dot.  Then string concatenation
>could look Perl-y! :-)
>
> string big = string1 . string2;
>

I think operator+ for concatenation is perfectly natural. operator += for
concatenation is ok for adding to the first string.  But, for the math heads
who insit on being obtuse, consider operator- for concatenation.  Its not
commutative (and remember, for ints, * _is_ commutative) and it looks like
hyphenation (which should please all the english types).

Todd Blanchard





Author: jimad@microsoft.com (Jim Adcock)
Date: 01 Jul 93 18:28:58 GMT
Raw View
In article <C9Et0q.EAo@megatest.com> djones@megatest.com (Dave Jones) writes:
|For those of us who have studied math, using + for concatenation is not
|intuitive; It is, well, *irksome*. Addition is commutative. As I have
|explained in previous posts, operator+ is invariably reserved for
|commutative functions, but concat(A,B) is not the same as concat(B,A).

Irksome or not, "+" is the most common operator for string concats
in C++ libraries, and in other languages.  First and foremost operators
should be mnemonic, and based on prior practice.  Introducing new
operators where prior practice didn't exist comes at high price --
consider the iostream << >> operators as an example.  Trying to change
existing practice is practically impossible.  People have learned "+",
expect "+", and remember "+".  Do you really think any form of
mathematical equivalence comes to the minds of 99% of the user
population when they are performing a string concat?

Agreed that it would be nice if C++ catered better to the needs of
math programmers.  In which case we'd have exponentiation support,
some solution along the lines of noalias, and real support for
IEEE numbers.  And we could kill off both fortran and the numerical
C group.  But such isn't to be.




Author: beard@bedford.symantec.com (Patrick C. Beard)
Date: 1 Jul 93 18:14:52 GMT
Raw View
In article <C9Et0q.EAo@megatest.com> Dave Jones, djones@megatest.com
writes:
>From article <1983@ulogic.UUCP>, by hartman@ulogic.UUCP (Richard M.
Hartman):
>
>> There may be good reasons having to do with precedence or
>> whatever for using the * operator, but at the "intuitive"
>> level it just doesn't work.
>
>For those of us who have studied math, using + for concatenation is not
>intuitive; It is, well, *irksome*. Addition is commutative. As I have
>explained in previous posts, operator+ is invariably reserved for
>commutative functions, but concat(A,B) is not the same as concat(B,A).
>
>Too bad there is no "adjacency operator" except for literal strings.
>AWK has such a feature. To form the concatenation, STR, of two strings
>STR1 and STR2 you write,
>
> STR = STR1 STR2;
>
>But since there is no adjacency operator, what should be used?
>Operator+ goes against its math roots, and apparently some people
>would be puzzled by operator*. Probably the best answer then is not
>to use either operator+ or operator*. I propose the following:
>
> friend String concat(String left, String right);
>
>... and perhaps also
>
> void String::append(String suffix);
> void String::prepend(String prefix);
>
>... or some combination of variations thereof, with const reference
declarators
>sprinkled in where deemed expedient.

How about operator,()? I think that using a comma is a natural way to
express concatenation:

String abcxyz = (String("abc") , String("xyz"));

Here's a really simplified implementation (obviously not a real string
class):

class String {
public:
 String(const char* s);

 friend String operator, (String& str1, String& str2);

private:
 static char* dup(const char* s);

 String(char** s);

private:
 char* itsRep;
};

String::String(const char* s)
{
 itsRep = dup(s);
}

String::String(char** s)
{
 itsRep = *s;
 *s = 0;
}

char* String::dup(const char* s)
{
 return strcpy(new char[1 + strlen(s)], s);
}

String operator, (String& str1, String& str2)
{
 char* news = new char[1 + strlen(str1.itsRep) + strlen(str2.itsRep)];
 strcpy(news, str1.itsRep);
 strcat(news, str2.itsRep);
 return String(&news);
}


void TestString()
{
 String abc("abc");
 String xyz("xyz");
 String abcxyz = (abc , xyz);
}


One annoying thing about this in declarations is the requirement to use
parentheses to make it parse correctly. No solution is ever perfect.

//
// Patrick C. Beard
// beard@cs.ucdavis.edu
//




Author: djones@megatest.com (Dave Jones)
Date: Fri, 2 Jul 1993 03:09:06 GMT
Raw View


Author: djones@megatest.com (Dave Jones)
Date: Fri, 2 Jul 1993 18:21:33 GMT
Raw View


Author: je@unix.brighton.ac.uk (John English)
Date: Sat, 3 Jul 1993 19:32:29 GMT
Raw View
I'm slightly amazed at the number of people engaged in earnest discussion
about whether * is a good concatenation operator or not.  As I recall, the
original poster had his tongue *firmly* in his cheek at the time.  I wonder
if he is equally amazed?

--
-------------------------------------------------------------------------------
John English                    | "There are basically two types of dicks,
Dept. of Computing              |  hard dicks and floppy dicks."
University of Brighton          |    -- from a student essay on storage media
E-mail: je@unix.brighton.ac.uk  | "Disks are divided into sex & tractors."
Fax:    0273 642405             |    -- from one of my lectures (on a bad day)
-------------------------------------------------------------------------------




Author: djones@megatest.com (Dave Jones)
Date: Wed, 30 Jun 1993 00:50:17 GMT
Raw View


Author: warsaw@nlm.nih.gov (Barry A. Warsaw)
Date: 30 Jun 1993 18:45:07 GMT
Raw View
Too bad we can't overload operator dot.  Then string concatenation
could look Perl-y! :-)

 string big = string1 . string2;

Now who doesn't think that's completely natural? :-)




Author: sgl1@kimbark.uchicago.edu (Steven Lane)
Date: Wed, 30 Jun 1993 18:48:54 GMT
Raw View
In article <C9Et0q.EAo@megatest.com> djones@megatest.com (Dave Jones) writes:

>Too bad there is no "adjacency operator" except for literal strings.
>AWK has such a feature. To form the concatenation, STR, of two strings
>STR1 and STR2 you write,
>
> STR = STR1 STR2;
>
>But since there is no adjacency operator, what should be used?
>Operator+ goes against its math roots, and apparently some people
>would be puzzled by operator*. Probably the best answer then is not
>to use either operator+ or operator*. I propose the following:
>
> friend String concat(String left, String right);

Which unfortunately gets rid of the economy of

 StringA = String1 + String2 + ' ' + String4;

Lack of commutuativity just doesn't seem to militate against the "intuitive-
ness" of such expressions; when we see strings being "added", it's readily
apparent that the order does matter. This isn't to say that concatenation is
*just* like addition, only that it's *most* like addition, unless you want
to implement it as a function rather than an operator, at which point the
above remark on economy comes into play again.

--
----
Steve Lane
University of Chicago, Department of History
sgl1@midway.uchicago.edu




Author: hartman@ulogic.UUCP (Richard M. Hartman)
Date: 30 Jun 93 18:20:01 GMT
Raw View
In article <C9Et0q.EAo@megatest.com> djones@megatest.com (Dave Jones) writes:
>From article <1983@ulogic.UUCP>, by hartman@ulogic.UUCP (Richard M. Hartman):
>
>> There may be good reasons having to do with precedence or
>> whatever for using the * operator, but at the "intuitive"
>> level it just doesn't work.
>
>For those of us who have studied math, using + for concatenation is not
>intuitive; It is, well, *irksome*. Addition is commutative. As I have
>explained in previous posts, operator+ is invariably reserved for
>commutative functions, but concat(A,B) is not the same as concat(B,A).

This makes no difference when deciding between * and +, since both
are commutative functions.

 int a, b;

  a+b == b+a --> TRUE
  a*b == b*a --> TRUE

>But since there is no adjacency operator, what should be used?
>Operator+ goes against its math roots, and apparently some people
>would be puzzled by operator*. Probably the best answer then is not
>to use either operator+ or operator*. I propose the following:
>
> friend String concat(String left, String right);
>
>... and perhaps also
>
> void String::append(String suffix);
> void String::prepend(String prefix);
>
>... or some combination of variations thereof, with const reference declarators
>sprinkled in where deemed expedient.

I think that ALL classes should have functions (member or friend)
that do the actual work, and operators are only defined on top of
them as syntactic sugar.  e.g.:

 String &operator +(const String &l, const String &r)
  { return concat(l,r); }

Which leaves us back at operator choice....

What do you think of operator & (as used by the NIH String class)?

  -Richard Hartman
  hartman@ulogic.COM

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
"186,000 mi/sec.  Not just a good idea, it's the LAW!"





Author: jrickard@eoe.co.uk (John Rickard)
Date: 30 Jun 93 19:32:54 GMT
Raw View
Dave Jones (djones@megatest.com) wrote:
: For those of us who have studied math, using + for concatenation is not
: intuitive; It is, well, *irksome*. Addition is commutative. As I have
: explained in previous posts, operator+ is invariably reserved for
: commutative functions, but concat(A,B) is not the same as concat(B,A).

Not invariably; it is used for addition of (possibly infinite)
ordinals, which is not commutative: omega+1 does not equal 1+omega.

Both addition of ordinals and concatenation of strings have the sense
of concatenating the contents of two things; more generally, addition
is often used for operations that are in some sense combining (putting
together) two things: 2+3=5, and if one puts two apples together with
three apples one has five apples.

I think this, together with the probable expectations of most
programmers, justifies using + for string concatenation.

It is true that for languages (sets of strings) it is convenient to
use * for concatenation because that fits well with using + for union,
but I don't think there's a corresponding useful use of + for single
strings.

I sha'n't have email/news access from tomorrow, so flames by phone or
paper mail.

John Rickard                +44 223 321613 (home)
12 Herbert Street           +44 223 421532 (work)
Cambridge CB4 1AQ  UK




Author: b91926@fnclub.fnal.gov (David Sachs)
Date: 30 Jun 1993 21:45:32 GMT
Raw View
In article <1881@eouk5.eoe.co.uk>, jrickard@eoe.co.uk (John Rickard) writes:
|> Dave Jones (djones@megatest.com) wrote:
|> : For those of us who have studied math, using + for concatenation is not
|> : intuitive; It is, well, *irksome*. Addition is commutative. As I have
|> : explained in previous posts, operator+ is invariably reserved for
|> : commutative functions, but concat(A,B) is not the same as concat(B,A).
|>
|> Not invariably; it is used for addition of (possibly infinite)
|> ordinals, which is not commutative: omega+1 does not equal 1+omega.
|>
|> Both addition of ordinals and concatenation of strings have the sense
|> of concatenating the contents of two things; more generally, addition
|> is often used for operations that are in some sense combining (putting
|> together) two things: 2+3=5, and if one puts two apples together with
|> three apples one has five apples.
|>
|> I think this, together with the probable expectations of most
|> programmers, justifies using + for string concatenation.
|>
|> It is true that for languages (sets of strings) it is convenient to
|> use * for concatenation because that fits well with using + for union,
|> but I don't think there's a corresponding useful use of + for single
|> strings.
|>
|> I sha'n't have email/news access from tomorrow, so flames by phone or
|> paper mail.
|>
|> John Rickard                +44 223 321613 (home)
|> 12 Herbert Street           +44 223 421532 (work)
|> Cambridge CB4 1AQ  UK

One possiblility would be to use operator| or operator||.