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
Whenever an
if x
if x
x
An example of a typical ordering function is less than or equal
to,
Returns
Returns
Returns a list in which all the sub-lists of
> lists:append([[1, 2, 3], [a, b], [4, 5, 6]]). [1,2,3,a,b,4,5,6]
Returns a new list
> lists:append("abc", "def"). "abcdef"
Concatenates the text representation of the elements
of
> lists:concat([doc, '/', file, '.', 3]). "doc/file.3"
Returns a copy of
Drops the last element of a
Drops elements
Returns a list which contains
> lists:duplicate(5, xx). [xx,xx,xx,xx,xx]
Calls
That is,
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]
Equivalent to
Takes a function from
That is,
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
Returns a flattened version of
Calls
> 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
Like
> 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
Calls
Returns a copy of
Searches the list of tuples
Returns a list of tuples where, for each tuple in
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}]
Returns
Returns the sorted list formed by merging
Returns a copy of
Searches the list of tuples
This function is retained for backward compatibility.
The function
Returns a list containing the sorted elements of the list
Returns a copy of
Searches the list of tuples
Returns the last element in
Takes a function from
> lists:mapfoldl(fun(X, Sum) -> {2*X, X+Sum} end, 0, [1,2,3,4,5]). {[2,4,6,8,10],15}
Returns the first element of
Returns
Returns the sorted list formed by merging all the sub-lists
of
Returns the sorted list formed by merging
Returns the sorted list formed by merging
Returns the sorted list formed by merging
Returns the first element of
Returns the
> lists:nth(3, [a, b, c, d, e]). c
Returns the
> 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]). []
Partitions
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]}
See also
Returns
Returns a list with the elements in
Returns a list with the elements in
> lists:reverse([1, 2, 3, 4], [a, b, c]). [4,3,2,1,a,b,c]
Returns a sequence of integers which starts with
Failure: If
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]
Returns a list containing the sorted elements of
Returns a list containing the sorted elements of
Splits
Partitions
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]}
See also
Returns the sub-list of
Returns the sub-list of
> 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). []
Returns a new list
> lists:subtract("123212", "212"). "312".
Returns
Returns the sum of the elements in
Takes elements
Returns the sorted list formed by merging
Returns a list containing the sorted elements of the list
Returns the sorted list formed by merging all the sub-lists
of
Returns the sorted list formed by merging
Returns the sorted list formed by merging
Returns the sorted list formed by merging
"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.
"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.
Returns a list containing the sorted elements of
Returns a list which contains the sorted elements of
"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 corresponding element in the second list.
"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 corresponding element in the second list, and the third element is taken from the corresponding element in the third list.
Combine the elements of two lists of equal length into one
list. For each pair
Example:
> lists:zipwith(fun(X, Y) -> X+Y end, [1,2,3], [4,5,6]). [5,7,9]
Combine the elements of three lists of equal length into one
list. For each triple
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]]