【Erlang新手成长日记】Erlang模块学习之lists

lists

列表处理函数

官方文档:http://www.erlang.org/doc/man/lists.html

函数:

all(Pred, List) -> boolean()

类型:

  Pred = fun((Elem :: T) -> boolean())

  List = [T]

  T = term()

说明:

  Returns true if Pred(Elem) returns true for all elements Elem in List, otherwise false.

any(Pred, List) -> boolean()

类型:

  Pred = fun((Elem :: T) -> boolean())

  List = [T]

  T = term()

说明:

  Returns true if Pred(Elem) returns true for at least one element Elem in List.

append(ListOfLists) -> List1

类型:

  ListOfLists = [List]

  List = List1 = [T]

  T = term()

说明:

  Returns a list in which all the sub-lists of ListOfLists have been appended.

示例:

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 List3 which is made from the elements of List1 followed by the elements of List2.

示例:

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

concat(Things) -> string()

类型:

  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.

示例:

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

delete(Elem, List1) -> List2

类型:

  Elem = T

  List1 = List2 = [T]

  T = term()

说明:

  Returns a copy of List1 where the first element matching Elem is deleted, if there is such an element.

dropwhile(Pred, List1) -> List2

类型:

  Pred = fun((Elem :: T) -> boolean()

  List1 = List2 = [T]

  T = term()

说明:

  Drops elements Elem from List1 while Pred(Elem) returns true and returns the remaining list.

duplicate(N, Elem) -> List

类型:

  N = integer() >= 0

  Elem = T

  List = T

  T = term()

说明:

  Returns a list which contains N copies of the term Elem.

实例:

lists:duplicate(5, xx).
%% [xx,xx,xx,xx,xx]

filter(Pred, List1) -> List2

类型:

  Pred = fun((Elem :: T) -> boolean()

  List1 = List2 = [T]

  T = term()

说明:

  List2 is a list of all elements Elem in List1 for which Predr(Elem) returns true.

flatlength(DeepList) -> integer() >= 0

类型:

  DeepList = [Term | DeepList]

说明:

  Equivalent to length(flatten(DeepList)), but more efficient.

flatmap(Fun, List1) -> List2

类型:

  Fun = fun((A) -> [B])

  List1 = List2 = [T]

  T = term()

说明: 

  Takes a function from As to lists of Bs, and a list of As (List1) and produces a list of Bs 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)).

示例:

lists:flatmap(fun(X)->[X,X] end, [a,b,c]).
%% [a,a,b,b,c,c]

flatten(DeepList) -> List

类型:

  DeepList = [term() | List]

  List = [term()]

说明:

  Returns a flattened(平整的) version of DeepList.

flatten(DeepList, Tail) -> List

类型:

  DeepList = [term() | List]

  Tail = List = [term()]

说明:

  Returns a flattened version of DeepList with the tail Tail appended.

foldl(Fun, Acc0, List) -> Acc1

类型:

  Fun = fun((Elem :: T, AccIn) -> AccOut)

  Acc0 = Acc1 = AccIn = 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.

示例: 

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.

示例:

P = fun(A, AccIn) -> io:format("~p ", [A]), AccIn end.
lists:foldl(P, void, [1,2,3]).
%% 1 2 3 void
lists:foldr(P, void, [1,2,3]).
%% 3 2 1 void

foreach(Fun, List) -> ok

类型:

  Fun = fun((Elem :: T) -> term())

  List = [T]

  T = term()

说明:

  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.

keydelete(Key, N, TupleList1) -> TupleList2

类型:

  Key = term()

  N = integer() >= 1

      1..tuple_size(Tuple)

  TupleList1 = TupleList2 = [Tuple]

  Tuple = tuple()

说明:

  Returns a copy of TupleList1 where the first occurrence of a tuple whose Nth element compares equal to Key is deleted, if there is such a tuple.

keyfind(Key, N, TupleList) -> Tuple | false

类型:

  Key = term()

  N = 1..tuple_size(Tuple)

  TupleList = [Tuple]

  Tuple = tuple()

说明:

  Searches the list of tuples TupleList for a tuple whose Nth element compares equal to Key. Returns Tuple if such a tuple is found, otherwise false.

