list相关的习题

代码如下

 1 -module(adv_list).
 2 -export([filter/2, reverse/1, concatenate/1, flatten/1]).
 3 
 4 filter(List, Value) ->
 5     FiltedList = [X || X<-List, X=<Value],
 6     io:format("filted:~w~n", [FiltedList]).
 7 
 8 reverse(List) ->
 9     RevertedList = revert(List, []),
10     io:format("reverted:~w~n", [RevertedList]),
11     RevertedList.
12 
13 revert([H|T], RevertedList) ->
14     revert(T, [H] ++ RevertedList);
15 revert([], RevertedList) ->
16     RevertedList.
17 
18 concatenate(List) ->
19     ConcatedList = concate(List, []),
20     io:format("concated:~w~n", [ConcatedList]).
21 
22 concate([H|T], ConcatedList) ->
23     concate(T, reverse(H) ++ ConcatedList);
24 concate([], ConcatedList) ->
25     reverse(ConcatedList).
26 
27 flatten(List) ->
28     FlatedList = reverse(flat(List, [])),
29     io:format("flated:~w~n", [FlatedList]).
30 
31 flat([H|T], FlatedList) ->
32     case is_list(H) of
33         true -> flat(T, flat(H, FlatedList));
34         false -> flat(T, [H] ++ FlatedList)
35     end;
36 flat([], FlatedList) ->
37     FlatedList.

 

posted on 2012-12-21 17:22  应无所住而生其心  阅读(317)  评论(0编辑  收藏  举报

导航