HOME

TheInfoList



OR:

In
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, 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 whi ...
s or
arrays An array is a systematic arrangement of similar objects, usually in rows and columns. Things called an array include: {{TOC right Music * In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
in some
high-level programming language A high-level programming language is a programming language with strong Abstraction (computer science), abstraction from the details of the computer. In contrast to low-level programming languages, it may use natural language ''elements'', be ea ...
s.


Lisp

Append originates in the
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
Lisp Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation. Originally specified in the late 1950s, ...
. 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 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 programming language A high-level programming language is a programming language with strong Abstraction (computer science), abstraction from the details of the computer. In contrast to low-level programming languages, it may use natural language ''elements'', be ea ...
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 whi ...
s as primitive
data structure In computer science, a data structure is a data organization and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the relationships amo ...
s have adopted an append. To append lists, as an operator,
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 pioneered several programming language ...
uses ++,
OCaml OCaml ( , formerly Objective Caml) is a General-purpose programming language, general-purpose, High-level programming language, high-level, Comparison of multi-paradigm programming languages, multi-paradigm programming language which extends the ...
uses @. Other languages use the + or ++ symbols to nondestructively concatenate a
string String or strings may refer to: *String (structure), a long flexible structure made from threads twisted together, which is used to tie, bind, or hang other objects Arts, entertainment, and media Films * ''Strings'' (1991 film), a Canadian anim ...
, list, or array.


Prolog

The
logic programming Logic programming is a programming, database and knowledge representation paradigm based on formal logic. A logic program is a set of sentences in logical form, representing knowledge about some problem domain. Computation is performed by applyin ...
language
Prolog Prolog is a logic programming language that has its origins in artificial intelligence, automated theorem proving, and computational linguistics. Prolog has its roots in first-order logic, a formal logic. Unlike many other programming language ...
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

In Miranda (programming language), Miranda, this right- Fold (higher-order function), 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 (higher-order function), 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

In
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 pioneered several programming language ...
, this right- Fold (higher-order function), 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 high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language". Perl was developed ...
, 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 or the infix operators and to append lists. >>> l = , 2>>> l.extend( , 4, 5 >>> l , 2, 3, 4, 5>>> l + , 7 , 2, 3, 4, 5, 6, 7 Do not confuse with the list method , which adds a single element to a list: >>> l = , 2>>> l.append(3) >>> l , 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:


References

* *{{Cite journal , last=Steele , first=Guy L. Jr. , author-link=Guy L. Steele Jr. , year=1990 , title=
Common Lisp Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ''ANSI INCITS 226-1994 (S2018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperli ...
: The Language , edition=2nd , page=418 Description of append. Functional programming Lisp (programming language) Programming constructs Articles with example code Articles with example Haskell code Articles with example Perl code Articles with example Python (programming language) code DOS on IBM PC compatibles