keymap(Fun, N, TupleList1) -> TupleList2

类型:

  Fun = fun((Term1 :: term()) -> Term2 :: term())

  N = integer() >= 0

      1..tuple_size(Tuple)

  TupleList1 = TupleList2 = [Tuple]

  Tuple = tuple()

说明:

  Returns a list of tuples where, for each tuple in TupleList1, the Nth element Term1 of the tuple has been replaced with the result of calling Fun(Term1).

示例:

Fun = fun(Atom) -> atom_to_list(Atom) end.
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 = 1..tuple_size(Tuple)

  TupleList = [Tuple]

  Tuple = tuple()

说明:

  Returns true if there is a tuple in TupleList whose Nth element compares equal to Key, otherwise false.

keymerge(N, TupleList1, TupleList2) -> TupleList3

类型:

  N = integer() >= 0

      1..tuple_size(Tuple)

  TupleList1 = [T1]

  TupleList2 = [T2]

  TupleList3 = [(T1 | T2)]

  T1 = T2 = [Tuple]

  Tuple = tuple()

说明:

  Returns the sorted list formed by merging TupleList1 and TupleList2. The merge is performed on the Nth element of each tuple. Both TupleList1 and TupleList2 must be key-sorted prior to evaluating this function. When two tuples compare equal, the tuple from TupleList1 is picked before the tuple from TupleList2.

keyreplace(Key, N, TupleList1, NewTuple) -> TupleList2

类型:

  Key = term()

  N = integer() >= 0

      1..tuple_size(Tuple)

  TupleList1 = TupleList2 = [Tuple]

  NewTuple = Tuple

  Tuple = tuple()

说明:

  Returns a copy of TupleList1 where the first occurrence of a T tuple whose Nth element compares equal to Key is replaced with NewTuple, if there is such a tuple T.

keysort(N, TupleList1) -> TupleList2

类型:

  N = integer() >= 0

      1..tuple_size(Tuple)

  TupleList1 = TupleList2 = [Tuple]

  Tuple = tuple()

说明:

  Returns a list containing the sorted elements of the list TupleList1. Sorting is performed on the Nth element of the tuples. The sort is stable.

keystore(Key, N, TupleList1, NewTuple) -> TupleList2

类型:

  Key = term()

  N = integer() >= 1

      1..tuple_size(Tuple)

  TupleList1 = [Tuple]

  TupleList2 = [Tuple, ...]

  NewTuple = Tuple

  Tuple = tuple()

说明:

  Returns a copy of TupleList1 where the first occurrence of a tuple T whose Nth 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.

keytake(Key, N, TupleList1) -> {value, Tuple, TupleList2} | false

类型:

  Key = term()

  N = integer() >= 0

      1..tuple_size(Tuple)

  TupleList1 = TupleList2 = [tuple()]

  Tuple = tuple()

说明:

  Searches the list of tuples TupleList1 for a tuple whose Nth 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.

last(List) -> Last

类型:

  List = [T, ...]

  Last = T

  T = term()

说明:

  Returns the last element in List.

map(Fun, List1) -> List2

类型:

  Fun = fun((A) -> B)

  List1 = [A]

  List2 = [B]

  A = B = term()

说明:

  Takes a function from As to Bs, and a list of As and produces a list of Bs by applying the function to every element in the list. This function is used to obtain the return values. The evaluation order is implementation dependent.

mapfoldl(Fun, Acc0, List1) -> {List2, Acc1}

类型:

  Fun = fun((A, AccIn) -> {B, AccOut})

  Acc0 = Acc1 = AccIn = AccOut = term()

  List1 = [A]

  List2 = [B]

  A = B = term()

说明:

  mapfoldl combines the operations of map/2 and foldl/3 into one pass. An 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}

mapfoldr(Fun, Acc0, List1) -> List2

类型:

  Fun = fun((A, AccIn) -> {B, AccOut})

  Acc0 = Acc1 = AccIn = Accout = term()

  List1 = [A]

  List2 = [B]

  A = B = term()

说明:

  mapfoldr combines the operations of map/2 and foldr/3 into one pass.

max(List) -> Max

类型:

  List = [T, ...]

  Max = T

  T = term()

