17

Many reasonably modern programming languages (Java, Python, C++, Ruby) use + to represent string concatenation. "A" + "B" is the string "AB".

Languages with a more mathematical background tend to use other operators for this (Haskell uses the monoid operator <> or the list-specific operator ++, Julia uses *), and some languages with non-C heritage also seem to have diverged from this trend (Elixir uses <>, Perl uses ., Lua uses ..).

What was the first programming language to use + to represent string concatenation? That is, what is the first programming language where it would be idiomatic for me to take two variables a and b containing strings and write a + b, expecting to get something reasonable out of it? I realize C++ predates (and directly influenced) all of the examples I gave above. Did C++ start this trend, or did it borrow the idea from its own predecessors?

user3840170
  • 23,072
  • 4
  • 91
  • 150
Silvio Mayolo
  • 273
  • 2
  • 6
  • 2
    C++ is almost certainly not where this originated from, because string concatenation using a plus operator comes from STL, not from vanilla C++, and was not even standartized before 1998. – introspec Jul 21 '21 at 18:08
  • 2
    I personally find the + notation overly verbose. I prefer SNOBOL/Spitbol where string concatenation is done by the character (that is, whitespace) which you needed to have anyway to separate symbols ... (i.e., juxtaposition leads to concatenation). Among the many improvements to C++ operator overloading I would like would be not just user-defined operator symbols but being able to define operator" " () (to be distinguished from operator""() _suffix_ ...). – davidbak Jul 21 '21 at 18:18
  • 7
    I think BASIC was pretty early here, though not sure if it is the earliest. Or are you not counting BASIC for some reason? – Brian H Jul 21 '21 at 18:19
  • 3
    @JonCuster - perhaps you don't remember because there was no concatenation operator in SNOBOL/Spitbol - as I mention in my comment above! Concatenation just happened: It was such an important expression in a string manipulation language they implemented it as the default operation requiring minimal syntax! Brilliant! (Also used the same (non-)operator to build patterns, the other most important expression in a string manipulation language ...) – davidbak Jul 21 '21 at 18:23
  • 4
  • 1
    @davidbak - good to find out that I did not forget the operator, there just wasn't one to remember. I wish that applied to more things I supposedly learned in the early 1980's... – Jon Custer Jul 21 '21 at 18:45
  • The earliest language to have a concatenation operator was probably PL/1 but it was with ||, not with +. Since then there has been // for Fortran, & for Basic, .. for Lua. – cup Jul 21 '21 at 21:39
  • @user3840170 - Wonderful! Sadly, it obviously didn't happen. Why did he consider it an April Fool's joke, I wonder? Some of the best ideas just go away .... – davidbak Jul 21 '21 at 23:08
  • @davidbak well, it's just not a good idea. In a language like Snobol perhaps because everything is strings you do that all the time, but for most tasks, in modern languages you would only occasionally have to concatenate strings. So it makes much more sense to reserve whitespace for something truely ubiquitous. In ML and Haskell, whitespace is function application. – leftaroundabout Jul 22 '21 at 08:25
  • Overloading + for this purpose only makes sense in languages that have certain approaches to typing. The fact that perl doesn't use this character for this purpose is not just because Larry Wall didn't like the way a plus symbol looked, it's because he wanted '2'+'2' to give '4'. –  Jul 22 '21 at 15:59
  • It was such a big deal that even languages which don't support operator overloading, like Java, still made an exception to allow + for string concatenation. (Last I checked, it's the only overloaded operator in Java, though I may be a little out of date on that one...) – Darrel Hoffman Jul 22 '21 at 17:45
  • @DarrelHoffman Nope, that's still true. + is still super magic in Java, and is the only operator to work on any non-primitive types. – Silvio Mayolo Jul 22 '21 at 18:20
  • @DarrelHoffman: The == and != operators are also overloaded for primitive-primitive and reference-reference operations, but unfortunately not for primitive-reference or reference-primitive operations (which, if allowed, should report equality if the reference identifies a boxed value which equals the primitive, and inequality otherwise). – supercat Jul 22 '21 at 20:42
  • @DarrelHoffman: In Java, - is overloaded for byte, char, short, int, long, float, and double, for example. (And arguably, for null, too.) In fact, most operators in Java have probably at least half a dozen overloads. Which is ironic, considering that the standard answer given to the question why Java doesn't allow programmers to overload operators is that "operator overloading is evil". – Jörg W Mittag Jul 24 '21 at 08:51
  • 1
    @JörgWMittag: not really; byte char short are promoted to int first, and only int long float double are different operations; see JLS 15.18.2 and JVM 2.11.3. Similarly box types are accepted, but are first unboxed and use the same operators not different ones. And I don't see any - (or numeric +) that works on null; can you give an example or cite? – dave_thompson_085 Jul 26 '21 at 04:16
  • Note also that PL/I's '||' found its way into SQL and thence to XPath and XQuery. "+" doesn't work here because of weak typing: if @x and @y are XML attributes, then adding their values is not the same as concatenating their values. – Michael Kay Aug 18 '21 at 23:36

1 Answers1

24

The earliest that was easy to find was ALGOL 68.

After further research, it was unlikely to be any form of BASIC. The original Dartmouth BASIC language was initially released in 1964, but had no string variables. Like ALGOL 68, the fourth version of Dartmouth BASIC was released in 1968, but while it was the first version to have string variables, it did not yet have a string concatenation operator. The eighth version of Dartmouth BASIC released in 1971 was the first to support string concatenation, but the operator was &.

Justme
  • 31,506
  • 1
  • 73
  • 145
  • 4
    A (practical) prerequisite for runtime string concatenation is a heap, so that narrows down the possibilities for contending languages in the 1960s. – dave Jul 21 '21 at 22:11
  • 6
    @another-dave: Many Pascal dialects supported concatenation even though string objects were statically sized. A runtime error would typically occur if the destination was too small to hold the result. – supercat Jul 21 '21 at 22:35
  • FYI: Basic-PLUS (1972) used + as the string concatenation operator ('&' was used as a line-continuation token). – RBarryYoung Jul 22 '21 at 15:26
  • Link: https://ia801901.us.archive.org/BookReader/BookReaderImages.php?zip=/17/items/bitsavers_decpdp11rsSICPLUSLangManOct72_10455777/DEC-11-ORBPA-A-D_BASIC-PLUS_LangMan_Oct72_jp2.zip&file=DEC-11-ORBPA-A-D_BASIC-PLUS_LangMan_Oct72_jp2/DEC-11-ORBPA-A-D_BASIC-PLUS_LangMan_Oct72_0078.jp2&id=bitsavers_decpdp11rsSICPLUSLangManOct72_10455777&scale=4&rotate=0 – RBarryYoung Jul 22 '21 at 15:30
  • 2
    Hmm, actually, BASIC-Plus appeared in 1970, but I cannot confirm if it already had string concatenation by then. "+" was definitely in use by BASIC-Plus by 1972. Algol68 on the other hand did not have a working compiler until 1970, so it's a bit fuzzy... – RBarryYoung Jul 22 '21 at 15:40