ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
* lists模块的reverse/1经过高度优化,可以在重排lists元素顺序时考虑使用 * 如果两个list都拥有很多数据,那么请不要使用'--',而是将数据转化到ordsets,然后调用ordsets:substract/2 * 调用lists:flatten/1可以将list扁平化,这个操作代价很大,比'++'还要昂贵。下面这些时候我们可以避免: 将数据发送给port时 调用list_bo_binary/1和iolist_to_binary前 ```erlang 1.all(F, List) -> boolean()     > lists:all( fun(X) -> X == 1 end, [1,1,1,1,1] ) -> true     > lists:all( fun(X) -> X == 1 end, [1,1,1,1,2] ) -> false. 2.any(Pred, List) -> boolean()     > lists:any( fun(X) -> X == 1 end, [2,2,2,2,2] ) -> false     > lists:any( fun(X) -> X == 1 end, [1,2,2,2,2] ) -> true. 3.append(ListOfLists) -> List1     > lists:append([[1, 2, 3], [a, b], [4, 5, 6]]) -> [1,2,3,a,b,4,5,6]   append(List1, List2) -> List3     > lists:append("abc", "def") -> "abcdef" 4.concat(Things) -> string()     > lists:concat([doc, '/', file, '.', 3]) -> "doc/file.3" 5.delete(Elem, List1) -> List2     > lists:delete( 1, [1,2,3,4,5,1] ) -> [2,3,4,5,1]. 6.dropwhile(Pred, List1) -> List2     > lists:dropwhile( fun(X) -> X==1 end, [1,2,3,4] ) -> [2,3,4]. 7.duplicate(N, Elem) -> List     > lists:duplicate(5, xx).[xx,xx,xx,xx,xx] 8.filter(Pred, List1) -> List2     > lists:filter(fun(X) -> X < 3 end, [1,2,3,4,5,6,] ) -> [1,2]. 9.flatlength(DeepList) -> integer() >= 0     > lists:flatlength( [[1], [2,3]] ) -> 3.     > lists:flatlength([[1], [2,3], [4], 5]) -> 5.     去除中括号后所有的项数 10.flatmap(Fun, List1) -> List2     > lists:flatmap(fun(X)->[X,X] end, [a,b,c]).[a,a,b,b,c,c] 11.flatten(DeepList) -> List     > lists:flatten( [1,[2], [3, [4, [5,6]]]] ) -> [1,2,3,4,5,6]     > lists:flatten( ["a",["b"], ["c", ["d", ["e","f"]]]] ) -> "abcdef" 12.flatten(DeepList, Tail) -> List     > lists:flatten( ["a",["b"], ["c", ["d", ["e","f"]]]], "qqq" ) -> "abcdefqqq" 13. foldl(Fun, Acc0, List) -> Acc1     > 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     > lists:foldl(  fun( X, A ) ->X++"a"++A end, [], ["1","2","3","4","5"] ) -> "5a4a3a2a1a"     > lists:foldl(  fun( X, A ) ->{X++"a", A}end, [], ["1","2","3","4","5"] ) -> {"5a",{"4a",{"3a",{"2a",{"1a",[]}}}}}     > lists:foldl(  fun( X, A ) ->[X++"a"| A] end, [], ["1","2","3","4","5"] ) -> ["5a","4a","3a","2a","1a"] 14.foldr(Fun, Acc0, List) -> Acc1     > lists:foldr(  fun( X, A ) ->[X++"a"| A] end, [], ["1","2","3","4","5"] ) -> ["1a","2a","3a","4a","5a"]     > P = fun(A, AccIn) -> io:format("~p ", [A]), AccIn end.#Fun     > lists:foldl(P, void, [1,2,3]). 1 2 3 void     > lists:foldr(P, void, [1,2,3]).3 2 1 void 15.foreach(Fun, List) -> ok     对列表List的每一个元素执行fun函数,并且成功返回ok 16.keydelete(Key, N, TupleList1) -> TupleList2     lists:keydelete(Key, 1, [{Key,V},{...}]). 17.keyfind(Key, N, TupleList) -> Tuple | false     lists:keyfind(Key, 1, [{Key,V},{...}]). 18.keymap(Fun, N, TupleList1) -> TupleList2     > Fun = fun(Atom) -> atom_to_list(Atom) end.#Fun2     > lists:keymap(Fun, 2, [{name,jane,22},{name,lizzie,20},{name,lydia,15}]).[{name,"jane",22},{name,"lizzie",20},{name,"lydia",15}] 19.keymember(Key, N, TupleList) -> boolean()     > lists:keymember( 1, 2, [{2,3},{4,5},{1,2}] ) -> false. 20.keymerge(N, TupleList1, TupleList2) -> TupleList3     > lists:keymerge( 3, [{a,b,c},{c,d,e},{e,f,g}], [{a,b,1},{c,d,2},{e,f,3}] ) -> [{a,b,1},{c,d,2},{e,f,3},{a,b,c},{c,d,e},{e,f,g}]     每一项元组的第3项来排序 21.keyreplace(Key, N, TupleList1, NewTuple) -> TupleList2     > lists:keyreplace( 1, 1, [{1,2},{3,4}], {1,5} ) -> [{1,5},{3,4}] 22.keysearch(Key, N, TupleList) -> {value, Tuple} | false     > lists:keysearch( 1, 2, [{1,2},{3,4},{5,1}] ) -> {value,{5,1}}     The function lists:keyfind/3 (introduced in R13A) is in most cases more convenient. 23.keysort(N, TupleList1) -> TupleList2     > lists:keysort( 2, [{5,6},{3,4},{1,2},{7,8}] ) -> [{1,2},{3,4},{5,6},{7,8}] 24.keystore(Key, N, TupleList1, NewTuple) -> TupleList2     > lists:keystore( 1, 1, [{1,2},{3,4},{5,6}], {1,7} ) -> [{1,7},{3,4},{5,6}]     > lists:keystore( 6, 1, [{1,2},{3,4},{5,6}], {1,7} ) -> [{1,2},{3,4},{5,6},{1,7}] 25.keytake(Key, N, TupleList1) -> {value, Tuple, TupleList2} | false     > lists:keytake( 1, 1, [{1,2},{3,4}] ) -> {value,{1,2},[{3,4}]}     > lists:keytake( 2, 1, [{1,2},{3,4}] ) -> false 26.last(List) -> Last     > lists:last( [1,2,3,4,5] ) -> 5 27.map(Fun, List1) -> List2     将函数F应用到 List1 的每一个元素上(性能问题) 28.mapfoldl(Fun, Acc0, List1) -> {List2, Acc1}     > lists:mapfoldl(fun(X, Sum) -> {2*X, X+Sum} end,0, [1,2,3,4,5]) -> {[2,4,6,8,10],15} 29.mapfoldr(Fun, Acc0, List1) -> {List2, Acc1}     > lists:mapfoldr(fun(X, Sum) -> {2*X, X+Sum} end,0, [1,2,3,4,5]) -> {[2,4,6,8,10],15} 30.max(List) -> Max     > lists:max( [a,b,c,d,e] ) -> e 31.member(Elem, List) -> boolean()     > lists:member( "a", ["a", "b", "c", "d"] ) -> true 32.merge(ListOfLists) -> List1     > lists:merge( [["a","b","c"], [[1,2,3]]] ) -> [[1,2,3],"a","b","c"]     效果类似append函数 33.merge(List1, List2) -> List3     > lists:merge( [1,2,3], ["a", "b", "c"] ) -> [1,2,3,"a","b","c"] 34.merge(Fun, List1, List2) -> List3     > lists:merge( fun(X,Y) -> X<Y end, [1,12,13,55,44,33,22,11], ["a", "ab", "ac"] ) -> [1,12,13,55,44,33,22,11,"a","ab","ac"] Returns the sorted list formed by merging List1 and List2. Both List1 and List2 must be sorted according to the ordering functionFun 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. 35.merge3(List1, List2, List3) -> List4     > lists:merge3( [1,2,3],[1,2,3],[4,5,6] ) -> [1,1,2,2,3,3,4,5,6] 36.min(List) -> Min     > lists:min( ["a","b","c","d","e"] ) -> "a" 37.nth(N, List) -> Elem     > lists:nth(3, [a, b, c, d, e]).c 38.nthtail(N, List) -> Tail     > 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]) -> [] 39.partition(Pred, List) -> {Satisfying, NotSatisfying}     > 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]} 40.prefix(List1, List2) -> boolean()     > lists:prefix( [1,2,3], [1,2,3,4,5] ) -> true     > lists:prefix( "我们", "我们都有一个家" ) -> true 41.reverse(List1) -> List2     reverse(List1, Tail) -> List2     > lists:reverse([1, 2, 3, 4], [a, b, c]).[4,3,2,1,a,b,c] 42.seq(From, To) -> Seq     seq(From, To, Incr) -> Seq     > lists:seq(From, To, Incr)) == (To-From+Incr) div Incr     > 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] 43.sort(List1) -> List2    sort(Fun, List1) -> List2 44.split(N, List1) -> {List2, List3}     > lists:split( 4, [1,2,3,3,3,34,5,65] ) -> {[1,2,3,3],[3,34,5,65]} 45.splitwith(Pred, List) -> {List1, List2}    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]}     > lists:splitwith(fun(A) -> is_atom(A) end, [a,b,c,d,1,2,3,4,e]) -> {[a,b,c,d],[1,2,3,4,e]} 46.sublist(List1, Len) -> List2    sublist(List1, Start, Len) -> List2     > 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).[] 47.subtract(List1, List2) -> List3     > lists:subtract("123212", "212")."312".    lists:subtract(A, B) is equivalent to A -- B. WarUsing ordered lists and ordsets:subtract/2 is a much better choice if both lists are long. 48.suffix(List1, List2) -> boolean()     > lists:suffix( "aa", "bbccaa" ) -> true 49.sum(List) -> number()     > lists:sum( [1,2,3,4] ) -> 10 50.takewhile(Pred, List1) -> List2     lists:takewhile( fun(X) -> X==1 end, [1,2,3,4,5] ) -> [1] 51.ukeymerge(N, TupleList1, TupleList2) -> TupleList3     > lists:ukeymerge( 3, [{a,b,c},{c,d,e},{e,f,g}], [{a,b,1},{c,d,2},{e,f,3}] ) -> [{a,b,1},{c,d,2},{e,f,3},{a,b,c},{c,d,e},{e,f,g}]     > lists:ukeymerge( 2, [{a,b},{c,d},{e,f}], [{a,b},{c,d},{e,f}] ) -> [{a,b},{c,d},{e,f}]     排序去重复 52.ukeysort(N, TupleList1) -> TupleList2     u表示去重复??? 53.umerge(ListOfLists) -> List1    umerge(List1, List2) -> List3    umerge(Fun, List1, List2) -> List3    umerge3(List1, List2, List3) -> List4 54.unzip(List1) -> {List2, List3}     un表示功能相反??     > lists:unzip( [{1,2},{3,4},{5,6}]) -> {[1,3,5],[2,4,6]}   unzip3(List1) -> {List2, List3, List4}     > lists:unzip3( [{1,2,3},{3,4,4},{5,6,6}] ) -> {[1,3,5],[2,4,6],[3,4,6]} 55.usort(List1) -> List2     排序去重复    usort(Fun, List1) -> List2 56.zip(List1, List2) -> List3     > lists:zip( [1,2,3],[4,5,6] ) -> [{1,4}, {2,5}, {3,6}]    zip3(List1, List2, List3) -> List4     > lists:zip3( [1,2,3], [4,5,6], [7,8,9] ) -> [{1,4,7},{2,5,8},{3,6,9}] 57.zipwith(Combine, List1, List2) -> List3    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    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]] ```