Append


In computer programming, append is the operation for concatenating linked lists or arrays in some high-level programming languages.

Lisp

Append originates in the Lisp programming language. The append procedure takes zero or more lists as arguments, and returns the concatenation of these lists.

' ' ')
;Output:

Since the append procedure must completely copy all of its arguments except the last, both its time and space complexity are O for a list of elements. It may thus be a source of inefficiency if used injudiciously in code.
The nconc procedure performs the same function as append, but destructively: it alters the cdr of each argument, 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:


ls2
)))

Append can also be implemented using fold-right:

))

Other languages

Following Lisp, other high-level languages which feature linked lists as primitive data structures have adopted an append. Haskell uses the ++ operator to append lists. OCaml uses the @ operator to append lists.
Other languages use the + or ++ symbols for nondestructive string/list/array concatenation.

Prolog

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

append.
append :-
append.

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

?- append.

yields the solutions:
L = , R = ;
L = , R = ;
L = , R = ;
L = , R =

Miranda

This right-fold, from Hughes, has the same semantics 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 = reduce cons
=
= cons 1 )

=

Haskell

This right-fold has the same effect as the Scheme implementation above:

append :: -> ->
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
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
When opening a file, use the ">>" mode to append rather than over write.

open;
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 =
l.extend
print l +

After executing this code, l is a list containing , while the output generated is the list .
Do not confuse with the list method "append", which adds a single element to a list:

l =
l.append

Here, the result is a list containing .

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!