# Detailed explanation of functions and usage of erlang lists series of functions

blog.csdn.net/zhangjingya...

The OTP path is/erl8.3\lib\stdlib-3.3\src\lists.erl

**
**

1. With function Pred **
**1, all(Pred, List) -> boolean()
If each element in the List is executed as a parameter of the Pred function, and the result returns true, then the all function returns true,
otherwise it returns false

example:

lists:all(fun(E) -> true end,[1,2,3,4]).

result

true

2, any(Pred, List) -> boolean()
If at least one element in the List is executed as a parameter of the Pred function, and the result returns true, then the any function returns true,
otherwise it returns false

example

lists:any(fun(E) -> is_integer(E) end,[q,2,a,4]).

result

true

3. dropwhile(Pred, List1) -> List2
executes the Pred function with the elements in the List1 list as parameters, if it returns true, discards it, and finally returns
the list of remaining elements

example

lists:dropwhile(fun(E) -> is_atom(E) end,[a,1,2,a,b]).

result

[1,2,a,b]\

4. filter(Pred, List1) -> List2
returns a list, this list is composed of elements in List1 whose Pred function returns true.

lists:filter(fun(E) -> is_integer(E) end,[q,2,a,4]).

result:

[2,4]

5. map(Fun, List1) -> List2
executes each element in List1 in Fun, and then returns an element, and these returned elements form a list, which is
returned to List2
example:
lists:map(fun(X )->[X,X] end, [a,b,c]).
Result: [[a,a],[b,b],[c,c]]

6, flatmap(Fun, List1) -> List2
This function is similar to map, which is equivalent to executing
lists:append(lists:map(List1)).
That is, the result of the map is appended.
Example:
lists:flatmap(fun (X)->[X,X] end, [a,b,c]).
Result: [a,a,b,b,c,c]

7, foldl(Fun, Acc0, List) -> Acc1
Fun This function has two parameters
. The first parameter is the element in the List, and the second parameter is the return value after the Fun function is executed. This parameter is executed for the first time Time
is Acc0
example: Sum [1,2,3,4,5]
lists:foldl(fun(X, Sum) -> X + Sum end, 0, [1,2,3,4,5]) .
results: 15
performing procedure: first, when Fun first execution, the value of X takes the first element of the list list 1, Sum take 0,
Fun For the second execution, the value of X takes the second list list Element 2, Sum takes the return value of Fun for the first time and
pushes in turn until each element in the List is executed, and finally foldl returns the result of the last time.

8. foldr(Fun, Acc0, List) -> Acc1
This function of foldr is similar to foldl,
but when Fun is executed, the value of X takes the last one in the List first, and then takes the second from the bottom.

9. foreach(Fun, List) -> ok
executes the Fun function with each element in the List as a parameter. The execution order is in the order of the elements in the List. This function finally returns ok. It is one-sided
example lists:foreach(fun(X)->
%%using X to do somethings
%%
end,List)

10. keymap(Fun, N, TupleList1) -> TupleList2
treats the Nth item of each element in TupleList1 as a parameter in Fun, and then this Nth item is finally replaced with the value returned by Fun after execution.
Example:
List1 = [{name,"zhangjing"},{name,"zhangsan"}].
lists:keymap(fun(X)->
list_to_atom(X)
end,2,List1).
Results:
[{name,zhangjing},{ name,zhangsan}]

11. mapfoldl(Fun, Acc0, List1) -> {List2, Acc1}
This function is equivalent to combining the map and foldl functions. Execute the Fun function for each element in List1. After the execution, the first value of the curly brace is returned as the return value, and the
second value is passed to Fun as a parameter for the next use.
Example:
lists:mapfoldl(fun(X, Sum) -> {2*X, X+Sum} end,
0, [1,2,3,4,5]).
{[2,4,6,8, 10],15}

12. mapfoldr(Fun, Acc0, List1) -> {List2, Acc1}
This function is equivalent to combining map and foldr

13, merge(Fun, List1, List2) -> List3
The function of this function is also to merge List1 and List2, but the elements of List1 and List2 should be executed in Fun as parameters. If
Fun returns true, then the return value That is, List1 is in front and List2 is in the back. Otherwise, vice versa.
Example
lists: merge(fun(A,B)-> false end, [3,4],[2,1]).
Result
[2,1,3,4]

