Concatenation

In formal language theory and computer programming, concatenation is the operation of joining sequential objects, such as character strings, files, lists or pieces of audio or video, end-to-end. For example, the concatenation of "snow" and "ball" is "snowball". In certain formalizations of concatenation theory, also called string theory, string concatenation is a primitive notion.

## Contents

* 1 Terminology
* 2 Syntax
* 2.1 Lisp
* 2.1.1 Implementation
* 2.2 Prolog
* 2.3 Miranda
* 2.4 Haskell
* 2.5 Perl
* 2.6 Python
* 2.7 Bash
* 2.8 Other languages
* 3 Implementation
* 4 Concatenation of sets of strings
* 5 Algebraic properties
* 6 Applications
* 6.1 Audio and telephony
* 6.2 Database theory
* 6.3 Recreational mathematics
* 7 See also
* 8 References
* 8.1 Citations
* 8.2 Sources
* 9 Bibliography

## Terminology

To concatenate is to join two or more sequential objects together to create a new object, generally without modifying any of the original objects.

To append is to modify a sequential object by adding material at the end. Thus to append a to b is to modify b to be equal to the concatenation of the original value of b and a. Despite this, in some high-level programming languages, especially functional and logical ones, `append` is the operation for concatenating lists or arrays without modifying the originals.

To prepend is to modify a sequential object by adding material at the beginning. Thus to prepend a to b is to modify b to be equal to the concatenation of a and the original value of b.

## Syntax

In many programming languages, string concatenation is a binary infix operator, and in some it is written without an operator. This is implemented in different ways:

* Overloading the plus sign `+` Example from C#: `"Hello, " + "World"` has the value `"Hello, World"`.
* Dedicated operator, such as `.` in PHP, `&` in Visual Basic,[1] and `||` in SQL.[2][3] This has the advantage over reusing `+` that it allows implicit type conversion to string.
* string literal concatenation, which means that adjacent strings are concatenated without any operator. Example from C: `"Hello, " "World"` has the value `"Hello, World"`.
* Interpolation, using a string interpolater to concatenate. Example from C#: `$"{hello}{world}"`, where `string hello = "Hello, ";` and `string world = "World";`
* Formatting, using a string formatter to concatenate. Example from Java: `String.format("%s%s", "Hello, ", "World")`

In many scientific publications or standards the concatenation operator notation used is `||`.[4]

### Lisp

The `append` procedure takes zero or more (linked) lists as arguments, and returns the concatenation of these lists.

(append '(1 2 3) '(a b) '() '(6))
;Output: (1 2 3 a b 6)

Since the `append` procedure must completely copy all of its arguments except the last, both its time and space complexity are O(n) for a list of n elements. It may thus be a source of inefficiency if used injudiciously in code.

The `nconc` procedure (called `append!` in Scheme) performs the same function as `append`, but destructively: it alters the cdr of each argument (save the last), pointing it to the next list.

#### Implementation

`Append` can easily be defined recursively in terms of `cons`. The following is a simple implementation in Scheme, for two arguments only:

(define append
(lambda (ls1 ls2)
(if (null? ls1)
ls2
(cons (car ls1) (append (cdr ls1) ls2)))))

Append can also be implemented using fold-right:

(define append
(lambda (a b)
(fold-right cons b a)))

### Prolog

The logic programming language Prolog features a built-in `append` predicate, which can be implemented as follows:

append([],Ys,Ys).
append([X|Xs],Ys,[X|Zs]) :-
append(Xs,Ys,Zs).

This predicate can be used for appending, but also for picking lists apart. Calling

?- append(L,R,[1,2,3]).

yields the solutions:

L = [], R = [1, 2, 3] ;
L = [1], R = [2, 3] ;
L = [1, 2], R = [3] ;
L = [1, 2, 3], R = []

### Miranda

In Miranda, this right-fold, from Hughes (1989:5-6), has the same semantics (by example) as the Scheme implementation above, for two arguments.

