%% 对list模块经常用到的进行补充
%% 1 对所有元素进行去重
%% (1) lists:umerge
%% (2)
ldrop_dup(List) ->
Fun = fun(X, List1) ->
IsMem = lists:member(X, List1),
if IsMem == true ->
List1;
true ->
[X|List1]
end
end,
RetList = lists:foldl(Fun, [], List),
lists:reverse(RetList).
%% 去掉某个元素
ldrop_ele(Ele, List) ->
erlang:display([?MODULE, ?LINE, Ele, List]),
Fun = fun(X, List1) ->
if X == Ele ->
List1;
true ->
[X|List1]
end
end,
RetList = lists:foldl(Fun, [], List),
lists:reverse(RetList).
%% 对某个元素进行去重, 留下第一个,其他的都删除
%% 如果只有一个就删除
ldrop_one_dup(Ele, List) ->
Ret = ldrop_one_dup_i(Ele, List, 0, []),
Ret.
ldrop_one_dup_i(Ele, [], Count, RetList) ->
RList = lists:reverse(RetList),
if Count == 1 ->
ldrop_ele(Ele, RList);
true ->
RList
end;
ldrop_one_dup_i(Ele, [H|T], Count, RetList) ->
if H == Ele andalso Count == 0 ->
ldrop_one_dup_i(Ele, T, 1, [H|RetList]);
H == Ele andalso Count == 1 ->
ldrop_one_dup_i(Ele, T, 2, RetList);
H == Ele andalso Count > 1 ->
ldrop_one_dup_i(Ele, T, Count, RetList);
true ->
ldrop_one_dup_i(Ele, T, Count, [H|RetList])
end.
%% 对某个子列表的元素进行去重, 去重List1的
ldrop_list_dup([], List2) ->
List2;
ldrop_list_dup(List1, List2) ->
[H|T] = List1,
NewList = ldrop_one_dup(H, List2),
ldrop_list_dup(T, NewList).
%% 对某个子列表的元素完全去掉,去掉list1的
ldrop_list([], List2) ->
List2;
ldrop_list(List1, List2) ->
[H|T] = List1,
NewList = ldrop_ele(H, List2),
ldrop_list(T, NewList).
%% 替换,把列表的首次遇到的某个值,替换为另一个值, 把Ele1替换为Ele2
%% 如果不存在,返回原来的值
lrep_one(Ele1,Ele2, List) ->
lrep_one1(Ele1, Ele2, false, List, []).
lrep_one1(_Ele1, _Ele2, Flag, [], List) ->
if Flag ->
List;
true ->
lists:reverse(List)
end;
lrep_one1(Ele1, Ele2, Flag, List1, List2) ->
[H1|T1] = List1,
if H1 == Ele1 ->
NewList2 = [Ele2|List2],
if is_atom(T1) ->
RList = [T1|NewList2],
lists:reverse(RList);
true ->
%RList1 = lists:reverse(NewList2),
%RList = lists:merge([RList1, T1])
RList1 = lists:reverse(T1),
RList2 = lists:merge(RList1, NewList2),
RList = lists:reverse(RList2),
lrep_one1(Ele1, Ele2, true, [], RList)
end;
true ->
lrep_one1(Ele1, Ele2, Flag, T1, [H1|List2])
end.
%% 替换,把列表的某个值,完全替换为另一个值, 把Ele1替换为Ele2
lrep_all(Ele1, Ele2, List) ->
Fun = fun(X, List1) ->
if X == Ele1 ->
[Ele2 | List1];
true ->
[X|List1]
end
end,
RetList = lists:foldl(Fun, [], List),
lists:reverse(RetList).
lists.erl 模块注意事项:
(1)
append/1: 实现中用到 ++
append([E]) -> E;
append([H|T]) -> H ++ append(T);
append([]) -> [].
(2)
delete/2 实现用递归,却非尾递归
delete(Item, [Item|Rest]) -> Rest;
delete(Item, [H|Rest]) ->
[H|delete(Item, Rest)];
delete(_, []) -> [].
(3)
dropwhile/2
dropwhile(Pred, [Hd|Tail]=Rest) ->
case Pred(Hd) of
true -> dropwhile(Pred, Tail);
false -> Rest
end;
dropwhile(Pred, []) when is_function(Pred, 1) -> [].
删除函数返回true的元素,如果遇到返回false立即返回剩余的列表,不管以后是否能有返回true的元素
(4)
duplicate/2
duplicate(N, X) when is_integer(N), N >= 0 -> duplicate(N, X, []).
duplicate(0, _, L) -> L;
duplicate(N, X, L) -> duplicate(N-1, X, [X|L]).
如果不要is_integer的guard语句,当传入参数N非整形,返回bad argument in an arithmetic expression, 否则返回
no function clause matching lists:duplicate
(5)
lists里面的bif
%% Bifs: member/2, reverse/2
%% Bifs: keymember/3, keysearch/3, keyfind/3
(6)
suffix/2
suffix(List1, List2)
List1如果是List2的子列表,结尾子列表,则为true,否则false
(7)
不少的对元祖组成的列表处理,
keydelete
keyfind
keymap
keymember
keymerge
keyreplace
...
lists主要是对list的各种处理,包括各种复杂的元素结构,例如元祖组成的列表等
%% 1 对所有元素进行去重
%% (1) lists:umerge
%% (2)
ldrop_dup(List) ->
Fun = fun(X, List1) ->
IsMem = lists:member(X, List1),
if IsMem == true ->
List1;
true ->
[X|List1]
end
end,
RetList = lists:foldl(Fun, [], List),
lists:reverse(RetList).
%% 去掉某个元素
ldrop_ele(Ele, List) ->
erlang:display([?MODULE, ?LINE, Ele, List]),
Fun = fun(X, List1) ->
if X == Ele ->
List1;
true ->
[X|List1]
end
end,
RetList = lists:foldl(Fun, [], List),
lists:reverse(RetList).
%% 对某个元素进行去重, 留下第一个,其他的都删除
%% 如果只有一个就删除
ldrop_one_dup(Ele, List) ->
Ret = ldrop_one_dup_i(Ele, List, 0, []),
Ret.
ldrop_one_dup_i(Ele, [], Count, RetList) ->
RList = lists:reverse(RetList),
if Count == 1 ->
ldrop_ele(Ele, RList);
true ->
RList
end;
ldrop_one_dup_i(Ele, [H|T], Count, RetList) ->
if H == Ele andalso Count == 0 ->
ldrop_one_dup_i(Ele, T, 1, [H|RetList]);
H == Ele andalso Count == 1 ->
ldrop_one_dup_i(Ele, T, 2, RetList);
H == Ele andalso Count > 1 ->
ldrop_one_dup_i(Ele, T, Count, RetList);
true ->
ldrop_one_dup_i(Ele, T, Count, [H|RetList])
end.
%% 对某个子列表的元素进行去重, 去重List1的
ldrop_list_dup([], List2) ->
List2;
ldrop_list_dup(List1, List2) ->
[H|T] = List1,
NewList = ldrop_one_dup(H, List2),
ldrop_list_dup(T, NewList).
%% 对某个子列表的元素完全去掉,去掉list1的
ldrop_list([], List2) ->
List2;
ldrop_list(List1, List2) ->
[H|T] = List1,
NewList = ldrop_ele(H, List2),
ldrop_list(T, NewList).
%% 替换,把列表的首次遇到的某个值,替换为另一个值, 把Ele1替换为Ele2
%% 如果不存在,返回原来的值
lrep_one(Ele1,Ele2, List) ->
lrep_one1(Ele1, Ele2, false, List, []).
lrep_one1(_Ele1, _Ele2, Flag, [], List) ->
if Flag ->
List;
true ->
lists:reverse(List)
end;
lrep_one1(Ele1, Ele2, Flag, List1, List2) ->
[H1|T1] = List1,
if H1 == Ele1 ->
NewList2 = [Ele2|List2],
if is_atom(T1) ->
RList = [T1|NewList2],
lists:reverse(RList);
true ->
%RList1 = lists:reverse(NewList2),
%RList = lists:merge([RList1, T1])
RList1 = lists:reverse(T1),
RList2 = lists:merge(RList1, NewList2),
RList = lists:reverse(RList2),
lrep_one1(Ele1, Ele2, true, [], RList)
end;
true ->
lrep_one1(Ele1, Ele2, Flag, T1, [H1|List2])
end.
%% 替换,把列表的某个值,完全替换为另一个值, 把Ele1替换为Ele2
lrep_all(Ele1, Ele2, List) ->
Fun = fun(X, List1) ->
if X == Ele1 ->
[Ele2 | List1];
true ->
[X|List1]
end
end,
RetList = lists:foldl(Fun, [], List),
lists:reverse(RetList).
lists.erl 模块注意事项:
(1)
append/1: 实现中用到 ++
append([E]) -> E;
append([H|T]) -> H ++ append(T);
append([]) -> [].
(2)
delete/2 实现用递归,却非尾递归
delete(Item, [Item|Rest]) -> Rest;
delete(Item, [H|Rest]) ->
[H|delete(Item, Rest)];
delete(_, []) -> [].
(3)
dropwhile/2
dropwhile(Pred, [Hd|Tail]=Rest) ->
case Pred(Hd) of
true -> dropwhile(Pred, Tail);
false -> Rest
end;
dropwhile(Pred, []) when is_function(Pred, 1) -> [].
删除函数返回true的元素,如果遇到返回false立即返回剩余的列表,不管以后是否能有返回true的元素
(4)
duplicate/2
duplicate(N, X) when is_integer(N), N >= 0 -> duplicate(N, X, []).
duplicate(0, _, L) -> L;
duplicate(N, X, L) -> duplicate(N-1, X, [X|L]).
如果不要is_integer的guard语句,当传入参数N非整形,返回bad argument in an arithmetic expression, 否则返回
no function clause matching lists:duplicate
(5)
lists里面的bif
%% Bifs: member/2, reverse/2
%% Bifs: keymember/3, keysearch/3, keyfind/3
(6)
suffix/2
suffix(List1, List2)
List1如果是List2的子列表,结尾子列表,则为true,否则false
(7)
不少的对元祖组成的列表处理,
keydelete
keyfind
keymap
keymember
keymerge
keyreplace
...
lists主要是对list的各种处理,包括各种复杂的元素结构,例如元祖组成的列表等