This module contains functions for list processing. The functions are organized in two groups: those in the first group perform a particular operation on one ore several lists, whereas those in the second group perform use a user-defined function (given as the first argument) to perform an operation on one list.
ListOfLists = [List]List = List1 = [term()]Returns a list in which all the sub-lists of ListOfLists
have been appended. For example:
> lists:append([[1, 2, 3], [a, b], [4, 5, 6]]). [1, 2, 3, a, b, 4, 5, 6]
List1 = List2 = List3 = [term()]Returns a new list List3 which is made from the
elements of List1 followed by the elements of List2.
For example:
> lists:append("abc", "def").
"abcdef".lists:append(A,B) is equivalent to A ++ B.
Things = [Thing]Thing = atom() | integer() | float() | string()Concatenates the ASCII list representation of the elements
of Things. The elements of Things can be atoms,
integers, floats or strings.
> lists:concat([doc, '/', file, '.', 3]). "doc/file.3"
delete(Element, List1) -> List2
List1 = list2 = [Element]Element = term()Returns a copy of List1, but the first occurrence of
Element, if present, is deleted.
N = int()List = [Element]Element = term()Returns a list which contains N copies of the term
Element.
![]() |
|
> lists:duplicate(5, xx). [xx, xx, xx, xx, xx]
Equivalent to length(flatten(DeepList)), but more
efficient.
DeepList = [term() | DeepList]Returns a flattened version of DeepList.
flatten(DeepList, Tail) -> List
DeepList = [term() | DeepList]Tail = [term()]Returns a flattened version of DeepList with the
tail Tail appended.
keydelete(Key, N, TupleList1) -> TupleList2
TupleList1 = TupleList2 = [tuple()]N = int()Key = term()Returns a copy of TupleList1 where the first
occurrence of a tuple whose Nth element is Key is deleted, if present.
keymember(Key, N, TupleList) -> bool()
TupleList = [tuple()]N = int()Key = term()Searches the list of tuples TupleList for a tuple
whose Nth element is Key.
N = int()List1 = List2 = [tuple()]Returns the sorted list formed by merging the List1
and List2. The merge is performed on the Nth
element of each tuple. Both List1 and List2
must be key-sorted prior to evaluating this function; otherwise
the order of the elements in the result will be undefined.
When elements in the input lists compare equal, elements from
List1 are picked before elements from List2.
keyreplace(Key, N, TupleList1, NewTuple) -> TupleList2
Key = term()N = int()TupleList1 = TupleList2 = [tuple()]NewTuple = tuple()Returns a list of tuples. In this list, a tuple is replaced
by the tuple NewTuple. This tuple is the first tuple
in the list where the element number N is equal to
Key.
keysearch(Key, N, TupleList) -> Result
TupleList = [tuple()]N = int()Key = term()Result = {value, tuple()} | falseSearches the list of the tuples TupleList for
Tuple whose Nth element is Key.
Returns {value, Tuple} if such a tuple is found, or
false if no such tuple is found.
N = int()List1 = List2 = [tuple()]Returns a list containing the sorted elements of
List1. TupleList1 must be a list of tuples,
and the sort is performed on the Nth element of the
tuple. The sort is stable.
List = [Element]Element = term()Returns the last element in List.
List = [Element]Element = Max = term()Returns the maximum element of List.
member(Element, List) -> bool()
List = [Element]Element = term()Returns true if Element is contained in the
list List, otherwise false.
List1 = List2 = List3 = [term()]Returns the sorted list formed by merging List1 and
List2. Both List1 and List2 must be
sorted prior to evaluating this function.
merge(Fun, List1, List2) -> List
List = List1 = List2 = [Element]Fun = fun(Element, Element) -> bool()Element = term()Returns the sorted list formed by merging List1 and
List2. Both List1 and List2 must be
sorted prior to evaluating this function, according to the
ordering function Fun. Fun(A,B) should
return true if A comes before B in the ordering,
false otherwise.
List = [Element]Element = Max = term()Returns the minimum element of List.
N = int()List = [Element]Element = term()Returns the Nth element of the List. For
example:
> lists:nth(3, [a, b, c, d, e]). c
N = int()List1 = List2 = [Alpha]Returns the Nth tail of List. For example:
> lists:nthtail(3, [a, b, c, d, e]). [d, e]
prefix(List1, List2) -> bool()
List1 = List2 = [term()]Returns true if List1 is a prefix of
List2, otherwise false.
List1 = List2 = [term()]Returns a list with the top level elements in List1
in reverse order.
reverse(List1, List2) -> List3
List1 = List2 = List3 = [term()]Returns a list where List1 has been reversed and
appended to the beginning of List2. Equivalent to
reverse(List1) ++ List2. For example:
> lists:reverse([1, 2, 3, 4], [a, b, c]). [4, 3, 2, 1, a, b, c]
seq(From, To) -> [int()]
seq(From, To, Incr) -> [int()]
From = To = Incr = int()Returns a sequence of integers which starts with From
and contains the successive results of adding Incr to the
previous element, until To has been reached or passed (in
the latter case, To is not an element of the sequence).
If To-From has a different sign from Incr, or if
Incr = 0 and From is different from To, an
error is signalled (this implies that the result is never an empty
list - the first element is always From).
seq(From, To) is equivalent to seq(From, To, 1).
Examples:
> lists:seq(1, 10). [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > lists:seq(1, 20, 3). [1, 4, 7, 10, 13, 16, 19] > lists:seq(1, 1, 0). [1]
List1 = List2 = [term()]Returns a list which contains the sorted elements of List1.
List1 = List2 = [Element]Fun = fun(Element, Element) -> bool()Element = term()Returns a list which contains the sorted elements of List1,
according to the ordering function Fun. Fun(A,B) should
return true if A comes before B in the ordering,
false otherwise.
List1 = List2 = [term()]N = int()Returns the first N elements of List.
It is not an error for N to exceed the length of the
list when List is a proper list - in that case the whole
list is returned.
sublist(List1, Start, Length) -> List2
List1 = List2 = [term()]Start = End = int()Returns the sub-list of List starting at Start
of length Length. Terminates with a runtime failure if
Start is not in List, but a sub-list of
a length less than Length is accepted. Start is
considered to be in List if Start >= 1 and
Start <= length(List)+1.
subtract(List1, List2) -> List3
List1 = List2 = List3 = [term()]Returns a new list List3 which is a copy of
List1, subjected to the following procedure: for each element in
List2, its first occurrence in List1 is removed.
For example:
> lists:subtract("123212", "212").
"312".lists:subtract(A,B) is equivalent to A -- B.
suffix(List1, List2) -> bool()
Returns true if List1 is a suffix of
List2, otherwise false.
List = [number()]Returns the sum of the elements in List.
Pred = fun(A) -> bool()List = [A]Returns true if all elements X in List satisfy
Pred(X).
Pred = fun(Element) -> bool()List = [Element]Element = term()Returns true if any of the elements in List
satisfies Pred.
dropwhile(Pred, List1) -> List2
Pred = fun(A) -> bool()List1 = List2 = [A]Drops elements X from List1 while
Pred(X) is true and returns the remaining list.
Pred = fun(A) -> bool()List1 = List2 = [A]List2 is a list of all elements X in
List1 for which Pred(X) is true.
flatmap(Function, List1) -> Element
Function = fun(A) -> BList1 = [A]Element = [B]flatmap behaves as if it had been defined as follows:
flatmap(Func, List) ->
append(map(Func, List))
foldl(Function, Acc0, List) -> Acc1
Function = fun(A, AccIn) -> AccOutList = [A]Acc0 = Acc1 = AccIn = AccOut = term()Acc0 is returned if the list is empty.
For example:
> lists:foldl(fun(X, Sum) -> X + Sum end, 0, [1,2,3,4,5]). 15 > lists:foldl(fun(X, Prod) -> X * Prod end, 1, [1,2,3,4,5]). 120
foldr(Function, Acc0, List) -> Acc1
Function = fun(A, AccIn) -> AccOutList = [A]Acc0 = Acc1 = AccIn = AccOut = term()Calls Function on successive elements of List
together with an extra argument Acc (short for
accumulator). Function must return a new accumulator
which is passed to the next call. Acc0 is returned if the list is empty.
foldr differs from
foldl in that the list is traversed "bottom up"
instead of "top down". foldl is tail recursive and
would usually be preferred to foldr.
foreach(Function, List) -> void()
Function = fun(A) -> void()List = [A]Applies the function Function to each of the
elements in List. This function is used for its side
effects and the evaluation order is defined to be the same
as the order of the elements in the list.
Func = fun(A) -> BList1 = [A]List2 = [B]map takes a function from As to Bs,
and a list of As and produces a list of Bs
by applying the function to every element in the list. This
function is used to obtain the return values. The
evaluation order is implementation dependent.
mapfoldl(Function, Acc0, List1) -> {List2, Acc}
Function = fun(A, AccIn) -> {B, AccOut}Acc0 = Acc1 = AccIn = AccOut = term()List1 = [A]List2 = [B]mapfold combines the operations of map and
foldl into one pass. For example, we could sum the
elements in a list and double them at the same time:
> lists:mapfoldl(fun(X, Sum) -> {2*X, X+Sum} end,
0, [1,2,3,4,5]).
{[2,4,6,8,10],15}
mapfoldr(Function, Acc0, List1) -> {List2, Acc}
Function = fun(A, AccIn) -> {B, AccOut}Acc0 = Acc1 = AccIn = AccOut = term()List1 = [A]List2 = [B]mapfold combines the operations of map and
foldr into one pass.
splitwith(Pred, List) -> {List1, List2}
Pred = fun(A) -> bool()List = List1 = List2 = [A]Partitions Lists into List1 and List2
according to Pred.
splitwith behaves as if it had been defined as follows:
splitwidth(Pred, List) ->
{takewhile(Pred, List), dropwhile(Pred, List)}.
Note also that List == List1 ++ List2.
takewhile(Pred, List1) -> List2
Pred = fun(A) -> bool()List1 = List2 = [A]Returns the longest prefix of List1 for which all
elements X in List1 satisfy Pred(X).
Some of the exported functions in lists.erl are not documented. In
particular, this applies to a number of maps and
folds which have an extra argument for environment
passing. These functions are no longer needed because Erlang 4.4 and
later releases have Funs.
![]() |
Any undocumented functions in lists should not be used. |