In general, to append is to join or add on to the end of something. For example, an appendix is a section appended (added to the end) of a document.
(append '(1 2 3) '(a b) '() '(6)) ;Output: (1 2 3 a b 6)
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 elements. It may thus be a source of inefficiency if used injudiciously in code.
(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)))
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
++ symbols for nondestructive string/list/array concatenation.
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
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 xs
This is essentially a reimplementation of Haskell's
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 [1, 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 [2, 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.
In Python, use the list method "extend" or the infix operators + and += to append lists.
l = [1, 2] l.extend([3, 4, 5]) print l + [6, 7]
After executing this code, l is a list containing [1, 2, 3, 4, 5], while the output generated is the list [1, 2, 3, 4, 5, 6, 7].
Do not confuse with the list method "append", which adds a single element to a list:
l = [1, 2] l.append(3)
Here, the result is a list containing [1, 2, 3].
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:
append is a DOS command 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