14, partition(Pred, List) -> {Satisfying, NotSatisfying}
The function of this function is to divide the List into two List1 and List2. List1 is composed of elements that take the List element as a parameter to the Pred function and return true.
List2 is composed of Pred Composition of elements that return false.
Note that what is returned is a tuple.
Example
lists:partition(fun(A) -> A rem 2 == 1 end, [1,2,3,4,5,6,7]). The
result is
{[1,3 ,5,7],[2,4,6]}

15, sort(Fun, List1) -> List2
If the Fun function returns true, the sorting is from small to large, otherwise, from large to small.
Fun has two parameters.
Example
lists:sort(fun(A,B)-> false end,[1,2,3]).
Result
[3,2,1]

16, splitwith(Pred, List) -> {List1, List2}
divide List into List1 and List2,
List1 is composed of elements in List that return true in Pred function, but there is one thing, if it encounters false, it will be left
All the elements of is placed in List2, and only those with the previous value of true in List1.
Example
lists: splitwith(fun(A) -> is_atom(A) end, [a,b,1,c,d,2,3,4,e]).
Result
{[a,b],[1,c ,d,2,3,4,e]}

17, takewhile(Pred, List1) -> List2
The element elements in List1 execute Pred(element) in turn, if it returns true, get this element, until there is an element that executes Pred(element) returns false
Example
lists: takewhile(fun(E )-> is_atom(E) end,[a,b,1,e,{c},[d]]).
Result
[a,b]

18,umerge(Fun, List1, List2) -> List3
This function is different from merge in that when Fun returns true, the same elements cannot appear in the returned List3.
Question: But when Fun returns false, there can be the same elements in List3 element.
Example (when Fun returns true)
lists:umerge(fun(A,B)-> true end,[1,2],[2,3]).
Result
[1,2,3]
(when Fun is false) )
lists:umerge(fun(A,B)-> false end,[1,2],[2,3]).
[2,3,1,2] is
so amazing, there are 2 duplicates

19. usort(Fun, List1) -> List2 is
sorted according to the Fun function. If Fun returns true, only the first element of List1 is returned.
If Fun returns false, List1 is sorted from largest to smallest.
Example 1
lists: usort(fun (A,B) -> true end, [1,2,2,3,4]).
Result
[1]

Example 2
lists:usort(fun(A,B) -> false end, [1,2,2,3,4]).
Result
[4,3,2,2,1]

20, zipwith(Combine, List1, List2) -> List3
executes the Combine function for each element in List1 and List2 , and then returns an element. List3 is composed of elements returned by the Combine function.
The function is similar to map, but here is the operation on two lists.
Example
lists: zipwith(fun(X, Y) -> X+Y end, [1,2,3], [4,5,6]).
Result
[5,7,9]

21. zipwith3(Combine, List1, List2, List3) -> List4
executes the Combine function for each element in List1, List2, and List3 , and then returns an element. List4 is composed of elements returned by the Combine function.
The function is similar to map, but here is the operation on the three lists.
Example
lists: zipwith3(fun(X, Y, Z) -> X+Y+Z end, [1,2,3], [4,5,6],[7,8,9]).
Result
[12 ,15,18]

**2. without function Pred
**1, append(ListOfLists) -> List1
ListOfLists is composed of List, and List is a list, which can be any type of element.
This function is to combine all the elements of the list in ListOfLists
Compile a list in order. Tip: The elements in ListOfLists must be lists in order to use this function

example

lists:append([[1, 2, 3], [a, b], [4, 5, 6]]).

result:

[1,2,3,a,b,4,5,6]

2. append(List1, List2) -> List3
connects the two lists List1 and List2 to form a list, and then returns the new list.
The function of this function is equivalent to List1 ++ List2

example

lists:append("abc", "def").

result

"abcdef"

3. concat(Things) -> string()
where Things is a list, in which atom() | integer() | float() | string()
combines the elements in this list into a string, and then returns

example

lists:concat([doc,'/', file,'.', 3]).

result

doc/file.3"

4. delete(Elem, List1) -> List2
List1 is composed of many Elements. The function of this function is to find the first element in List1 that is the same as Elem,
then delete it, and return the new list after deletion.

example

lists:delete({name,"zhangsan"},[{name,"lisi"},{name,"zhangsan"},{name,"wangmazi"})).

result

[{name,"lisi"},{name,"wangmazi"}]

