lists
List processing functions.
This module contains functions for list processing.
Unless otherwise stated, all functions assume that position numbering starts at 1. That is, the first element of a list is at position 1.
Two terms T1
and T2
compare equal if
T1 == T2
evaluates to true
. They match
if T1 =:= T2
evaluates to true
.
Whenever an ordering function
F
is expected as argument, it is assumed that the
following properties hold of F
for all x, y, and z:
If x
F
y and yF
x, then x = y (F
is antisymmetric).If x
F
y and yF
z, then xF
z (F
is transitive).x
F
y or yF
x (F
is total).
An example of a typical ordering function is less than or equal
to: =</2
.
Functions
all(Pred, List) -> boolean()
Pred = fun((Elem :: T) -> boolean())
List = [T]
T = term()
Returns true
if
returns true
for all elements
in
, otherwise false
.
any(Pred, List) -> boolean()
Pred = fun((Elem :: T) -> boolean())
List = [T]
T = term()
Returns true
if
returns true
for at least one element
in
.
append(ListOfLists) -> List1
ListOfLists = [List]
List = List1 = [T]
T = term()
Returns a list in which all the sublists of
have been appended.
Example:
> lists:append([[1, 2, 3], [a, b], [4, 5, 6]]).
[1,2,3,a,b,4,5,6]
append(List1, List2) -> List3
List1 = List2 = List3 = [T]
T = term()
Returns a new list
, which is made from
the elements of
followed by the elements of
.
Example:
> lists:append("abc", "def").
"abcdef"
lists:append(A, B)
is equivalent to A ++ B
.
concat(Things) -> string()
Things = [Thing]
Thing = atom() | integer() | float() | string()
Concatenates the text representation of the elements of
. The elements of
can be atoms, integers, floats, or strings.
Example:
> lists:concat([doc, '/', file, '.', 3]).
"doc/file.3"
delete(Elem, List1) -> List2
Elem = T
List1 = List2 = [T]
T = term()
Returns a copy of
where the first element
matching
is deleted, if there is such an
element.
droplast(List) -> InitList
List = [T, ...]
InitList = [T]
T = term()
Drops the last element of a
. The list is to
be non-empty, otherwise the function crashes with a
function_clause
.
dropwhile(Pred, List1) -> List2
Pred = fun((Elem :: T) -> boolean())
List1 = List2 = [T]
T = term()
Drops elements
from
while
returns true
and
returns the remaining list.
duplicate(N, Elem) -> List
N = integer() >= 0
Elem = T
List = [T]
T = term()
Returns a list containing
copies of term
.
Example:
> lists:duplicate(5, xx).
[xx,xx,xx,xx,xx]
filter(Pred, List1) -> List2
Pred = fun((Elem :: T) -> boolean())
List1 = List2 = [T]
T = term()
is a list of all elements
in
for which
returns true
.
filtermap(Fun, List1) -> List2
Fun = fun((Elem) -> boolean() | {true, Value})
List1 = [Elem]
List2 = [Elem | Value]
Elem = Value = term()
Calls
on successive
elements Elem
of
.
must return either a Boolean or a tuple
{true,
. The function returns the list of
elements for which
returns a new value, where
a value of true
is synonymous with
{true,
.
That is, filtermap
behaves as if it had been defined as
follows:
filtermap(Fun, List1) -> lists:foldr(fun(Elem, Acc) -> case Fun(Elem) of false -> Acc; true -> [Elem|Acc]; {true,Value} -> [Value|Acc] end end, [], List1).
Example:
> lists:filtermap(fun(X) -> case X rem 2 of 0 -> {true, X div 2}; _ -> false end end, [1,2,3,4,5]).
[1,2]
flatlength(DeepList) -> integer() >= 0
DeepList = [term() | DeepList]
Equivalent to length(flatten(
, but
more efficient.
flatmap(Fun, List1) -> List2
Fun = fun((A) -> [B])
List1 = [A]
List2 = [B]
A = B = term()
Takes a function from
s to lists of
s, and a list of
s
(
) and produces a list of
s by applying the function to every element in
and appending the resulting lists.
That is, flatmap
behaves as if it had been defined as
follows:
flatmap(Fun, List1) -> append(map(Fun, List1)).
Example:
> lists:flatmap(fun(X)->[X,X] end, [a,b,c]).
[a,a,b,b,c,c]
flatten(DeepList) -> List
DeepList = [term() | DeepList]
List = [term()]
Returns a flattened version of
.
flatten(DeepList, Tail) -> List
DeepList = [term() | DeepList]
Tail = List = [term()]
Returns a flattened version of
with tail
appended.
foldl(Fun, Acc0, List) -> Acc1
Fun = fun((Elem :: T, AccIn) -> AccOut)
Acc0 = Acc1 = AccIn = AccOut = term()
List = [T]
T = term()
Calls
on successive elements A
of
, starting
with
.
must return a new accumulator, which is
passed to the next call. The function returns the final value of
the accumulator.
is returned if the list is
empty.
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(Fun, Acc0, List) -> Acc1
Fun = fun((Elem :: T, AccIn) -> AccOut)
Acc0 = Acc1 = AccIn = AccOut = term()
List = [T]
T = term()
Like foldl/3
, but the
list is traversed from right to left.
Example:
>P = fun(A, AccIn) -> io:format("~p ", [A]), AccIn end.
#Fun<erl_eval.12.2225172> >lists:foldl(P, void, [1,2,3]).
1 2 3 void >lists:foldr(P, void, [1,2,3]).
3 2 1 void
foldl/3
is tail recursive and is usually preferred to
foldr/3
.
join(Sep, List1) -> List2
Sep = T
List1 = List2 = [T]
T = term()
Inserts
between each element in
. Has no
effect on the empty list and on a singleton list. For example:
>lists:join(x, [a,b,c]).
[a,x,b,x,c] >lists:join(x, [a]).
[a] >lists:join(x, []).
[]
foreach(Fun, List) -> ok
Fun = fun((Elem :: T) -> term())
List = [T]
T = term()
Calls
for each element
in
. 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.
keydelete(Key, N, TupleList1) -> TupleList2
Key = term()
N = integer() >= 1
TupleList1 = TupleList2 = [Tuple]
Tuple = tuple()
Returns a copy of
where the first
occurrence of a tuple whose
th element compares
equal to
is deleted, if there is such a tuple.
keyfind(Key, N, TupleList) -> Tuple | false
Key = term()
N = integer() >= 1
TupleList = [Tuple]
Tuple = tuple()
Searches the list of tuples
for a
tuple whose
th element compares equal to
.
Returns
if such a tuple is found,
otherwise false
.
keymap(Fun, N, TupleList1) -> TupleList2
Fun = fun((Term1 :: term()) -> Term2 :: term())
N = integer() >= 1
TupleList1 = TupleList2 = [Tuple]
Tuple = tuple()
Returns a list of tuples where, for each tuple in
, the
th element
of the tuple
has been replaced with the result of calling
.
Examples:
>Fun = fun(Atom) -> atom_to_list(Atom) end.
#Fun<erl_eval.6.10732646> 2>lists:keymap(Fun, 2, [{name,jane,22},{name,lizzie,20},{name,lydia,15}]).
[{name,"jane",22},{name,"lizzie",20},{name,"lydia",15}]
keymember(Key, N, TupleList) -> boolean()
Key = term()
N = integer() >= 1
TupleList = [Tuple]
Tuple = tuple()
Returns true
if there is a tuple in
whose
th element
compares equal to
, otherwise false
.
keymerge(N, TupleList1, TupleList2) -> TupleList3
N = integer() >= 1
TupleList1 = [T1]
TupleList2 = [T2]
TupleList3 = [T1 | T2]
T1 = T2 = Tuple
Tuple = tuple()
Returns the sorted list formed by merging
and
.
The merge is performed on the
th element of each
tuple. Both
and
must be key-sorted before evaluating
this function. When two tuples compare equal, the tuple from
is picked before the tuple from
.
keyreplace(Key, N, TupleList1, NewTuple) -> TupleList2
Key = term()
N = integer() >= 1
TupleList1 = TupleList2 = [Tuple]
NewTuple = Tuple
Tuple = tuple()
Returns a copy of
where the first
occurrence of a T
tuple whose
th element
compares equal to
is replaced with
, if there is such a tuple T
.
keysearch(Key, N, TupleList) -> {value, Tuple} | false
Key = term()
N = integer() >= 1
TupleList = [Tuple]
Tuple = tuple()
Searches the list of tuples
for a
tuple whose
th element compares equal to
.
Returns {value,
if such a tuple is found,
otherwise false
.
Note!
This function is retained for backward compatibility. Function
keyfind/3
is usually more convenient.
keysort(N, TupleList1) -> TupleList2
N = integer() >= 1
TupleList1 = TupleList2 = [Tuple]
Tuple = tuple()
Returns a list containing the sorted elements of list
. Sorting is performed on the
th element of the tuples. The sort is stable.
keystore(Key, N, TupleList1, NewTuple) -> TupleList2
Key = term()
N = integer() >= 1
TupleList1 = [Tuple]
TupleList2 = [Tuple, ...]
NewTuple = Tuple
Tuple = tuple()
Returns a copy of
where the first
occurrence of a tuple T
whose
th element
compares equal to
is replaced with
, if there is such a tuple T
.
If there is no such tuple T
, a copy of
where
[
] has been appended to the end is
returned.
keytake(Key, N, TupleList1) -> {value, Tuple, TupleList2} | false
Key = term()
N = integer() >= 1
TupleList1 = TupleList2 = [tuple()]
Tuple = tuple()
Searches the list of tuples
for a
tuple whose
th element compares equal to
. Returns {value,
if such a tuple is found, otherwise
false
.
is a copy
of
where the first occurrence of
has been removed.
last(List) -> Last
List = [T, ...]
Last = T
T = term()
Returns the last element in
.
map(Fun, List1) -> List2
Fun = fun((A) -> B)
List1 = [A]
List2 = [B]
A = B = term()
Takes a function from
s to
s, and a list of
s and
produces a list of
s by applying
the function to every element in the list. This function is
used to obtain the return values. The evaluation order depends on
the implementation.
mapfoldl(Fun, Acc0, List1) -> {List2, Acc1}
Fun = fun((A, AccIn) -> {B, AccOut})
Acc0 = Acc1 = AccIn = AccOut = term()
List1 = [A]
List2 = [B]
A = B = term()
mapfoldr(Fun, Acc0, List1) -> {List2, Acc1}
Fun = fun((A, AccIn) -> {B, AccOut})
Acc0 = Acc1 = AccIn = AccOut = term()
List1 = [A]
List2 = [B]
A = B = term()
max(List) -> Max
List = [T, ...]
Max = T
T = term()
Returns the first element of
that compares
greater than or equal to all other elements of
.
member(Elem, List) -> boolean()
Elem = T
List = [T]
T = term()
Returns true
if
matches some element
of
, otherwise false
.
merge(ListOfLists) -> List1
ListOfLists = [List]
List = List1 = [T]
T = term()
Returns the sorted list formed by merging all the sublists of
. All sublists must be sorted before
evaluating this function. When two elements compare equal,
the element from the sublist with the lowest position in
is picked before the other
element.
merge(List1, List2) -> List3
List1 = [X]
List2 = [Y]
List3 = [X | Y]
X = Y = term()
Returns the sorted list formed by merging
and
. Both
and
must be
sorted before evaluating this function. When two elements
compare equal, the element from
is picked
before the element from
.
merge(Fun, List1, List2) -> List3
Fun = fun((A, B) -> boolean())
List1 = [A]
List2 = [B]
List3 = [A | B]
A = B = term()
Returns the sorted list formed by merging
and
. Both
and
must be sorted according to the ordering function
before evaluating this function.
is to return
true
if
compares less
than or equal to
in the ordering, otherwise
false
. When two elements compare equal, the element from
is picked before the element from
.
merge3(List1, List2, List3) -> List4
List1 = [X]
List2 = [Y]
List3 = [Z]
List4 = [X | Y | Z]
X = Y = Z = term()
Returns the sorted list formed by merging
,
, and
. All of
,
, and
must be sorted before evaluating this
function. When two elements compare equal, the element from
, if there is such an element,
is picked before the other element, otherwise the element
from
is picked before the element from
.
min(List) -> Min
List = [T, ...]
Min = T
T = term()
Returns the first element of
that compares
less than or equal to all other elements of
.
nth(N, List) -> Elem
N = integer() >= 1
List = [T, ...]
Elem = T
T = term()
Returns the
th element of
.
Example:
> lists:nth(3, [a, b, c, d, e]).
c
nthtail(N, List) -> Tail
N = integer() >= 0
List = [T, ...]
Tail = [T]
T = term()
Returns the
th tail of
,
that is, the sublist of
starting at
and continuing up to the end of the list.
Example
>lists:nthtail(3, [a, b, c, d, e]).
[d,e] >tl(tl(tl([a, b, c, d, e]))).
[d,e] >lists:nthtail(0, [a, b, c, d, e]).
[a,b,c,d,e] >lists:nthtail(5, [a, b, c, d, e]).
[]
partition(Pred, List) -> {Satisfying, NotSatisfying}
Pred = fun((Elem :: T) -> boolean())
List = Satisfying = NotSatisfying = [T]
T = term()
Partitions
into two lists, where the first
list contains all elements for which
returns true
,
and the second list contains all elements for which
returns false
.
Examples:
>lists:partition(fun(A) -> A rem 2 == 1 end, [1,2,3,4,5,6,7]).
{[1,3,5,7],[2,4,6]} >lists:partition(fun(A) -> is_atom(A) end, [a,b,1,c,d,2,3,4,e]).
{[a,b,c,d,e],[1,2,3,4]}
For a different way to partition a list, see
splitwith/2
.
prefix(List1, List2) -> boolean()
List1 = List2 = [T]
T = term()
Returns true
if
is a prefix of
, otherwise false
.
reverse(List1) -> List2
List1 = List2 = [T]
T = term()
Returns a list with the elements in
in reverse order.
reverse(List1, Tail) -> List2
List1 = [T]
Tail = term()
List2 = [T]
T = term()
Returns a list with the elements in
in reverse order, with tail
appended.
Example:
> lists:reverse([1, 2, 3, 4], [a, b, c]).
[4,3,2,1,a,b,c]
seq(From, To) -> Seq
From = To = integer()
Seq = [integer()]
seq(From, To, Incr) -> Seq
From = To = Incr = integer()
Seq = [integer()]
Returns a sequence of integers that starts with
and contains the successive results of
adding
to the previous element, until
is reached or passed (in the latter case,
is not an element of
the sequence).
defaults to 1.
Failures:
-
If
andTo <From -Incr
.Incr > 0 -
If
andTo >From -Incr
.Incr < 0 -
If
andIncr =:= 0
.From =/=To
The following equalities hold for all sequences:
length(lists:seq(From, To)) =:= To - From + 1 length(lists:seq(From, To, Incr)) =:= (To - From + Incr) div Incr
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, 0, 1).
[] >lists:seq(10, 6, 4).
[] >lists:seq(1, 1, 0).
[1]
sort(List1) -> List2
List1 = List2 = [T]
T = term()
Returns a list containing the sorted elements of
.
sort(Fun, List1) -> List2
Fun = fun((A :: T, B :: T) -> boolean())
List1 = List2 = [T]
T = term()
Returns a list containing the sorted elements of
, according to the ordering function
.
is to return true
if
compares less than or equal to
in the
ordering, otherwise false
.
split(N, List1) -> {List2, List3}
N = integer() >= 0
List1 = List2 = List3 = [T]
T = term()
Splits
into
and
.
contains the
first
elements and
the remaining elements (the
th tail).
splitwith(Pred, List) -> {List1, List2}
Pred = fun((T) -> boolean())
List = List1 = List2 = [T]
T = term()
Partitions
into two lists according to
. splitwith/2
behaves as if it is
defined as follows:
splitwith(Pred, List) -> {takewhile(Pred, List), dropwhile(Pred, List)}.
Examples:
>lists:splitwith(fun(A) -> A rem 2 == 1 end, [1,2,3,4,5,6,7]).
{[1],[2,3,4,5,6,7]} >lists:splitwith(fun(A) -> is_atom(A) end, [a,b,1,c,d,2,3,4,e]).
{[a,b],[1,c,d,2,3,4,e]}
For a different way to partition a list, see
partition/2
.
sublist(List1, Len) -> List2
List1 = List2 = [T]
Len = integer() >= 0
T = term()
Returns the sublist of
starting at
position 1 and with (maximum)
elements. It is
not an error for
to exceed the length of the
list, in that case the whole list is returned.
sublist(List1, Start, Len) -> List2
List1 = List2 = [T]
Start = integer() >= 1
Len = integer() >= 0
T = term()
Returns the sublist of
starting at
and with (maximum)
elements. It is not an error for
to exceed the length of
the list.
Examples:
>lists:sublist([1,2,3,4], 2, 2).
[2,3] >lists:sublist([1,2,3,4], 2, 5).
[2,3,4] >lists:sublist([1,2,3,4], 5, 2).
[]
subtract(List1, List2) -> List3
List1 = List2 = List3 = [T]
T = term()
Returns a new list
that is a copy of
, subjected to the following procedure:
for each element in
, its first occurrence
in
is deleted.
Example:
> lists:subtract("123212", "212").
"312".
lists:subtract(A, B)
is equivalent to A -- B
.
Warning!
The complexity of lists:subtract(A, B)
is proportional to
length(A)*length(B)
, meaning that it is very slow if both
A
and B
are long lists. (If both lists are long, it
is a much better choice to use ordered lists and
ordsets:subtract/2
.
suffix(List1, List2) -> boolean()
List1 = List2 = [T]
T = term()
Returns true
if
is a suffix of
, otherwise false
.
sum(List) -> number()
List = [number()]
Returns the sum of the elements in
.
takewhile(Pred, List1) -> List2
Pred = fun((Elem :: T) -> boolean())
List1 = List2 = [T]
T = term()
Takes elements
from
while
returns true
, that
is, the function returns the longest prefix of the list for which
all elements satisfy the predicate.
ukeymerge(N, TupleList1, TupleList2) -> TupleList3
N = integer() >= 1
TupleList1 = [T1]
TupleList2 = [T2]
TupleList3 = [T1 | T2]
T1 = T2 = Tuple
Tuple = tuple()
Returns the sorted list formed by merging
and
. The merge is performed on the
th element of each tuple. Both
and
must be key-sorted without duplicates before evaluating this function.
When two tuples compare equal, the tuple from
is picked and the
one from
is deleted.
ukeysort(N, TupleList1) -> TupleList2
N = integer() >= 1
TupleList1 = TupleList2 = [Tuple]
Tuple = tuple()
Returns a list containing the sorted elements of list
where all except the first tuple of
the tuples comparing equal have been deleted. Sorting is
performed on the
th element of the tuples.
umerge(ListOfLists) -> List1
ListOfLists = [List]
List = List1 = [T]
T = term()
Returns the sorted list formed by merging all the sublists
of
. All sublists must be sorted and
contain no duplicates before evaluating this function.
When two elements compare equal, the element from the sublist
with the lowest position in
is
picked and the other is deleted.
umerge(List1, List2) -> List3
List1 = [X]
List2 = [Y]
List3 = [X | Y]
X = Y = term()
Returns the sorted list formed by merging
and
. Both
and
must be
sorted and contain no duplicates before evaluating this
function. When two elements compare equal, the element from
is picked and the one from
is deleted.
umerge(Fun, List1, List2) -> List3
Fun = fun((A, B) -> boolean())
List1 = [A]
List2 = [B]
List3 = [A | B]
A = B = term()
Returns the sorted list formed by merging
and
. Both
and
must be sorted according to the ordering function
Fun
and contain no duplicates before evaluating this function.
is to return
true
if
compares less than or equal to
in the ordering, otherwise false
. When
two elements compare equal, the element from
is picked and the one from
is deleted.
umerge3(List1, List2, List3) -> List4
List1 = [X]
List2 = [Y]
List3 = [Z]
List4 = [X | Y | Z]
X = Y = Z = term()
Returns the sorted list formed by merging
,
, and
. All of
,
, and
must be sorted and contain no
duplicates before evaluating this function. When two
elements compare equal, the element from
is
picked if there is such an element, otherwise the element from
is picked, and the other is deleted.
unzip(List1) -> {List2, List3}
List1 = [{A, B}]
List2 = [A]
List3 = [B]
A = B = term()
"Unzips" a list of two-tuples into two lists, where the first list contains the first element of each tuple, and the second list contains the second element of each tuple.
unzip3(List1) -> {List2, List3, List4}
List1 = [{A, B, C}]
List2 = [A]
List3 = [B]
List4 = [C]
A = B = C = term()
"Unzips" a list of three-tuples into three lists, where the first list contains the first element of each tuple, the second list contains the second element of each tuple, and the third list contains the third element of each tuple.
usort(List1) -> List2
List1 = List2 = [T]
T = term()
Returns a list containing the sorted elements of
where all except the first element of the
elements comparing equal have been deleted.
usort(Fun, List1) -> List2
Fun = fun((T, T) -> boolean())
List1 = List2 = [T]
T = term()
Returns a list containing the sorted elements of
where all except the first element of the
elements comparing equal according to the ordering function
have been deleted.
is to return
true
if A
compares less than or equal to
B
in the ordering, otherwise false
.
zip(List1, List2) -> List3
List1 = [A]
List2 = [B]
List3 = [{A, B}]
A = B = term()
"Zips" two lists of equal length into one list of two-tuples, where the first element of each tuple is taken from the first list and the second element is taken from the corresponding element in the second list.
zip3(List1, List2, List3) -> List4
List1 = [A]
List2 = [B]
List3 = [C]
List4 = [{A, B, C}]
A = B = C = term()
"Zips" three lists of equal length into one list of three-tuples, where the first element of each tuple is taken from the first list, the second element is taken from the corresponding element in the second list, and the third element is taken from the corresponding element in the third list.
zipwith(Combine, List1, List2) -> List3
Combine = fun((X, Y) -> T)
List1 = [X]
List2 = [Y]
List3 = [T]
X = Y = T = term()
Combines the elements of two lists of equal length into one list.
For each pair
of list elements
from the two lists, the element in the result list is
.
zipwith(fun(X, Y) -> {X,Y} end, List1, List2)
is
equivalent to zip(List1, List2)
.
Example:
> lists:zipwith(fun(X, Y) -> X+Y end, [1,2,3], [4,5,6]).
[5,7,9]
zipwith3(Combine, List1, List2, List3) -> List4
Combine = fun((X, Y, Z) -> T)
List1 = [X]
List2 = [Y]
List3 = [Z]
List4 = [T]
X = Y = Z = T = term()
Combines the elements of three lists of equal length into one
list. For each triple
of list elements from the three lists, the element
in the result list is
.
zipwith3(fun(X, Y, Z) -> {X,Y,Z} end, List1, List2, List3)
is
equivalent to zip3(List1, List2, List3)
.
Examples:
>lists:zipwith3(fun(X, Y, Z) -> X+Y+Z end, [1,2,3], [4,5,6], [7,8,9]).
[12,15,18] >lists:zipwith3(fun(X, Y, Z) -> [X,Y,Z] end, [a,b,c], [x,y,z], [1,2,3]).
[[a,x,1],[b,y,2],[c,z,3]]