append a b = reduce cons b a

Where reduce is Miranda's name for fold, and cons constructs a list from two values or lists.

For example,

append [1,2] [3,4] = reduce cons [3,4] [1,2]
= (reduce cons [3,4]) (cons 1 (cons 2 nil))
= cons 1 (cons 2 [3,4]))
(replacing cons by cons and nil by [3,4])
= [1,2,3,4]

### Haskell

In Haskell, this right-fold has the same effect as the Scheme implementation above:

append :: [a] -> [a] -> [a]
append xs ys = foldr (:) ys xs

This is essentially a reimplementation of Haskell's `++` operator.

### Perl

In Perl, the push function is equivalent to the append method, and can be used in the following way.

my @list;
push @list, 1;
push @list, 2, 3;

The end result is a list containing [1, 2, 3]

The unshift function appends to the front of a list, rather than the end

my @list;
unshift @list, 1;
unshift @list, 2, 3;

The end result is a list containing [2, 3, 1]

When opening a file, use the ">>" mode to append rather than over write.

open(my $fh, '>>', "/some/file.txt");
print $fh "Some new text\n";
close $fh;

Note that when opening and closing file handles, one should always check the return value.

### Python

In Python, use the list method `extend` or the infix operators `+` and `+=` to append lists.

>>> l = [1, 2]
>>> l.extend([3, 4, 5])
>>> l
[1, 2, 3, 4, 5]
>>> l + [6, 7]
[1, 2, 3, 4, 5, 6, 7]

Do not confuse with the list method `append`, which adds a single element to a list:

>>> l = [1, 2]
>>> l.append(3)
>>> l
[1, 2, 3]

### Bash

In Bash the append redirect is the usage of ">>" for adding a stream to something, like in the following series of shell commands:

echo Hello world! >text; echo Goodbye world! >>text; cat text

The stream "Goodbye world!" is added to the text file written in the first command. The ";" implies the execution of the given commands in order, not simultaneously. So, the final content of the text file is:

Hello world!
Goodbye world!

### Other languages

Other high-level programming languages which feature linked lists as primitive data structures have adopted an `append`. To append lists, as an operator, Haskell uses `++`, OCaml uses `@`. Other languages use the `+` or `++` symbols to nondestructively concatenate a string, list, or array.

## Implementation

In programming, string concatenation generally occurs at run time, as string values are typically not known until run time. However, in the case of string literals, the values are known at compile time, and thus string concatenation can be done at compile time either via string literal concatenation or via constant folding, a potential run-time optimization.

## Concatenation of sets of strings

In formal language theory and pattern matching (including regular expressions), the concatenation operation on strings is generalised to an operation on sets of strings as follows:

For two sets of strings S1 and S2, the concatenation S1S2 consists of all strings of the form vw where v is a string from S1 and w is a string from S2, or formally S1S2 = { vw : v ∈ S1, w ∈ S2 }. Many authors also use concatenation of a string set and a single string, and vice versa, which are defined similarly by S1w = { vw : v ∈ S1 } and vS2 = { vw : w ∈ S2 }. In these definitions, the string vw is the ordinary concatenation of strings v and w as defined in the introductory section.

For example, if F = { a, b, c, d, e, f, g, h }, and R = { 1, 2, 3, 4, 5, 6, 7, 8 }, then FR denotes the set of all chess board coordinates in algebraic notation, while eR denotes the set of all coordinates of the kings' file.

In this context, sets of strings are often referred to as formal languages. The concatenation operator is usually expressed as simple juxtaposition (as with multiplication).

## Algebraic properties

The strings over an alphabet, with the concatenation operation, form an associative algebraic structure called a free monoid. The identity element is the null string.

Sets of strings with concatenation and alternation form a semiring, with concatenation distributing over alternation. The identity for alternation (the 0 element) is the empty set and identity for concatenation (the 1 element) is the set containing just the null string.

## Applications

