Append
   HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, append is the operation for concatenating
linked list In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which ...
s or arrays in some
high-level programming language In computer science, a high-level programming language is a programming language with strong abstraction from the details of the computer. In contrast to low-level programming languages, it may use natural language ''elements'', be easier to u ...
s.


Lisp

Append originates in the
Lisp programming language Lisp (historically LISP) is a family of programming languages with a long history and a distinctive, fully parenthesized prefix notation. Originally specified in 1960, Lisp is the second-oldest high-level programming language still in common u ...
. 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 A scheme is a systematic plan for the implementation of a certain idea. Scheme or schemer may refer to: Arts and entertainment * ''The Scheme'' (TV series), a BBC Scotland documentary series * The Scheme (band), an English pop band * ''The Schem ...
) 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 Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics ...
in terms of
cons In computer programming, ( or ) is a fundamental function in most dialects of the Lisp programming language. ''constructs'' memory objects which hold two values or pointers to two values. These objects are referred to as (cons) cells, conses, ...
. 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)))


Other languages

Following Lisp, other
high-level language In computer science, a high-level programming language is a programming language with strong abstraction from the details of the computer. In contrast to low-level programming languages, it may use natural language ''elements'', be easier to use, ...
s which feature
linked list In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which ...
s as primitive
data structure In computer science, a data structure is a data organization, management, and storage format that is usually chosen for efficient access to data. More precisely, a data structure is a collection of data values, the relationships among them, ...
s have adopted an append.
Haskell Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lan ...
uses the ++ operator to append lists.
OCaml OCaml ( , formerly Objective Caml) is a general-purpose, multi-paradigm programming language Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms. ...
uses the @ operator to append lists. Other languages use the + or ++ symbols for nondestructive string/list/array concatenation.


Prolog

The
logic programming language Logic programming is a programming paradigm which is largely based on formal logic. Any program written in a logic programming language is a set of sentences in logical form, expressing facts and rules about some problem domain. Major logic prog ...
Prolog Prolog is a logic programming language associated with artificial intelligence and computational linguistics. Prolog has its roots in first-order logic, a formal logic, and unlike many other programming languages, Prolog is intended primarily ...
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, ,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

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 In computer programming, ( or ) is a fundamental function in most dialects of the Lisp programming language. ''constructs'' memory objects which hold two values or pointers to two values. These objects are referred to as (cons) cells, conses, ...
constructs a list from two values or lists. For example, append ,2 ,4= reduce cons ,4 ,2 = (reduce cons ,4 (cons 1 (cons 2 nil)) = cons 1 (cons 2 ,4) (replacing cons by cons and nil by ,4 = ,2,3,4


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 Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
, 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 , 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 , 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 =
, 2 The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
l.extend( , 4, 5 print l + , 7
After executing this code, l is a list containing
, 2, 3, 4, 5 The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
while the output generated is the list , 2, 3, 4, 5, 6, 7 Do not confuse with the list method "append", which adds a single element to a list: l =
, 2 The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
l.append(3)
Here, the result is a list containing , 2, 3


Bash

In
Bash Bash or BASH may refer to: Arts and entertainment * ''Bash!'' (Rockapella album), 1992 * ''Bash!'' (Dave Bailey album), 1961 * '' Bash: Latter-Day Plays'', a dramatic triptych * ''BASH!'' (role-playing game), a 2005 superhero game * "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:
{{samp, Goodbye world!


References

* Hughes, John. 1989. Why functional programming matters. ''Computer Journal 32'', 2, 98-107. https://web.archive.org/web/20070413005952/http://www.math.chalmers.se/~rjmh/Papers/whyfp.pdf * Steele, Guy L. Jr. ''
Common Lisp Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fr ...
: The Language, Second Edition''. 1990. pg. 418, description of append. Functional programming Lisp (programming language) Programming constructs Articles with example code DOS on IBM PC compatibles