说明:

  Returns the first element of List that compares greater than or equal to all other elements of List.

 

member(Elem, List)

类型:

  Elem = term()

  List = [term()]

说明:

  Returns true if Elem matches some element of List, otherwise false.

merge(ListOfLists) -> List1

类型:

  ListOfLists = [List]

  List = List1 = [T]

  T = term()

说明:

  Returns the sorted list formed by merging all the sub-lists of ListOfLists. All sub-lists must be sorted prior to evaluating this function. When two elements compare equal, the element from the sub-list with the lowest position in ListOfLists 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 List1 and List2. Both List1 and List2 must be sorted prior to evaluating this function. When two elements compare equal, the element from List1 is picked before the element from List2.

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 List1 and List2. Both List1 and List2 must be sorted according to the ordering function Fun prior to evaluating this function. Fun(A, B) should return true if A compares less than or equal to B in the ordering, false otherwise. When two elements compare equal, the element from List1 is picked before the element from List2. 

merge3(List1, List2, List3) -> List4

类型:

  List1 = [X]

  List2 = [Y]

  List3 = [Z]

  List4 = [(X | Y | Z)]

说明:

  Returns the sorted list formed by merging List1List2 and List3. All of List1List2 and List3 must be sorted prior to 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.

min(List) -> Min

类型:

  List = [T, ...]

  Min = T

  T = term()

说明:

  Returns the first element of List that compares less than or equal to all other elements of List.

nth(N, List) -> Elem

类型:

  N = integer() >= 0

      1..length(List)

  List = [T, ...]

  Elem = T

  T = term()

说明:

  Returns the Nth element of List.

示例:

lists:nth(3, [a, b, c, d, e]).
%% c

 nthtail(N, List) -> Elem

类型:

  N = integer() >= 0

      1..length(List)

  List = [T, ...]

  Elem = T

  T = term()

说明:

  Returns the Nth tail of List, that is, the sublist of List starting at N+1 and continuing up to the end of the list.

示例:

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 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.

示例:

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]}

prefix(List1, List2) -> boolean()

类型:

  List1 = List2 = [T]

  T = term()

说明:

  Returns true if List1 is a prefix of List2, otherwise false.

reverse(List1) -> List2

类型:

  List1 = List2 = [T]

  T = term()

说明:

  Returns a list with the elements in List1 in reverse order.

reverse(List1, Tail) -> List2

类型:

  List1 = Tail = List2 = [term()]

说明:

  Returns a list with the elements in List1 in reverse order, with the tail Tail appended.

示例:

lists:reverse([1, 2, 3, 4], [a, b, c]).
%% [4,3,2,1,a,b,c]

seq(From, To) -> Seq

seq(From, To, Incr) -> Seq

类型:

  From = To = Incr = integer()

  Seq = [integer()]

 说明:

  Returns a sequence of integers which starts with From and contains the successive results of adding Incr to the previous element, until To has been reached or passed (in the latter case, To is not an element of the sequence). Incr defaults to 1.

  Failure: If To<From-Incr and Incr is positive, or if To>From-Incr and Incr is negative, or if Incr==0 and From/=To.

示例:

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 List1.

sort(Fun, List1) -> List2

类型:

  Fun = fun((A :: T, B :: T) -> boolean())

  List1 = List2 = [T]

  T = term()

说明:

  Returns a list containing the sorted elements of List1, according to the ordering function FunFun(A, B) should returntrue if A compares less than or equal to B in the ordering, false otherwise.

  

split(N, List1) -> {List2, List3}

类型;

  N = integer() >= 0

      1..length(List1)

  List1 = List2 = List3 = [T]

说明:

  Splits List1 into List2 and List3List2 contains the first N elements and List3 the rest of the elements (the Nth tail).

splitwith(Pred, List) -> {List1, List2}

类型:

  Pred = fun((T) -> boolean())

  List = List1 = List2 = [T]

  T = term()

说明:

  Partitions List into two lists according to Predsplitwith/2 behaves as if it is defined as follows:

splitwith(Pred, List) ->
    {takewhile(Pred, List), dropwhile(Pred, List)}.

示例:

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]}

sublist(List1, Len) -> List2

类型:

  List1 = List2 = [T]

  N = integer() >= 0

  T = term()

