appendis the name of a
procedurefor concatenating (linked) lists or arrays in some high-level programming languages.
Appendoriginates in the
Lisp programming language. The
appendprocedure takes two 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)
appendprocedure must completely copy all of its arguments except the last, both its time and space complexity are O("n") for a list of elements. It may thus be a source of inefficiency if used injudiciously in code.
Appendcan easily be defined recursively in terms of
. 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)))))
Following Lisp, other
high-level languages which feature linked lists as primitive data structures have adopted an
appendHaskell uses the
++operator to append lists.
@operator to append lists.
Other languages use the
++symbols for nondestructive string/list/array concatenation.
logic programming languageProlog features a built-in
appendpredicate, 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 =  , R = [2, 3] ; L = [1, 2] , R =  ; L = [1, 2, 3] , R = 
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
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]
This right-fold has the same effect as the Scheme implementation above:
append :: [a] -> [a] -> [a] append xs ys = foldr (:) ys xsThis is essentially a reimplementation of Haskell's
append is a
DOScommand that allows programs to open data files in specified directories as if they were in the current directory. It appends the directories to the search path list.
* Hughes, John. 1989. Why functional programming matters. Computer Journal 32, 2, 98-107. http://www.math.chalmers.se/~rjmh/Papers/whyfp.pdf
* Steele, Guy L. Jr. "
Common Lisp: The Language, Second Edition". 1990. pg. 418, description of
Wikimedia Foundation. 2010.