5. Duplicate(N, Elem) -> List
returns a list of N Elems.

example

lists:duplicate(5,"test").

result

["test","test","test","test","test"]

6, flatlength(DeepList) -> integer() >= 0
My understanding is that DeepList is a set of lists in the list.
Calculate the length of the list, that is, use the flatten function to convert DeepList into the number of elements in the List.
This function and length() The difference is: The
length function is to get the number of list elements,
and the flatlength function is to first convert the DeepList into the number of List.
For example, List = [1,2,[3,4]] This list is
calculated by length(List) The value is: 3
lists:flatlength(List) evaluates to: 4
In fact, lists:flatlength(List) = length(flatten(List))

7. flatten(DeepList) -> List
turns DeepList into a list with term() only.
Examples:
lists:flatten([[a,a],[b,b],[c,c]]).
Result:
[a ,a,b,b,c,c]

8. flatten(DeepList, Tail) -> List
is to turn DeepList into a list with term only, and then add a Tail at the back.
Example:
lists:flatten([[a,a],[b,b],[c,c]],[dd]).
Result:
[a,a,b,b,c,c,dd]

9,keydelete(Key, N, TupleList1) -> TupleList2
This function is suitable for handling the case where the elements in the list are tuples.
Delete the Nth element of the element in TupleList1 and the element with the same Key. Only delete the first one and the same afterwards. is not deleted
examples:
List = [{name, "Zhangjing"}, {Sex, "MALE"}, {name, "zhangsan"}, {Sex, "MALE"}],
Lists: keydelete ( "MALE", 2 ,List)
Result:
[{name,"zhangjing"},{name,"zhangsan"},{sex,"male"}]

10,keyfind(Key, N, TupleList) -> Tuple | false
Find a Tuple in TupleList, if it is found, return, if not, return false.
This Tuple must satisfy the Nth element and the key is the same.
Example:
List1 = [{name,"zhangjing"},{name,"zhangsan"}].
lists:keyfind("zhangjing",2,List1)
Result: {name,"zhangjing"}

11. keymember(Key, N, TupleList) -> boolean()
If the Nth element in the TupleList is consistent with the key, it returns true, otherwise it returns false.
Example:
List1 = [{name,"zhangjing"}, {name,"zhangsan"}].
lists:keymember("zhangjing",2,List1).Result
: true

12. keymerge(N, TupleList1, TupleList2) -> TupleList3 mixes
TupleList1 and TupleList2 to form a TupleList. The
newly formed TupleList is sorted according to the Nth element of Tuple.
Example:
List1 = [{name,"zhangjing" },{name,"zhangsan"}].
List2 = [{nick,"zj"},{nick,"zs"}].
lists:keymerge(2,List1,List2).
Result:
[{name,"zhangjing "},
{name,"zhangsan"},
{nick,"zj"},
{nick,"zs"}]

13. keyreplace(Key, N, TupleList1, NewTuple) -> TupleList2
finds the Nth element in the Tuple of TupleList1 that is consistent with the Key, and then replaces this Tuple with NewTuple. If it is not found
, it returns to the original TupleList1
example:
List1 = [{name,"zhangjing"},{name,"zhangsan"}]
lists:keyreplace("zhangjing",2,List1,{nickname,"netzj"}).
Result:
[{nickname,"netzj"} ,{name,"zhangsan"}]

14. keysearch(Key, N, TupleList) -> {value, Tuple} | false
This function is similar to keyfind, that is, the structure of the return value is not the same,
but also to find a Tuple in TupleList. The Nth element of this Tuple is the same as Key .
Example:
List1 = [{name,"zhangjing"},{name,"zhangsan"}]
lists:keysearch("zhangjing",2,List1).
Result:
{value,{name,"zhangjing"}}

15, keysort(N, TupleList1) -> TupleList2
sorts Tuples in TupleList1 according to the Nth element, and then returns a TupleList in a new order.
However, this sorting is fixed.
Example:
List1 = [{name,"zhangsan"},{name,"zhangjing"}].
lists:keysort(2,List1).
Result:
[{name,"zhangjing"},{name,"zhangsan"}]