说明:

  Returns the sub-list of List1 starting at position 1 and with (max) Len elements. It is not an error for Len 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

      1..(length(List1) + 1)

  Len = integer() >= 0

  T = term()

说明:

  Returns the sub-list of List1 starting at Start and with (max) Len elements. It is not an error for Start+Len to exceed the length of the list.

示例:

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 List3 which is a copy of List1, subjected to the following procedure: for each element in List2, its first occurrence in List1 is deleted.

示例:

lists:subtract("123212", "212").
%% "312"

suffix(List1, List2) -> boolean()

类型:

  List1 = List2 = [T]

  T = term()

说明:

  Returns true if List1 is a suffix of List2, otherwise false.

sum(List) -> number()

类型:

  List = [integer()]

说明:

  Returns the sum of the elements in List.

takewhile(Pred, List1) -> List2

类型:

  Pred = fun((Elem :: T) -> boolean())

  List1 = 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.

ukeymerge(N, TupleList1, TupleList2) -> TupleList3

类型:

  N = integer() >= 1

      1..tuple_size(Tuple)

  TupleList1 = [T1]

  TupleList2 = [T2]

  TupleList3 = [(T1 | T2)]

  T1 = T2 = Tuple

  Tuple = tuple()

说明:

  Returns the sorted list formed by merging TupleList1 and TupleList2. The merge is performed on the Nth element of each tuple. Both TupleList1 and TupleList2 must be key-sorted without duplicates prior to evaluating this function. When two tuples compare equal, the tuple from TupleList1 is picked and the one from TupleList2 deleted.

ukeysort(N, List1) -> List2

类型:

  N = integer() >= 0

      1..tuple_size(Tuple)

  List1 = List2 = Tuple

  Tuple = tuple()

说明:

  Returns a list containing the sorted elements of the list TupleList1 where all but the first tuple of the tuples comparing equal have been deleted. Sorting is performed on the Nth element of the tuples.

umerge(ListOfLists) -> List1

类型:

  ListOfLists = [List]

  List = List1 = [T]

  T = term()

说明:

  Returns the sorted list formed by merging all the sub-lists of ListOfLists. All sub-lists must be sorted and contain no duplicates prior to evaluating this function. When two elements compare equal, the element from the sub-list with the lowest position in ListOfLists is picked and the other one deleted.

umerge(List1, List2) -> List3

类型:

  List1 = [A]

  List2 = [B]

  List3 = [(A | B)]

  A = 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 prior to evaluating this function. Fun(A, B) should return true if A compares less than or equal to B in the ordering, false otherwise. When two elements compare equal, the element from List1 is picked and the one from List2 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 List1 and List2. Both List1 and List2 must be sorted according to the ordering function Fun and contain no duplicates prior to evaluating this function. Fun(A, B) should return true if A compares less than or equal to B in the ordering, false otherwise. When two elements compare equal, the element from List1 is picked and the one from List2 deleted.

umerge3(List1, Liset2, List3) -> List4

类型:

  List1 = [A]

  List2 = [B]

  List3 = [C]

  List4 = [(A | B | C)]

  A = B= C = 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 prior to 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 one 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 List1 where all but 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 which contains the sorted elements of List1 where all but the first element of the elements comparing equal according to the ordering function Fun have been deleted. Fun(A, B) should return true if A compares less than or equal to B in the ordering, false otherwise.

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 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 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()

说明:

  Combine the elements of two lists of equal length into one list. For each pair X, Y of list elements from the two lists, the element in the result list will be Combine(X, Y).

  zipwith(fun(X, Y) -> {X,Y} end, List1, List2) is equivalent to zip(List1, List2).

示例:

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()

说明:

  Combine the elements of three lists of equal length into one list. For each triple X, Y, Z of list elements from the three lists, the element in the result list will be Combine(X, Y, Z).

  zipwith3(fun(X, Y, Z) -> {X,Y,Z} end, List1, List2, List3) is equivalent to zip3(List1, List2, List3).

示例:

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]]

转载于:https://www.cnblogs.com/dyingbleed/archive/2012/08/29/2657022.html

猜你喜欢

转载自blog.csdn.net/weixin_34378767/article/details/93301802
今日推荐