View Source lists (stdlib v6.0)
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
.
Summary
Functions
Returns true
if Pred(Elem)
returns true
for all elements Elem
in List
,
otherwise false
. The Pred
function must return a boolean.
Returns true
if Pred(Elem)
returns true
for at least one element Elem
in
List
. The Pred
function must return a boolean.
Returns a list in which all the sublists of ListOfLists
have been appended.
Returns a new list List3
, which is made from the elements of List1
followed
by the elements of List2
.
Concatenates the text representation of the elements of Things
. The elements
of Things
can be atoms, integers, floats, or strings.
Returns a copy of List1
where the first element matching Elem
is deleted, if
there is such an element.
Drops the last element of a List
. The list is to be non-empty, otherwise the
function crashes with a function_clause
.
Drops elements Elem
from List1
while Pred(Elem)
returns true
and returns
the remaining list. The Pred
function must return a boolean.
Returns a list containing N
copies of term Elem
.
Equivalent to enumerate(1, 1, List1)
Equivalent to enumerate(Index, 1, List1)
Returns List1
with each element H
replaced by a tuple of form {I, H}
where
I
is the position of H
in List1
. The enumeration starts with Index
and
increases by Step
in each step.
List2
is a list of all elements Elem
in List1
for which Pred(Elem)
returns true
. The Pred
function must return a boolean.
Calls Fun(Elem)
on successive elements Elem
of List1
in order to update or
remove elements from List1
.
Equivalent to length(flatten(DeepList))
, but more efficient.
Takes a function from A
s to lists of B
s, and a list of A
s (List1
) and
produces a list of B
s by applying the function to every element in List1
and
appending the resulting lists.
Returns a flattened version of DeepList
.
Returns a flattened version of DeepList
with tail Tail
appended.
Calls Fun(Elem, AccIn)
on successive elements A
of List
, starting with
AccIn == Acc0
. Fun/2
must return a new accumulator, which is passed to the
next call. The function returns the final value of the accumulator. Acc0
is
returned if the list is empty.
Like foldl/3
, but the list is traversed from right to left.
Calls Fun(Elem)
for each element Elem
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.
Inserts Sep
between each element in List1
. Has no effect on the empty list
and on a singleton list. For example
Returns a copy of TupleList1
where the first occurrence of a tuple whose N
th
element compares equal to Key
is deleted, if there is such a tuple.
Searches the list of tuples TupleList
for a tuple whose N
th element compares
equal to Key
. Returns Tuple
if such a tuple is found, otherwise false
.
Returns a list of tuples where, for each tuple in TupleList1
, the N
th
element Term1
of the tuple has been replaced with the result of calling
Fun(Term1)
.
Returns true
if there is a tuple in TupleList
whose N
th element compares
equal to Key
, otherwise false
.
Returns the sorted list formed by merging TupleList1
and TupleList2
.
Returns a copy of TupleList1
where the first occurrence of a T
tuple whose
N
th element compares equal to Key
is replaced with NewTuple
, if there is
such a tuple T
.
Searches the list of tuples TupleList
for a tuple whose N
th element compares
equal to Key
. Returns {value, Tuple}
if such a tuple is found, otherwise
false
.
Returns a list containing the sorted elements of list TupleList1
. Sorting is
performed on the N
th element of the tuples. The sort is stable.
Returns a copy of TupleList1
where the first occurrence of a tuple T
whose
N
th element compares equal to Key
is replaced with NewTuple
, if there is
such a tuple T
. If there is no such tuple T
, a copy of TupleList1
where
[NewTuple
] has been appended to the end is returned.
Searches the list of tuples TupleList1
for a tuple whose N
th element
compares equal to Key
. Returns {value, Tuple, TupleList2}
if such a tuple is
found, otherwise false
. TupleList2
is a copy of TupleList1
where the first
occurrence of Tuple
has been removed.
Returns the last element in List
.
Takes a function from A
s to B
s, and a list of A
s and produces a list of
B
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.
Returns the first element of List
that compares greater than or equal to all
other elements of List
.
Returns true
if Elem
matches some element of List
, otherwise false
.
Returns the sorted list formed by merging List1
, List2
, and List3
. All of
List1
, List2
, and List3
must be sorted before evaluating this function.
Returns the sorted list formed by merging all the sublists of ListOfLists
. All
sublists must be sorted before evaluating this function.
Returns the sorted list formed by merging List1
and List2
. Both List1
and
List2
must be sorted before evaluating this function.
Returns the sorted list formed by merging List1
and List2
. Both List1
and
List2
must be sorted according to the
ordering function Fun
before evaluating this
function.
Returns the first element of List
that compares less than or equal to all
other elements of List
.
Returns the N
th element of List
.
Returns the N
th tail of List
, that is, the sublist of List
starting at
N+1
and continuing up to the end of the list.
Partitions List
into two lists, where the first list contains all elements for
which Pred(Elem)
returns true
, and the second list contains all elements for
which Pred(Elem)
returns false
.
Returns true
if List1
is a prefix of List2
, otherwise false
.
Returns a list with the elements in List1
in reverse order.
Returns a list with the elements in List1
in reverse order, with tail Tail
appended.
If there is a Value
in List
such that Pred(Value)
returns true
, returns
{value, Value}
for the first such Value
, otherwise returns false
. The
Pred
function must return a boolean.
Equivalent to seq(From, To, 1)
Returns a sequence of integers that starts with From
and contains the
successive results of adding Incr
to the previous element, until To
is
reached or passed (in the latter case, To
is not an element of the sequence).
Incr
defaults to 1.
Returns a list containing the sorted elements of List1
.
Returns a list containing the sorted elements of List1
, according to the
ordering function Fun
. Fun(A, B)
is to return
true
if A
compares less than or equal to B
in the ordering, otherwise
false
.
Splits List1
into List2
and List3
. List2
contains the first N
elements
and List3
the remaining elements (the N
th tail).
Partitions List
into two lists according to Pred
.
splitwith/2
behaves as if it is defined as follows
Returns the sublist of List1
starting at position 1 and with (maximum) Len
elements. It is not an error for Len
to exceed the length of the list, in that
case the whole list is returned.
Returns the sublist of List1
starting at Start
and with (maximum) Len
elements. It is not an error for Start+Len
to exceed the length of the list.
Returns a new list List3
that is a copy of List1
, subjected to the following
procedure: for each element in List2
, its first occurrence in List1
is
deleted.
Returns true
if List1
is a suffix of List2
, otherwise false
.
Returns the sum of the elements in List
.
Takes elements Elem
from List1
while Pred(Elem)
returns true
, that is,
the function returns the longest prefix of the list for which all elements
satisfy the predicate. The Pred
function must return a boolean.
Returns the sorted list formed by merging TupleList1
and TupleList2
. The
merge is performed on the N
th element of each tuple. Both TupleList1
and
TupleList2
must be key-sorted without duplicates before evaluating this
function.
Returns a list containing the sorted elements of list TupleList1
where all
except the first tuple of the tuples comparing equal have been deleted. Sorting
is performed on the N
th element of the tuples.
Returns the sorted list formed by merging List1
, List2
, and List3
. All of
List1
, List2
, and List3
must be sorted and contain no duplicates before
evaluating this function.
Returns the sorted list formed by merging all the sublists of ListOfLists
. All
sublists must be sorted and contain no duplicates before evaluating this
function.
Returns the sorted list formed by merging List1
and List2
. Both List1
and
List2
must be sorted and contain no duplicates before evaluating this
function.
Returns the sorted list formed by merging List1
and List2
. Both List1
and
List2
must be sorted according to the
ordering function Fun
and contain no duplicates
before evaluating this function.
Returns a list containing the elements of List1
with duplicated elements
removed (preserving the order of the elements). The first occurrence of each
element is kept.
Returns a list containing the elements of List1
without the elements for which
Fun
returned duplicate values (preserving the order of the elements). The
first occurrence of each element is kept.
"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.
"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.
Returns a list containing the sorted elements of List1
where all except the
first element of the elements comparing equal have been deleted.
Returns a list containing the sorted elements of List1
where all except the
first element of the elements comparing equal according to the
ordering function Fun
have been deleted.
Fun(A, B)
is to return true
if A
compares less than or equal to B
in the
ordering, otherwise false
.
Equivalent to zip3(List1, List2, List3, fail)
"Zips" three lists 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.
Equivalent to zip(List1, List2, fail)
"Zips" two lists 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.
Combines the elements of three lists into one list. For each triple X, Y, Z
of
list elements from the three lists, the element in the result list is
Combine(X, Y, Z)
.
Combines the elements of two lists into one list. For each pair X, Y
of list
elements from the two lists, the element in the result list is Combine(X, Y)
.
Functions
-spec all(Pred, List) -> boolean() when Pred :: fun((Elem :: T) -> boolean()), List :: [T], T :: term().
Returns true
if Pred(Elem)
returns true
for all elements Elem
in List
,
otherwise false
. The Pred
function must return a boolean.
-spec any(Pred, List) -> boolean() when Pred :: fun((Elem :: T) -> boolean()), List :: [T], T :: term().
Returns true
if Pred(Elem)
returns true
for at least one element Elem
in
List
. The Pred
function must return a boolean.
-spec append(ListOfLists) -> List1 when ListOfLists :: [List], List :: [T], List1 :: [T], T :: term().
Returns a list in which all the sublists of ListOfLists
have been appended.
Example:
> lists:append([[1, 2, 3], [a, b], [4, 5, 6]]).
[1,2,3,a,b,4,5,6]
-spec append(List1, List2) -> List3 when List1 :: [T], List2 :: [T], List3 :: [T], T :: term().
Returns a new list List3
, which is made from the elements of List1
followed
by the elements of List2
.
Example:
> lists:append("abc", "def").
"abcdef"
lists:append(A, B)
is equivalent to A ++ B
.
-spec concat(Things) -> string() when Things :: [Thing], Thing :: atom() | integer() | float() | string().
Concatenates the text representation of the elements of Things
. The elements
of Things
can be atoms, integers, floats, or strings.
Example:
> lists:concat([doc, '/', file, '.', 3]).
"doc/file.3"
-spec delete(Elem, List1) -> List2 when Elem :: T, List1 :: [T], List2 :: [T], T :: term().
Returns a copy of List1
where the first element matching Elem
is deleted, if
there is such an element.
-spec droplast(List) -> InitList when List :: [T, ...], InitList :: [T], T :: term().
Drops the last element of a List
. The list is to be non-empty, otherwise the
function crashes with a function_clause
.
-spec dropwhile(Pred, List1) -> List2 when Pred :: fun((Elem :: T) -> boolean()), List1 :: [T], List2 :: [T], T :: term().
Drops elements Elem
from List1
while Pred(Elem)
returns true
and returns
the remaining list. The Pred
function must return a boolean.
-spec duplicate(N, Elem) -> List when N :: non_neg_integer(), Elem :: T, List :: [T], T :: term().
Returns a list containing N
copies of term Elem
.
Example:
> lists:duplicate(5, xx).
[xx,xx,xx,xx,xx]
-spec enumerate(List1) -> List2 when List1 :: [T], List2 :: [{Index, T}], Index :: integer(), T :: term().
Equivalent to enumerate(1, 1, List1)
-spec enumerate(Index, List1) -> List2 when List1 :: [T], List2 :: [{Index, T}], Index :: integer(), T :: term().
Equivalent to enumerate(Index, 1, List1)
-spec enumerate(Index, Step, List1) -> List2 when List1 :: [T], List2 :: [{Index, T}], Index :: integer(), Step :: integer(), T :: term().
Returns List1
with each element H
replaced by a tuple of form {I, H}
where
I
is the position of H
in List1
. The enumeration starts with Index
and
increases by Step
in each step.
That is, enumerate/3
behaves as if it had been defined as
follows:
enumerate(I, S, List) ->
{List1, _ } = lists:mapfoldl(fun(T, Acc) -> {{Acc, T}, Acc+S} end, I, List),
List1.
The default values for Index
and Step
are both 1
.
Examples:
> lists:enumerate([a,b,c]).
[{1,a},{2,b},{3,c}]
> lists:enumerate(10, [a,b,c]).
[{10,a},{11,b},{12,c}]
> lists:enumerate(0, -2, [a,b,c]).
[{0,a},{-2,b},{-4,c}]
-spec filter(Pred, List1) -> List2 when Pred :: fun((Elem :: T) -> boolean()), List1 :: [T], List2 :: [T], T :: term().
List2
is a list of all elements Elem
in List1
for which Pred(Elem)
returns true
. The Pred
function must return a boolean.
-spec filtermap(Fun, List1) -> List2 when Fun :: fun((Elem) -> boolean() | {true, Value}), List1 :: [Elem], List2 :: [Elem | Value], Elem :: term(), Value :: term().
Calls Fun(Elem)
on successive elements Elem
of List1
in order to update or
remove elements from List1
.
Fun/1
must return either a Boolean or a tuple {true, Value}
. The function
returns the list of elements for which Fun
returns a new value, where a value
of true
is synonymous with {true, Elem}
.
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]
-spec flatlength(DeepList) -> non_neg_integer() when DeepList :: [term() | DeepList].
Equivalent to length(flatten(DeepList))
, but more efficient.
-spec flatmap(Fun, List1) -> List2 when Fun :: fun((A) -> [B]), List1 :: [A], List2 :: [B], A :: term(), B :: term().
Takes a function from A
s to lists of B
s, and a list of A
s (List1
) and
produces a list of B
s by applying the function to every element in List1
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]
Returns a flattened version of DeepList
.
-spec flatten(DeepList, Tail) -> List when DeepList :: [term() | DeepList], Tail :: [term()], List :: [term()].
Returns a flattened version of DeepList
with tail Tail
appended.
-spec foldl(Fun, Acc0, List) -> Acc1 when Fun :: fun((Elem :: T, AccIn) -> AccOut), Acc0 :: term(), Acc1 :: term(), AccIn :: term(), AccOut :: term(), List :: [T], T :: term().
Calls Fun(Elem, AccIn)
on successive elements A
of List
, starting with
AccIn == Acc0
. Fun/2
must return a new accumulator, which is passed to the
next call. The function returns the final value of the accumulator. Acc0
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
-spec foldr(Fun, Acc0, List) -> Acc1 when Fun :: fun((Elem :: T, AccIn) -> AccOut), Acc0 :: term(), Acc1 :: term(), AccIn :: term(), 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
.
Calls Fun(Elem)
for each element Elem
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.
-spec join(Sep, List1) -> List2 when Sep :: T, List1 :: [T], List2 :: [T], T :: term().
Inserts Sep
between each element in List1
. 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, []).
[]
-spec keydelete(Key, N, TupleList1) -> TupleList2 when Key :: term(), N :: pos_integer(), TupleList1 :: [Tuple], TupleList2 :: [Tuple], Tuple :: tuple().
Returns a copy of TupleList1
where the first occurrence of a tuple whose N
th
element compares equal to Key
is deleted, if there is such a tuple.
-spec keyfind(Key, N, TupleList) -> Tuple | false when Key :: term(), N :: pos_integer(), TupleList :: [Tuple], Tuple :: tuple().
Searches the list of tuples TupleList
for a tuple whose N
th element compares
equal to Key
. Returns Tuple
if such a tuple is found, otherwise false
.
-spec keymap(Fun, N, TupleList1) -> TupleList2 when Fun :: fun((Term1 :: term()) -> Term2 :: term()), N :: pos_integer(), TupleList1 :: [Tuple], TupleList2 :: [Tuple], Tuple :: tuple().
Returns a list of tuples where, for each tuple in TupleList1
, the N
th
element Term1
of the tuple has been replaced with the result of calling
Fun(Term1)
.
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}]
-spec keymember(Key, N, TupleList) -> boolean() when Key :: term(), N :: pos_integer(), TupleList :: [Tuple], Tuple :: tuple().
Returns true
if there is a tuple in TupleList
whose N
th element compares
equal to Key
, otherwise false
.
-spec keymerge(N, TupleList1, TupleList2) -> TupleList3 when N :: pos_integer(), TupleList1 :: [T1], TupleList2 :: [T2], TupleList3 :: [T1 | T2], T1 :: Tuple, T2 :: Tuple, Tuple :: tuple().
Returns the sorted list formed by merging TupleList1
and TupleList2
.
The merge is performed on the N
th element of each tuple. Both TupleList1
and
TupleList2
must be key-sorted before evaluating this function. When two tuples
compare equal, the tuple from TupleList1
is picked before the tuple from
TupleList2
.
-spec keyreplace(Key, N, TupleList1, NewTuple) -> TupleList2 when Key :: term(), N :: pos_integer(), TupleList1 :: [Tuple], TupleList2 :: [Tuple], NewTuple :: Tuple, Tuple :: tuple().
Returns a copy of TupleList1
where the first occurrence of a T
tuple whose
N
th element compares equal to Key
is replaced with NewTuple
, if there is
such a tuple T
.
-spec keysearch(Key, N, TupleList) -> {value, Tuple} | false when Key :: term(), N :: pos_integer(), TupleList :: [Tuple], Tuple :: tuple().
Searches the list of tuples TupleList
for a tuple whose N
th element compares
equal to Key
. Returns {value, Tuple}
if such a tuple is found, otherwise
false
.
Note
This function is retained for backward compatibility. Function
keyfind/3
is usually more convenient.
-spec keysort(N, TupleList1) -> TupleList2 when N :: pos_integer(), TupleList1 :: [Tuple], TupleList2 :: [Tuple], Tuple :: tuple().
Returns a list containing the sorted elements of list TupleList1
. Sorting is
performed on the N
th element of the tuples. The sort is stable.
-spec keystore(Key, N, TupleList1, NewTuple) -> TupleList2 when Key :: term(), N :: pos_integer(), TupleList1 :: [Tuple], TupleList2 :: [Tuple, ...], NewTuple :: Tuple, Tuple :: tuple().
Returns a copy of TupleList1
where the first occurrence of a tuple T
whose
N
th element compares equal to Key
is replaced with NewTuple
, if there is
such a tuple T
. If there is no such tuple T
, a copy of TupleList1
where
[NewTuple
] has been appended to the end is returned.
-spec keytake(Key, N, TupleList1) -> {value, Tuple, TupleList2} | false when Key :: term(), N :: pos_integer(), TupleList1 :: [tuple()], TupleList2 :: [tuple()], Tuple :: tuple().
Searches the list of tuples TupleList1
for a tuple whose N
th element
compares equal to Key
. Returns {value, Tuple, TupleList2}
if such a tuple is
found, otherwise false
. TupleList2
is a copy of TupleList1
where the first
occurrence of Tuple
has been removed.
-spec last(List) -> Last when List :: [T, ...], Last :: T, T :: term().
Returns the last element in List
.
-spec map(Fun, List1) -> List2 when Fun :: fun((A) -> B), List1 :: [A], List2 :: [B], A :: term(), B :: term().
Takes a function from A
s to B
s, and a list of A
s and produces a list of
B
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.
-spec mapfoldl(Fun, Acc0, List1) -> {List2, Acc1} when Fun :: fun((A, AccIn) -> {B, AccOut}), Acc0 :: term(), Acc1 :: term(), AccIn :: term(), AccOut :: term(), List1 :: [A], List2 :: [B], A :: term(), B :: term().
Combines the operations of map/2
and foldl/3
into one pass.
Example:
Summing 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}
-spec max(List) -> Max when List :: [T, ...], Max :: T, T :: term().
Returns the first element of List
that compares greater than or equal to all
other elements of List
.
Returns true
if Elem
matches some element of List
, otherwise false
.
-spec merge3(List1, List2, List3) -> List4 when List1 :: [X], List2 :: [Y], List3 :: [Z], List4 :: [X | Y | Z], X :: term(), Y :: term(), Z :: term().
Returns the sorted list formed by merging List1
, List2
, and List3
. All of
List1
, List2
, and List3
must be sorted before evaluating this function.
When two elements compare equal, the element from List1
, if there is such an
element, is picked before the other element, otherwise the element from List2
is picked before the element from List3
.
-spec merge(ListOfLists) -> List1 when ListOfLists :: [List], List :: [T], List1 :: [T], T :: term().
Returns the sorted list formed by merging all the sublists of ListOfLists
. All
sublists must be sorted before evaluating this function.
When two elements compare equal, the element from the sublist with the lowest
position in ListOfLists
is picked before the other element.
-spec merge(List1, List2) -> List3 when List1 :: [X], List2 :: [Y], List3 :: [X | Y], X :: term(), Y :: term().
Returns the sorted list formed by merging List1
and List2
. Both List1
and
List2
must be sorted before evaluating this function.
When two elements compare equal, the element from List1
is picked before the
element from List2
.
-spec merge(Fun, List1, List2) -> List3 when Fun :: fun((A, B) -> boolean()), List1 :: [A], List2 :: [B], List3 :: [A | B], A :: term(), B :: term().
Returns the sorted list formed by merging List1
and List2
. Both List1
and
List2
must be sorted according to the
ordering function Fun
before evaluating this
function.
Fun(A, B)
is to return true
if A
compares less than or equal to
B
in the ordering, otherwise false
. When two elements compare equal, the
element from List1
is picked before the element from List2
.
-spec min(List) -> Min when List :: [T, ...], Min :: T, T :: term().
Returns the first element of List
that compares less than or equal to all
other elements of List
.
-spec nth(N, List) -> Elem when N :: pos_integer(), List :: [T, ...], Elem :: T, T :: term().
Returns the N
th element of List
.
Example:
> lists:nth(3, [a, b, c, d, e]).
c
-spec nthtail(N, List) -> Tail when N :: non_neg_integer(), List :: [T, ...], Tail :: [T], T :: term().
Returns the N
th tail of List
, that is, the sublist of List
starting at
N+1
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]).
[]
-spec partition(Pred, List) -> {Satisfying, NotSatisfying} when Pred :: fun((Elem :: T) -> boolean()), List :: [T], Satisfying :: [T], NotSatisfying :: [T], T :: term().
Partitions List
into two lists, where the first list contains all elements for
which Pred(Elem)
returns true
, and the second list contains all elements for
which Pred(Elem)
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
.
Returns true
if List1
is a prefix of List2
, otherwise false
.
-spec reverse(List1) -> List2 when List1 :: [T], List2 :: [T], T :: term().
Returns a list with the elements in List1
in reverse order.
Returns a list with the elements in List1
in reverse order, with tail Tail
appended.
Example:
> lists:reverse([1, 2, 3, 4], [a, b, c]).
[4,3,2,1,a,b,c]
-spec search(Pred, List) -> {value, Value} | false when Pred :: fun((T) -> boolean()), List :: [T], Value :: T.
If there is a Value
in List
such that Pred(Value)
returns true
, returns
{value, Value}
for the first such Value
, otherwise returns false
. The
Pred
function must return a boolean.
Equivalent to seq(From, To, 1)
-spec seq(From, To, Incr) -> Seq when From :: integer(), To :: integer(), Incr :: integer(), Seq :: [integer()].
Returns a sequence of integers that starts with From
and contains the
successive results of adding Incr
to the previous element, until To
is
reached or passed (in the latter case, To
is not an element of the sequence).
Incr
defaults to 1.
Failures:
- If
To < From - Incr
andIncr > 0
. - If
To > From - Incr
andIncr < 0
. - If
Incr =:= 0
andFrom =/= 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]
-spec sort(List1) -> List2 when List1 :: [T], List2 :: [T], T :: term().
Returns a list containing the sorted elements of List1
.
-spec sort(Fun, List1) -> List2 when Fun :: fun((A :: T, B :: T) -> boolean()), List1 :: [T], List2 :: [T], T :: term().
Returns a list containing the sorted elements of List1
, according to the
ordering function Fun
. Fun(A, B)
is to return
true
if A
compares less than or equal to B
in the ordering, otherwise
false
.
-spec split(N, List1) -> {List2, List3} when N :: non_neg_integer(), List1 :: [T], List2 :: [T], List3 :: [T], T :: term().
Splits List1
into List2
and List3
. List2
contains the first N
elements
and List3
the remaining elements (the N
th tail).
-spec splitwith(Pred, List) -> {List1, List2} when Pred :: fun((T) -> boolean()), List :: [T], List1 :: [T], List2 :: [T], T :: term().
Partitions List
into two lists according to Pred
.
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]}
The Pred
function must return a boolean. For a different way to partition a
list, see partition/2
.
-spec sublist(List1, Len) -> List2 when List1 :: [T], List2 :: [T], Len :: non_neg_integer(), T :: term().
Returns the sublist of List1
starting at position 1 and with (maximum) Len
elements. It is not an error for Len
to exceed the length of the list, in that
case the whole list is returned.
-spec sublist(List1, Start, Len) -> List2 when List1 :: [T], List2 :: [T], Start :: pos_integer(), Len :: non_neg_integer(), T :: term().
Returns the sublist of List1
starting at Start
and with (maximum) Len
elements. It is not an error for Start+Len
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).
[]
-spec subtract(List1, List2) -> List3 when List1 :: [T], List2 :: [T], List3 :: [T], T :: term().
Returns a new list List3
that is a copy of List1
, subjected to the following
procedure: for each element in List2
, its first occurrence in List1
is
deleted.
Example:
> lists:subtract("123212", "212").
"312".
lists:subtract(A, B)
is equivalent to A -- B
.
Returns true
if List1
is a suffix of List2
, otherwise false
.
Returns the sum of the elements in List
.
-spec takewhile(Pred, List1) -> List2 when Pred :: fun((Elem :: T) -> boolean()), List1 :: [T], List2 :: [T], T :: term().
Takes elements Elem
from List1
while Pred(Elem)
returns true
, that is,
the function returns the longest prefix of the list for which all elements
satisfy the predicate. The Pred
function must return a boolean.
-spec ukeymerge(N, TupleList1, TupleList2) -> TupleList3 when N :: pos_integer(), TupleList1 :: [T1], TupleList2 :: [T2], TupleList3 :: [T1 | T2], T1 :: Tuple, T2 :: Tuple, Tuple :: tuple().
Returns the sorted list formed by merging TupleList1
and TupleList2
. The
merge is performed on the N
th element of each tuple. Both TupleList1
and
TupleList2
must be key-sorted without duplicates before evaluating this
function.
When two tuples compare equal, the tuple from TupleList1
is picked
and the one from TupleList2
is deleted.
-spec ukeysort(N, TupleList1) -> TupleList2 when N :: pos_integer(), TupleList1 :: [Tuple], TupleList2 :: [Tuple], Tuple :: tuple().
Returns a list containing the sorted elements of list TupleList1
where all
except the first tuple of the tuples comparing equal have been deleted. Sorting
is performed on the N
th element of the tuples.
-spec umerge3(List1, List2, List3) -> List4 when List1 :: [X], List2 :: [Y], List3 :: [Z], List4 :: [X | Y | Z], X :: term(), Y :: term(), Z :: term().
Returns the sorted list formed by merging List1
, List2
, and List3
. All of
List1
, List2
, and List3
must be sorted and contain no duplicates before
evaluating this function.
When two elements compare equal, the element from
List1
is picked if there is such an element, otherwise the element from
List2
is picked, and the other is deleted.
-spec umerge(ListOfLists) -> List1 when ListOfLists :: [List], List :: [T], List1 :: [T], T :: term().
Returns the sorted list formed by merging all the sublists of ListOfLists
. 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 ListOfLists
is picked and the other is deleted.
-spec umerge(List1, List2) -> List3 when List1 :: [X], List2 :: [Y], List3 :: [X | Y], X :: term(), Y :: term().
Returns the sorted list formed by merging List1
and List2
. Both List1
and
List2
must be sorted and contain no duplicates before evaluating this
function.
When two elements compare equal, the element from List1
is picked
and the one from List2
is deleted.
-spec umerge(Fun, List1, List2) -> List3 when Fun :: fun((A, B) -> boolean()), List1 :: [A], List2 :: [B], List3 :: [A | B], A :: term(), B :: term().
Returns the sorted list formed by merging List1
and List2
. Both List1
and
List2
must be sorted according to the
ordering function Fun
and contain no duplicates
before evaluating this function.
Fun(A, B)
is to return true
if A
compares
less than or equal to B
in the ordering, otherwise false
. When two elements
compare equal, the element from List1
is picked and the one from List2
is
deleted.
-spec uniq(List1) -> List2 when List1 :: [T], List2 :: [T], T :: term().
Returns a list containing the elements of List1
with duplicated elements
removed (preserving the order of the elements). The first occurrence of each
element is kept.
Examples:
> lists:uniq([3,3,1,2,1,2,3]).
[3,1,2]
> lists:uniq([a, a, 1, b, 2, a, 3]).
[a, 1, b, 2, 3]
-spec uniq(Fun, List1) -> List2 when Fun :: fun((T) -> any()), List1 :: [T], List2 :: [T], T :: term().
Returns a list containing the elements of List1
without the elements for which
Fun
returned duplicate values (preserving the order of the elements). The
first occurrence of each element is kept.
Examples:
> lists:uniq(fun({X, _}) -> X end, [{b, 2}, {a, 1}, {c, 3}, {a, 2}]).
[{b, 2}, {a, 1}, {c, 3}]
-spec unzip3(List1) -> {List2, List3, List4} when List1 :: [{A, B, C}], List2 :: [A], List3 :: [B], List4 :: [C], A :: term(), B :: term(), 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.
-spec unzip(List1) -> {List2, List3} when List1 :: [{A, B}], List2 :: [A], List3 :: [B], A :: term(), 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.
-spec usort(List1) -> List2 when List1 :: [T], List2 :: [T], T :: term().
Returns a list containing the sorted elements of List1
where all except the
first element of the elements comparing equal have been deleted.
-spec usort(Fun, List1) -> List2 when Fun :: fun((T, T) -> boolean()), List1 :: [T], List2 :: [T], T :: term().
Returns a list containing the sorted elements of List1
where all except the
first element of the elements comparing equal according to the
ordering function Fun
have been deleted.
Fun(A, B)
is to return true
if A
compares less than or equal to B
in the
ordering, otherwise false
.
-spec zip3(List1, List2, List3) -> List4 when List1 :: [A], List2 :: [B], List3 :: [C], List4 :: [{A, B, C}], A :: term(), B :: term(), C :: term().
Equivalent to zip3(List1, List2, List3, fail)
-spec zip3(List1, List2, List3, How) -> List4 when List1 :: [A], List2 :: [B], List3 :: [C], List4 :: [{A | DefaultA, B | DefaultB, C | DefaultC}], A :: term(), B :: term(), C :: term(), How :: fail | trim | {pad, {DefaultA, DefaultB, DefaultC}}, DefaultA :: term(), DefaultB :: term(), DefaultC :: term().
"Zips" three lists 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.
For a description of the How
parameter, see zip/3
.
-spec zip(List1, List2) -> List3 when List1 :: [A], List2 :: [B], List3 :: [{A, B}], A :: term(), B :: term().
Equivalent to zip(List1, List2, fail)
-spec zip(List1, List2, How) -> List3 when List1 :: [A], List2 :: [B], List3 :: [{A | DefaultA, B | DefaultB}], A :: term(), B :: term(), How :: fail | trim | {pad, {DefaultA, DefaultB}}, DefaultA :: term(), DefaultB :: term().
"Zips" two lists 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.
The How
parameter specifies the behavior if the given lists are of different
lengths.
fail
- The call will fail if the given lists are not of equal length. This is the default.trim
- Surplus elements from the longer list will be ignored.Examples:
> lists:zip([a, b], [1, 2, 3], trim). [{a,1},{b,2}] > lists:zip([a, b, c], [1, 2], trim). [{a,1},{b,2}]
{pad, Defaults}
- The shorter list will be padded to the length of the longer list, using the respective elements from the givenDefaults
tuple.Examples:
> lists:zip([a, b], [1, 2, 3], {pad, {x, 0}}). [{a,1},{b,2},{x,3}] > lists:zip([a, b, c], [1, 2], {pad, {x, 0}}). [{a,1},{b,2},{c,0}]
-spec zipwith3(Combine, List1, List2, List3) -> List4 when Combine :: fun((X, Y, Z) -> T), List1 :: [X], List2 :: [Y], List3 :: [Z], List4 :: [T], X :: term(), Y :: term(), Z :: term(), T :: term().
Equivalent to zipwith3(Combine, List1, List2, List3, fail)
-spec zipwith3(Combine, List1, List2, List3, How) -> List4 when Combine :: fun((X | DefaultX, Y | DefaultY, Z | DefaultZ) -> T), List1 :: [X], List2 :: [Y], List3 :: [Z], List4 :: [T], X :: term(), Y :: term(), Z :: term(), How :: fail | trim | {pad, {DefaultX, DefaultY, DefaultZ}}, DefaultX :: term(), DefaultY :: term(), DefaultZ :: term(), T :: term().
Combines the elements of three lists into one list. For each triple X, Y, Z
of
list elements from the three lists, the element in the result list is
Combine(X, Y, Z)
.
For a description of the How
parameter, see zip/3
.
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]]
-spec zipwith(Combine, List1, List2) -> List3 when Combine :: fun((X, Y) -> T), List1 :: [X], List2 :: [Y], List3 :: [T], X :: term(), Y :: term(), T :: term().
Equivalent to zipwith(Combine, List1, List2, fail)
-spec zipwith(Combine, List1, List2, How) -> List3 when Combine :: fun((X | DefaultX, Y | DefaultY) -> T), List1 :: [X], List2 :: [Y], List3 :: [T], X :: term(), Y :: term(), How :: fail | trim | {pad, {DefaultX, DefaultY}}, DefaultX :: term(), DefaultY :: term(), T :: term().
Combines the elements of two lists into one list. For each pair X, Y
of list
elements from the two lists, the element in the result list is Combine(X, Y)
.
For a description of the How
parameter, see zip/3
.
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]