List Operations ++ and - -

++ and -- are infix operators for list addition and subtraction.

A ++ B adds (that is, appends) A and B.

A -- B subtracts the list B from the list A. Subtraction means that every element in B is removed from A. Note that if some symbol X occurs only K times in B, then only the first K occurrences of X in A will be removed.

Here are some examples:

 
1>​ [1,2,3] ++ [4,5,6].
 
[1,2,3,4,5,6]
 
2>​ [a,b,c,1,d,e,1,x,y,1] -- [1].
 
[a,b,c,d,e,1,x,y,1]
 
3>​ [a,b,c,1,d,e,1,x,y,1] -- [1,1].
 
[a,b,c,d,e,x,y,1]
 
4>​ [a,b,c,1,d,e,1,x,y,1] -- [1,1,1].
 
[a,b,c,d,e,x,y]
 
5>​ [a,b,c,1,d,e,1,x,y,1] -- [1,1,1,1].
 
[a,b,c,d,e,x,y]

++ can also be used in patterns. When matching strings, we can write patterns such as the following:

 

Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.