ArticleslgStudy

computer science

Concatenation

Concatenation is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Concatenation rather than just read about it. In short: 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".

Concatenation — main illustration
Concatenation — illustration

Key takeaways

  • Concatenation belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Concatenation to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Concatenation from memory before moving on to harder problems.

Reference excerpt

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.

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. Furthermore, the notation may be specific to string concatenation or support concatenation of various sequential data types depending on the language. Examples include:

Overloading the plus sign +, such as most BASICs and C#: "Hello, " + "World" has the value "Hello, World". Dedicated operator, such as . in PHP, & in Visual Basic, || in SQL and ~ in D. Advantages over reusing + may include supporting implicit type conversion to strings and distinguishing numeric array/list concatenation from vector addition, depending on the language. String literal concatenation, in which adjacent strings are concatenated without any operator. Example from C: "Hello, " "World" has the value "Hello, World". Interpolation, using a special syntax to embed variables or expressions within strings. 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 ||.

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

The append function copies all of its arguments except the last, so its time and space complexity are O(n) for a list of n elements. It may thus be a source of inefficiency if used unnecessarily. The nconc function (called append! in Scheme) concatenates its arguments destructively, altering the last cdr of each non-nil argument except the last, making it point to the next list.

Implementation Append can be defined recursively in terms of cons. The two-argument case in Scheme:

Append can also be implemented using Scheme fold-right or Common Lisp reduce:

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

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

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:

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.

The end result is a list containing [1, 2, 3] The unshift function appends to the front of a list, rather than the end

The end result is a list containing [2, 3, 1] When opening a file, use the ">>" mode to append rather than over write.

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

Python In Python, the infix operator + concatenates its arguments:

The list method extend and the augmented assignment operator += append by modification:

The list method append appends a single element to a list by modification:

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

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:

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 String concatenation generally occurs at run time. For string literals, it can be done at compile time as a special case of constant folding.

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).

… excerpt ends here. Continue reading the full article.

Illustrations

Concatenation: A spreadsheet's concatenate ("&") function is used to assemble a complex text string—in this example, XML code for an SVG "circle" element.
A spreadsheet's concatenate ("&") function is used to assemble a complex text string—in this example, XML code for an SVG "circle" element.

Worked examples

Example 1 — a first encounter with Concatenation

Start with the simplest possible case. Write down what Concatenation claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Concatenation before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Concatenation ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Concatenation

In research
Concatenation appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Concatenation in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Concatenation is common in secondary-school and first-year university syllabi. It links to neighbouring topics Formal languages, Operators (programming), String (computer science), so understanding it makes those chapters shorter.
In everyday life
Look for Concatenation outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Concatenation” →

How to study Concatenation in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Concatenation means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Concatenation out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Concatenation in simple terms?

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".

Why does Concatenation matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Concatenation?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Concatenation.

Tags

  • Formal languages
  • Operators (programming)
  • String (computer science)

Keep exploring