16, keystore(Key, N, TupleList1, NewTuple) -> TupleList2
This function is similar to the keyreplace function, the difference is that when the keystore does not find the corresponding Tuple,
the NewTuple will be appended to the end of the TupleList1.
Example:
List1 = [{name,"zhangsan"},{name,"zhangjing"}].
Found cases
lists:keystore("zhangjing",2,List1,{name,"netzhangjing"}).
[{name ,"netzhangjing"},{name,"zhangsan"}]
lists:keystore("zhanging",2,List1,{name,"netzhangjing"}).
[{name,"zhangjing"},{name ,"zhangsan"},{name,"netzhangjing"}]

17, keytake(Key, N, TupleList1) -> {value, Tuple, TupleList2} | false
Find Tuple in TupleList1. The Nth element of this Tuple is consistent with the Key. If such a Tuple is found
, then return, {value, Tuple, TupleList2} where TupleList2 is TupleList1 with Tuple removed.
Example:
List1 = [{name,"zhangjing"},{name,"zhangsan"},{name,"lisi"}].
lists:keytake("zhangjing", 2,List1).
Result:
{value,{name,"zhangjing"},[{name,"zhangsan"},{name,"lisi"}]}

18, last(List) -> Last
return: the last element of List
Example:
List1 = [{name,"zhangjing"},{name,"zhangsan"},{name,"lisi"}].
lists:last(List1 ).
Result:
{name,"lisi"}

19. max(List) -> Max
takes out the largest element in List. Generally, it is more suitable when List is of integer type.
Example:
lists:max([1,10,15,6]).
Result:
15

20, member(Elem, List) -> boolean()
If Elem and an element in List match (same), then return true, otherwise return false
Example
lists:member({sex,"1"},[{sex ,"1"},{sex,"2"},{sex,"3"}]).
Result:
true

21. merge(ListOfLists) -> List1
ListOfLists is a list, which consists of sublists.
The function of this function is to merge these sublists into one list.
Example:
lists:merge([[{11}],[{22}],[{33}]]).
Result
[{11},{22},{33}]

22. merge(List1, List2) -> List3
List1 and List2 are each a list. The function of this function is to merge the two lists into one list.
Example:
lists:merge([11],[22]).
Result
[11,22]
[2,1,3,4]

23, merge3(List1, List2, List3) -> List4 merges
List1, List2, List3 into one list
Example
lists:merge3([11],[22],[33,44]).
Result:
[11,22, 33,44]

24, min(List) -> Min
returns the smallest element in List, which corresponds to the max function.
Example
lists:min([1,2,3]).
Result
1

25, nth(N, List) -> Elem
returns the Nth element in the List.
Example
lists:nth(2,[{name,"zhangsan"},{name,"lisi"},{name,"wangmazi"}]).
Result
{name,"lisi"}

26, nthtail(N, List) -> Tail
returns the element after the Nth element in the List.
Example
lists:nthtail(3, [a, b, c, d, e]).
Result
[d,e]

27, prefix(List1, List2) -> boolean()
If List1 is the prefix of List2 (that is, the front part of List1 and List2 are the same), then return true, otherwise return false

28, reverse(List1) -> List2
reverses List1
Example
lists:reverse([1,2,3,4]).
Result
[4,3,2,1]

29,reverse(List1, Tail) -> List2
Reverse List1, then connect Tail to the back of Reverse List1, and then return
Example
lists:reverse([1, 2, 3, 4], [a, b, c ]).
[4,3,2,1,a,b,c]

30, seq(From, To) -> Seq
where From and To are both integers, this function returns an integer list from From to To.
Example
lists:seq(1,10).
Result
[1,2,3,4,5,6,7,8,9,10]

31, seq(From, To, Incr) -> Seq
returns an integer list, the next element of this list is greater than the previous element by Incr.
Example
lists:seq(1,10,4).
[1,5,9]

32, sort(List1) -> List2
sorts the elements in List1 from small to large, and then returns a new list.
Example
lists:sort([3,2,1]).
Result
[1,2,3]

33, split(N, List1) -> {List2, List3}
divide List1 into List2 and List3,
where List2 includes the first N elements of List1, and List3 includes the rest.
Example
lists:split(3,[1,2,3,4,5]).
Result
{[1,2,3],[4,5]}

This function is different from the number of partitions. Partition traverses all Lists. If splitwith encounters false during
traversal, the traversal ends immediately and the result is returned.

34, sublist(List1, Len) -> List2
returns a list from the first element to the Lenth element. When this Len is greater than the length of List1, it returns all.
Example
lists: sublist([1,2,3,4,5,6],3).
Result
[1,2,3]

