`
ZacMa
  • 浏览: 39097 次
  • 来自: 深圳
社区版块
存档分类
最新评论

<15> 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的各种处理,包括各种复杂的元素结构,例如元祖组成的列表等

分享到:
评论

相关推荐

    C++性能优化 指南(强列推荐) chm版

    Storage Structures &lt;br&gt; Arrays &lt;br&gt; Linked Lists &lt;br&gt; Hash Tables &lt;br&gt; Binary Trees &lt;br&gt; Red/Black Trees &lt;br&gt; Summary &lt;br&gt; &lt;br&gt; Chapter 12. Optimizing IO &lt;br&gt; Efficient Screen Output &lt;br&gt; Efficient ...

    C /C++库函数及文件大全 经典 chm

    各种函数以及库文件的...Ended Queues &lt;br&gt;C++ Lists &lt;br&gt;C++ Priority Queues &lt;br&gt;C++ Queues &lt;br&gt;C++ Stacks &lt;br&gt;C++ Sets &lt;br&gt;C++ Multisets &lt;br&gt;C++ Maps &lt;br&gt;C++ Multimaps &lt;br&gt;C++ Bitsets &lt;br&gt;Iterators &lt;br&gt;

    The C programming Language

    &lt;br&gt;Published by Prentice-Hall in 1988 &lt;br&gt;&lt;br&gt;ISBN 0-13-110362-8 (paperback)&lt;br&gt;ISBN 0-13-110370-9 &lt;br&gt;&lt;br&gt;Contents&lt;br&gt;Preface &lt;br&gt;Preface to the first edition &lt;br&gt;Introduction &lt;br&gt;Chapter 1: A ...

    The Scheme

    Lists and Pairs &lt;br&gt;Section 6.4. Numbers &lt;br&gt;Section 6.5. Characters &lt;br&gt;Section 6.6. Strings &lt;br&gt;Section 6.7. Vectors &lt;br&gt;Section 6.8. Symbols &lt;br&gt;&lt;br&gt;Chapter 7. Input and Output &lt;br&gt;Section 7.1. ...

    How_To_Think_Like_A_Computer_Scientist(C++)

    Linked lists &lt;br&gt;Chapter 19: Stacks &lt;br&gt;Chapter 20: Queues and Priority Queues &lt;br&gt;Chapter 21: Trees &lt;br&gt;Chapter 22: Heap &lt;br&gt;Chapter 23: File Input/Output and pmatrices &lt;br&gt;Appendix A: Quick ...

    hibernate 教程

    映射文件的模块化(Modular mapping files)&lt;br&gt;6. 集合类(Collections)映射&lt;br&gt;6.1. 持久化集合类(Persistent Collections)&lt;br&gt;6.2. 映射集合(Mapping a Collection)&lt;br&gt;6.3. 值集合和多对多关联(Collections of...

    hibernate

    映射文件的模块化(Modular mapping files)&lt;br&gt;6. 集合类(Collections)映射&lt;br&gt;6.1. 持久化集合类(Persistent Collections)&lt;br&gt;6.2. 映射集合(Mapping a Collection)&lt;br&gt;6.3. 值集合和多对多关联(Collections of...

    Visual C++ 编程资源大全(英文源码 控件)

    (2KB)&lt;END&gt;&lt;br&gt;28,listfind.zip&lt;br&gt;This sample shows how to use the CList&lt;&gt; template. (9KB)&lt;END&gt;&lt;br&gt;29,ndbrow.zip&lt;br&gt;This Visual C++ 6 project shows how to create an MDI application that hosts ...

    Visual C++ 编程资源大全(英文源码 数据库)

    15.zip&lt;br&gt;Tips: ODBC & Access database&lt;br&gt;ODBC和Access数据库的使用技巧(5KB)&lt;END&gt;&lt;br&gt;16,16.zip&lt;br&gt;Using DBGrid in unbound mode&lt;br&gt;在unbound模式下使用DBGrid(6KB)&lt;END&gt;&lt;br&gt;17,ADORsWizard.zip&lt;br&gt;ADO Data ...

    数据结构与算法分析(Java版英文)

    数据结构与算法分析(Java版英文)目录&lt;br&gt;PartI&lt;br&gt;Chapter1-Overview &lt;br&gt;Chapter2-Arrays&lt;br&gt;Chapter3-Simple Sorting&lt;br&gt;PartII&lt;br&gt;Chapter4--Stacks and Queues&lt;br&gt;Chapter5-linked lists&lt;br&gt;Chapter^-Recursion...

    BSD HACKS -- 100个业界最尖端的技巧和工具

    Get the Most Out of grep&lt;br&gt; Section 15. Manipulate Files with sed&lt;br&gt; Section 16. Format Text at the Command Line&lt;br&gt; Section 17. Delimiter Dilemma&lt;br&gt; Section 18. DOS Floppy Manipulation&lt;br&gt; ...

    C#中List和数组之间转换的方法

    List&lt;string&gt; listS=new List&lt;string&gt;(); listS.Add(str); listS.Add(hello); string[] str=listS.ToArray(); 二、数组转List (从string[]转到List&lt;string&gt;) string[] str={str,string,abc}; List&lt;string&gt; listS=...

    rm2.6正式版本

    &lt;br&gt;&lt;br&gt; v.2.1(Build 2001/11/29)&lt;br&gt; - 修正了几个BUG.&lt;br&gt;&lt;br&gt; v.2.1(Build 2001/11/22)&lt;br&gt; - 修正了几个BUG.&lt;br&gt; - 增加多语言功能.&lt;br&gt;&lt;br&gt; v.2.1(Build 2001/11/15)&lt;br&gt; - 修正了几个BUG.&lt;br&gt;&lt;br&gt; v.2.1...

    VB编程资源大全(英文源码 网络)

    &lt;END&gt;&lt;br&gt;12 , telnet.zip&lt;br&gt;Telnet Application&lt;END&gt;&lt;br&gt;13 , validip.zip&lt;br&gt;Validate an IP address&lt;br&gt;14 , dmvbtest.zip&lt;br&gt;This is a complete email sending client in Visual Basic&lt;END&gt;&lt;br&gt;15 , ...

    VB编程资源大全(英文源码 数据库)

    When you click on a table, it lists all &lt;END&gt;&lt;br&gt;38,3a.zip&lt;br&gt;An Inventory System, you can add, edit, sort, find, delete, print, change your password and create a backup of your database&lt;END&gt;&lt;br&gt;39,A...

    实验2列表、表格与多媒体标签(学生端资料).zip

    HTML提供两种类型的列表:无序列表(unordered lists, `&lt;ul&gt;`)和有序列表(ordered lists, `&lt;ol&gt;`)。无序列表使用圆点或其他符号来分隔项目,而有序列表则使用数字或字母进行排序。每个列表项由`&lt;li&gt;`标签定义。...

    Visual-MinGW

    * Home Page&lt;br&gt;* http://visual-mingw.sourceforge.net/&lt;br&gt;*&lt;br&gt;* Mailing list&lt;br&gt;* http://sourceforge.net/mail/?group_id=46778&lt;br&gt;* visual-mingw-general@lists.sourceforge.net&lt;br&gt;*&lt;br&gt;* License&lt;br&gt;* ...

    SANS AUD 507 IOS IPv4 Access Lists.pdf

    ```ip access-list extended {&lt;number&gt; | &lt;name&gt;} [&lt;sequence&gt;] {permit | deny} &lt;protocol&gt; &lt;source&gt; [&lt;ports&gt;] &lt;destination&gt; [&lt;ports&gt;] [&lt;options&gt;]``` 在这个扩展版本中,`&lt;number&gt;`范围为100-199(扩展列表)或...

    CNNA笔记

    and Open Shortest Path First(OSPF)&lt;br&gt;第七章:Layer 2 Switching&lt;br&gt;第八章:Virtual LANs(VLANs)&lt;br&gt;第九章:Managing a Cisco Interwork&lt;br&gt;第十章:Managing Traffic with Access Lists&lt;br&gt;第十一章:...

Global site tag (gtag.js) - Google Analytics