### Audio and telephony

In programming for telephony, concatenation is used to provide dynamic audio feedback to a user. For example, in a "time of day" speaking clock, concatenation is used to give the correct time by playing the appropriate recordings concatenated together. For example: "at the tone, the time will be", "eight", "thirty", "five", "and", "twenty", "five", "seconds".

The recordings themselves exist separately, but playing them one after the other provides a grammatically correct sentence to the listener.

This technique is also used in number change announcements, voice mail systems, or most telephony applications that provide dynamic feedback to the caller (e.g. moviefone, tellme, and others).

Programming for any kind of computerised public address system can also employ concatenation for dynamic public announcements (for example, flights in an airport). The system would archive recorded speech of numbers, routes or airlines, destinations, times, etc. and play them back in a specific sequence to produce a grammatically correct sentence that is announced throughout the facility.

### Database theory

One of the principles of relational database design is that the fields of data tables should reflect a single characteristic of the table's subject, which means that they should not contain concatenated strings. When concatenation is desired in a report, it should be provided at the time of running the report. For example, to display the physical address of a certain customer, the data might include building number, street name, building sub-unit number, city name, state/province name, postal code, and country name, e.g., "123 Fake St Apt 4, Boulder, CO 80302, USA", which combines seven fields. However, the customers data table should not use one field to store that concatenated string; rather, the concatenation of the seven fields should happen upon running the report. The reason for such principles is that without them, the entry and updating of large volumes of data becomes error-prone and labor-intensive. Separately entering the city, state, ZIP code, and nation allows data-entry validation (such as detecting an invalid state abbreviation). Then those separate items can be used for sorting or indexing the records, such as all with "Boulder" as the city name.

### Recreational mathematics

In recreational mathematics, many problems concern the properties of numbers under concatenation of their numerals in some base. Examples include home primes (primes obtained by repeatedly factoring the increasing concatenation of prime factors of a given number), Smarandache–Wellin numbers (the concatenations of the first prime numbers), and the Champernowne and Copeland–Erdős constants (the real numbers formed by the decimal representations of the positive integers and the prime numbers, respectively).

## See also

* Rope (data structure)

## References

### Citations

1. ↑ "Concatenation Operators in Visual Basic". .NET Visual Basic guide Language features: Operators and expressions. Microsoft. 2015. http://msdn.microsoft.com/en-us/library/te2585xw.aspx.
2. ↑ "SQL | Concatenation Operator". 12 January 2018. https://www.geeksforgeeks.org/sql-concatenation-operator/.
3. ↑ "Concatenation Operator". Oracle Database Online Documentation, 10g Release 2 (10.2) / Administration: Database SQL Reference. Oracle. http://docs.oracle.com/cd/B19306_01/server.102/b14200/operators003.htm.
4. ↑ Moriarty, Kathleen; Kaliski, Burt; Jonsson, Jakob; Rusch, Aneas (November 2016). ﻿PKCS #1: RSA Cryptography Specifications Version 2.2﻿ (Report). Internet Engineering Task Force. https://datatracker.ietf.org/doc/html/rfc8017#section-2.

### Sources

* Hopcroft, John E.; Ullman, Jeffrey D. (1979). Introduction to Automata Theory, Languages and Computation. Addison-Wesley. ISBN 0-201-02988-X.
* Corcoran, John; Frank, William; Maloney, Michael (1974). "String theory". Journal of Symbolic Logic 39 (4): 625–637. doi:10.2307/2272846.

## Bibliography

* Hughes, John (1989). "Why functional programming matters". Computer Journal 32 (2): 98–107. doi:10.1093/comjnl/32.2.98. http://www.math.chalmers.se/~rjmh/Papers/whyfp.pdf.
* Steele, Guy L. (1990). COMMON LISP: the language (2nd ed.). Bedford, Mass: Digital Press. ISBN 1-55558-041-6.

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Concatenation. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Concatenation&oldid=4417167"