35, sublist(List1, Start, Len) -> List2
returns a list starting from the Start position of List1 and Len elements behind.
Example
lists:sublist([1,2,3,4], 2, 2).
Result
[2,3]

36, subtract(List1, List2) -> List3 is
equivalent to List1-List2.
The function of this function is to return a copy of List1. For each element in List2, it will be deleted when it first appears in the copy of List1.
Example
lists:subtract("112233","12"). The
result is
"1233"

37, suffix(List1, List2) -> boolean()
If List1 is the suffix of List2, then return true, otherwise return false
Example
lists:suffix("22","1122"). The
result is
true

38, sum(List) -> number()
returns the sum of each element in the List. The elements in List should all be of type number().
Example
lists:sum([1,2,3,4]).
Result
10

39, ukeymerge(N, TupleList1, TupleList2) -> TupleList3
The elements in TupleList1 and TupleList2 are all tuples.
Combine TupleList1 and TupleList2. The merging rule is to follow the Nth element of the tuple. If the Nth element has the same , Then keep the in TupleList1
and delete the in TupleList2.

40. Ukeysort(N, TupleList1) -> TupleList2
The elements in TupleList1 are all tuples.
This function also returns a list of tuples. The returned list is sorted according to the Nth element of the tuple. If If
the Nth element is the same in the tuple, delete the next tuple.
Example
lists: ukeysort(1,[{name,"zhangsan"},{sex,"male"},{name,"himan"}]).
Result
[{name,"zhangsan"},{sex,"male" }]

41. The
only difference between umerge(ListOfLists) -> List1 and merge is that the same element cannot appear in it. If the same element appears, delete it, and only keep a unique
example
lists:umerge([[1,2] ,[2,3]]).
Result
[1,2,3]
analysis: Since the merge in [[1,2],[2,3]] is [1,2,2,3], this time there is Two identical elements 2, so only one 2 is saved, so the result is [1,2,3].

42, umerge3(List1, List2, List3) -> List4 merges
List1, List2, List3. The
difference from merge3 is that no duplicate elements can appear in the returned List4.
Example
lists:merge3([1,2],[2,3] ,[3,4]).
Result
[1,2,3,4]

43, unzip(List1) -> {List2, List3}
The elements in List1 are tuples, each tuple consists of two elements, and the return value List2 contains the first element of each tuple in each List1. The
return value List3 contains the second element of each tuple in each List1.
Example
lists:unzip([{name,"zhangsan"},{sex,"male"},{city,"hangzhou"}]).
Result
{[name,sex,city],["zhangsan","male" ,"hangzhou"]}

44, unzip3(List1) -> {List2, List3, List4}
The elements in List1 are tuples, each tuple consists of three elements, and the return value List2 contains the first element of each tuple in each List1 ; The
return value List3 contains the second element of each tuple in each List1; the return value List4 contains the third element of each tuple in each List1.
Example
lists:unzip3([{name,"zhangsan","apple"},{sex,"male","banana"},{city,"hangzhou","orange"}]).
Result
{[name,sex ,city],
["zhangsan","male","hangzhou"],
["apple","banana","orange"]}
Note that the final return is a tuple.

45. usort(List1) -> List2
sorts List1 in ascending order. If there are duplicate elements after sorting, delete the duplicates and save only one unique one.
Example
lists: usort([4,3,2,1,2,3,4]).
Result
[1,2,3,4]

46, zip(List1, List2) -> List3 merges
two lists of the same length into one list.
List3 is the first element of each tuple in it is obtained from List1, and the second element of each tuple The elements are obtained from List2.
Example
lists:zip([name,sex,city],["zhangsan","male","hangzhou"]). The
result is
[{name,"zhangsan"},{sex,"male "},{city,"hangzhou"}]
Note that if the lengths of List1 and List2 are inconsistent, then this function will report an error.

47, zip3(List1, List2, List3) -> List4 merges
three lists of the same length into one list.
List4 is the first element of each tuple in it is obtained from List1, and the first element of each tuple The two elements are obtained from List2
The third element of each tuple is obtained from List3.
Example
lists:zip3([name,sex,city],["zhangsan","male","hangzhou"],["nick","1","zhejiang"]).
Result
[{name,"zhangsan" ,"nick"},
{sex,"male","1"},
{city,"hangzhou","zhejiang"}]

\