利用List Comprehension過慮列表里的項目

2009-04-02
Erlang的數据結構十分容易進行匹配和過慮,下面演示個例子:
首先定義一個結構體
1> rd(person, {name, gender}).

創建一個person列表
2> List=[
2> {person, oscar, male},
2> {person, jojo, female},
2> {person, god, male},
2> {person, jessica, female}].

我們現在過慮出所有gender為male的人的名字
3> [P#person.name || P <- List, P#person.gender==male].
[oscar, god]

再將名為jojo的項目過慮出來
4> [P || P <- List, P#person.name==jojo].
[#person{name = jojo, gender = female}]

很簡便吧~~有興趣的可詳細參見這里

M-OSCAR | Powered by Blogger | Entries (RSS) | Comments (RSS) | Designed by MB Web Design | XML Coded By Cahayabiru.com