From 68d53c01b0b8e9a007a6a30158c19e34b2d2a34e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= This module contains functions for list processing. Whenever an
if x
If x
if x
If x
x
An example of a typical ordering function is less than or equal
- to,
An example of a typical ordering function is less than or equal
+ to:
Returns
Returns
Returns
Returns
Returns a list in which all the sub-lists of
-
Returns a list in which all the sublists of
+
Example:
> lists:append([[1, 2, 3], [a, b], [4, 5, 6]]). [1,2,3,a,b,4,5,6]
Returns a new list
Returns a new list
Example:
> lists:append("abc", "def"). "abcdef"
Concatenates the text representation of the elements
- of
Concatenates the text representation of the elements of
+
Example:
> lists:concat([doc, '/', file, '.', 3]). "doc/file.3"
Returns a copy of
Drops the last element of a
Drops the last element of a
Drops elements
Drops elements
Returns a list which contains
Returns a list containing
Example:
> lists:duplicate(5, xx). [xx,xx,xx,xx,xx]
Calls
That is,
Calls
That is,
filtermap(Fun, List1) ->
lists:foldr(fun(Elem, Acc) ->
@@ -179,26 +203,29 @@ filtermap(Fun, List1) ->
{true,Value} -> [Value|Acc]
end
end, [], List1).
- Example:
+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
Equivalent to
Takes a function from
Takes a function from
That is,
flatmap(Fun, List1) ->
append(map(Fun, List1)).
-
Example:
+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
Returns a flattened version of
Calls
Calls
Example:
> lists:foldl(fun(X, Sum) -> X + Sum end, 0, [1,2,3,4,5]). 15 @@ -244,12 +276,14 @@ flatmap(Fun, List1) -> 120
Like
Like
Example:
> P = fun(A, AccIn) -> io:format("~p ", [A]), AccIn end. #Fun<erl_eval.12.2225172> @@ -257,10 +291,11 @@ flatmap(Fun, List1) -> 1 2 3 void > lists:foldr(P, void, [1,2,3]). 3 2 1 void-
Calls
Calls
Returns a copy of
Searches the list of tuples
Returns a list of tuples where, for each tuple in
-
Examples:
+Examples:
> Fun = fun(Atom) -> atom_to_list(Atom) end. #Fun<erl_eval.6.10732646> @@ -324,33 +366,37 @@ flatmap(Fun, List1) -> [{name,"jane",22},{name,"lizzie",20},{name,"lydia",15}]
Returns
Returns
Returns the sorted list formed by merging
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
This function is retained for backward compatibility. Function
+
Returns a list containing the sorted elements of the list
-
Returns a list containing the sorted elements of list
+
Returns a copy of
Searches the list of tuples
Searches the list of tuples
Returns the last element in
Takes a function from
Takes a function from
Combines the operations of
+
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}
Combines the operations of
+
Returns the first element of
Returns
Returns
Returns the sorted list formed by merging all the sub-lists
- of
Returns the sorted list formed by merging all the sublists of
+
Returns the sorted list formed by merging
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
Returns the
Example:
> lists:nth(3, [a, b, c, d, e]). c
Returns the
Returns the
Example
> lists:nthtail(3, [a, b, c, d, e]). [d,e] @@ -557,70 +636,91 @@ c[]
Partitions
Examples:
+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
For a different way to partition a list, see
+
Returns
Returns a list with the elements in
Returns a list with the elements in
Example:
> lists:reverse([1, 2, 3, 4], [a, b, c]). [4,3,2,1,a,b,c]
Returns a sequence of integers which starts with
Returns a sequence of integers that starts with
+
Failure: If
Failures:
+If
If
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:
+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] @@ -634,74 +734,87 @@ length(lists:seq(From, To, Incr)) == (To-From+Incr) div Incr [1]
Returns a list containing the sorted elements of
Returns a list containing the sorted elements of
Splits
Splits
Partitions
splitwith(Pred, List) ->
{takewhile(Pred, List), dropwhile(Pred, List)}.
- Examples:
+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
For a different way to partition a list, see
+
Returns the sub-list of
Returns the sublist of
Returns the sub-list of
Returns the sublist of
Examples:
> lists:sublist([1,2,3,4], 2, 2). [2,3] @@ -711,142 +824,163 @@ splitwith(Pred, List) -> []
Returns a new list
Returns a new list
Example:
> lists:subtract("123212", "212"). "312".
The complexity of
The complexity of
Returns
Returns the sum of the elements in
Takes elements
Takes elements
Returns the sorted list formed by merging
Returns the sorted list formed by merging
+
Returns a list containing the sorted elements of the list
-
Returns a list containing the sorted elements of list
+
Returns the sorted list formed by merging all the sub-lists
- of
Returns the sorted list formed by merging all the sublists
+ of
Returns the sorted list formed by merging
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, @@ -854,76 +988,84 @@ splitwith(Pred, List) -> 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 + list and the second element is taken from the 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.
+ the 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
Combines the elements of two lists of equal length into one list.
+ For each pair
Example:
+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:
+Combines 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] -- cgit v1.2.3