_ | 覦覈襦 | 豕蠏手 | 殊螳 | 譯殊碁
FrontPage › Erlang襴

Contents

1 for
2 襴ろ語 螳?
3 Tuple 螳
4 襴ろ 伎(list comprehension)
5 quick sort
6 Permutations
7 Map
8 factorial
9 case if
10 try ... catch


1 for #

-module(lib_misc).
-export([for/3]).

for(Max, Max, F) -> [F(Max)];
for(I, Max, F) -> [F(I)|for(I+1, Max, F)].

2 襴ろ語 螳? #

-module(list_count).
-export([count/1]).

count([H|T]) -> 1 + count(T);
count([]) -> 0.

L = [1,2,3] 企朱..
  1. 1 + count([2,3])
  2. 1 + 1 + count([3])
  3. 1 + 1 + 1 + count([])
  4. 1 + 1 + 1 + 0

3 Tuple 螳 #

exam01.erl
-module(exam01).
-export([count/1]).

count(Tuple) -> count(tuple_to_list(Tuple), 0).
count([H|T], N) -> count(T, N+1);
count([], N) -> N.

1> c(exam01).
exam01.erl:5: Warning: variable 'H' is unused
{ok,exam01}
2> X = {1,2,{3,4}}.
{1,2,{3,4}}
3> exam01:count(X).
3

tuple_size()襯 伎覃 所 螳襯 蟲 .
4> tuple_size(X).
3
5>

.. 譟郁 暑. 襴ろ 螳
  1. {1}
  2. {2}
  3. {3,4}
襦 3螳. 3覯讌 襴ろ語企襦 願蟾讌 覈 螳語 螻 矩. れ螻 螳 企瓦.

exam02.erl
-module(exam02).
-export([loop/1]).

loop(Tuple) ->
        loop(Tuple, lists:seq(1, tuple_size(Tuple))).
loop(Tuple, [H|T]) ->
        L = to_list(H, Tuple),
        loop(Tuple, T, L).

loop(Tuple, [H|T], L) ->
        L1 = to_list(H, Tuple),
        loop(Tuple, T, L ++ L1);
loop(Tuple, [], L) -> length(L).

to_list(H, Tuple) ->
        case is_tuple(element(H,Tuple)) of
                true -> tuple_to_list(element(H,Tuple));
                false -> [element(H,Tuple)]
        end.

1> c(exam02).
exam02.erl:13: Warning: variable 'Tuple' is unused
{ok,exam02}
2> X = {1,2,{3,4}}.
{1,2,{3,4}}
3> exam02:loop(X).
4
4>

伎 譬 蠑語. 蠏碁 れ螻 螳 覦蠖覺る.
-module(exam03).
-export([count/1]).

count(Tuple) -> count(tuple_to_list(Tuple), 0).
count([H|T], N) ->
        case is_tuple(H) of
                true  -> count(T, N + tuple_size(H));
                false -> count(T, N + 1)
        end;
count([], N) -> N.

1> c(exam03).
{ok,exam03}
2> X = {1,2,{3,4}}.
{1,2,{3,4}}
3> exam03:count(X).
4
4>

4 襴ろ 伎(list comprehension) #

%%襴ろ語 2 螻燕蠍
L1=[1,2,3,4,5].
[2*X || X <- L1].

%% 朱Μ 螻燕蠍
L2 = [{1,3}, {2,3}].
[A*B || {A,B} <- L2].

%%{a, X} (X螳 企 螳企讌 蟯)襷 filter
L3= [{a,1}, {b,1}, {c,1}, {a,2}, {b,2}, {c,2}].
[X || {a, X} <- L3].
[{a,X} || {a, X} <- L3].

%%襴ろ L4 3覲企  襴ろ
L4 = [1,2,3,4,5].
[X || X <- L4, X > 3].

%%襴ろ 覲
[1,2,3] ++ [4,5,6].

%% 譟郁唄,  襴ろ
[{A, B} || A <- lists:seq(1,10), B <- lists:seq(11, 20), A rem 2 =:= 0, B rem 2 =:= 0].

%%Catesian Product
[[X]++[Y] || X<-"HT", Y<-"HT"].

erlang@A:~/work$ erl
Erlang (BEAM) emulator version 5.6.5 [source] [smp:4] [async-threads:0] [kernel-poll:false]

Eshell V5.6.5  (abort with ^G)
1> L1=[1,2,3,4,5].
[1,2,3,4,5]
2> [2*X || X <- L1].
[2,4,6,8,10]
3>
3> L2 = [{1,3}, {2,3}].
[{1,3},{2,3}]
4> [A*B || {A,B} <- L2].
[3,6]
5>
5> L3= [{a,1}, {b,1}, {c,1}, {a,2}, {b,2}, {c,2}].
[{a,1},{b,1},{c,1},{a,2},{b,2},{c,2}]
6> [X || {a, X} <- L3].
[1,2]
7> [{a,X} || {a, X} <- L3].
[{a,1},{a,2}]
8>
8> L4 = [1,2,3,4,5].
[1,2,3,4,5]
9> [X || X <- L4, X > 3].
[4,5]
10>
10> [1,2,3] ++ [4,5,6].
[1,2,3,4,5,6]
10>
11> [{A, B} || A <- lists:seq(1,10), B <- lists:seq(11, 20), A rem 2 =:= 0, B rem 2 =:= 0].
[{2,12},
 {2,14},
 {2,16},
 {2,18},
 {2,20},
 {4,12},
 {4,14},
 {4,16},
 {4,18},
 {4,20},
 {6,12},
 {6,14},
 {6,16},
 {6,18},
 {6,20},
 {8,12},
 {8,14},
 {8,16},
 {8,18},
 {8,20},
 {10,12},
 {10,14},
 {10,16},
 {10,18},
 {10,20}]
12>
12> [[X]++[Y] || X<-"HT", Y<-"HT"].
["HH","HT","TH","TT"]

5 quick sort #

-module(sort).
-export([qsort/1]).

qsort([Pivot|T]) ->
        qsort([X || X <- T, X < Pivot])
        ++ [Pivot] ++
        qsort([X || X <- T, X >= Pivot]);
qsort([]) -> [].
erlang@A:~/work$ erl
Erlang (BEAM) emulator version 5.6.5 [source] [async-threads:0] [kernel-poll:false]

Eshell V5.6.5  (abort with ^G)
1> c(sort).
{ok,sort}
2> sort:qsort([3,8,7,1,5]).
[1,3,5,7,8]
3>

襴ろ [3,8,7,1,5] ..
  1. Pivot 襴ろ語 る 3, 3 譴朱 殊曙 螳, るジ讓曙 蟇磯 螳 螳朱 覃 .
  2. 3覲企 螳 [1] ++ [3] ++ [8, 7, 5]
  3. 襴ろ [1]螻 [3] る襷 朱襦 qsort([]) -> []襯 襷 譬襭螻, [8, 7, 5] 8 Pivot [7, 5] ++ [8] ++ [], 譴螳蟆郁骸 [1] ++ [3] ++ [7, 5] ++ [8]
  4. 襴ろ [7, 5]襷 覃 . Pivot螳 7 7覲企 [5] ++ [7] ++ [].
  5. 豕譬蟆郁骸 [1] ++ [3] ++ [5] ++ [7] ++ [8]

6 Permutations #

1> L = [1,2,3].
[1,2,3]
2> [[X]++[Y] || X<-L,Y<-L].
[[1,1],[1,2],[1,3],[2,1],[2,2],[2,3],[3,1],[3,2],[3,3]]
3> [[X]++[Y] || X<-L,Y<-L--[X]].
[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
4>

[[X]++[Y] || X<-L,Y<-L--[X]] る(蠍一 * Catesian Product)
  • [[X]++[Y] || X<-L,Y<-L] L * L 讀, L螻 L Catesian Product
  • Y<-L Y<-L--[X]企, X<-L企襦 襴ろX 螳螳 伎
    • [1,2,3] -- [1] = [2,3]
    • [1,2,3] -- [2] = [1,3]
    • [1,2,3] -- [3] = [1,2]
  • 蟆郁骸..
    • 1 * [2,3] = [1,2], [1,3]
    • 2 * [1,3] = [2,1], [2,3]
    • 3 * [1,2] = [3,1], [3,2]
  • [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]

蟯語 豢螳襦 れ螻 螳 襷 .
1> L = [1,2,3].
[1,2,3]
2> [[H|T] || H<-L, T<-[L--[H]]].
[[1,2,3],[2,1,3],[3,1,2]]
3>
  • [1|[2,3]] = [1|[1,2,3]--[1]]
  • [2|[1,3]] = [2|[1,2,3]--[2]]
  • [3|[1,2]] = [3|[1,2,3]--[3]]

れ るジ ..
> [{X, Y} || X <- [1,2,3], Y <- [a,b]].
[{1,a},{1,b},{2,a},{2,b},{3,a},{3,b}]

-module(perms).
-export([perms/1]).

perms([]) -> [[]];
perms(L)  -> [[H|T] || H <- L, T <- perms(L--[H])].
%% 願碓  危危 3 蟇碁Π蟇 螳.. 讌讌 螳伎 蠏碁郁?? 

1> c(perms).
{ok,perms}
2> perms:perms([1,2,3]).
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

  1. [1|[2,3]] = [1|[1,2,3]--[1]] = [1,2,3] ... (1)
    1.1 (1)伎 蠏語
    1.1 [2,3] -- [2] = [3]
    1.1 [2,3] -- [3] = [2]
    1.1 [1|[3,2]] = [1,3,2]
  2. [2|[1,3]] = [2|[1,2,3]--[2]] = [2,1,3] ... (2)
    1.1 (2)伎 蠏語
    1.1 [1,3] -- [1] = [3]
    1.1 [1,3] -- [3] = [1]
    1.1 [2|[3,1]] = [2,3,1]
  3. [3|[1,2]] = [3|[1,2,3]--[3]] = [3,1,2] ... (3)
    1.1 (3)伎 蠏語
    1.1 [1,2] -- [1] = [2]
    1.1 [1,2] -- [2] = [1]
    1.1 [3|[2,1]]
  4. 蟆郁記.. [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

谿瑚襭

7 Map #

-module(mymap).
-export([map/2]).

map(_,[]) -> [];
map(F,[H|T]) -> [F(H)|map(F,T)].

F fun願, L = [1,2,3]企朱.. (谿瑚: [1|[2|[3]]] = [1,2,3] 讀, 螳 伎企)
  1. [F(1)|map(F,[2,3])]
  2. [F(1)|[F(2)|map(F,[3])]]
  3. [F(1)|[F(2)|[F(3)|map(F,[])]]]

erlang@A:~/work$ erl
Erlang (BEAM) emulator version 5.6.5 [source] [async-threads:0] [kernel-poll:false]

Eshell V5.6.5  (abort with ^G)
1> c(mymap).
{ok,mymap}
2> F = fun(X) -> X * X end.
#Fun<erl_eval.6.13229925>
3> L = [1,2,3].
[1,2,3]
4> mymap:map(F,L).
[1,4,9]
5>


8 factorial #

-module(mymath).
-export([factorial/1]).

factorial(1) -> 1;
factorial(N) -> N * factorial(N-1).

N = 4 企,
  1. 4 * factorial(4-1)
  2. 4 * 3 * factorial(3-1) ===> 螻1 factorial(4-1) 蟆郁骸 "3 * factorial(3-1)"企襦
  3. 4 * 3 * 2 * factorial(2-1) ===> 螻2 factorial(3-1) 蟆郁骸 "2 * factorial(2-1)"企襦
  4. 4 * 3 * 2 * 1 ===> 螻3 factorial(2-1) 蟆郁骸 "1" 企襦

10000!(facotrial(10000)) 1豐 蟇碁Π. 蠏瑚 VMWare 蠎讌 誤碁..

9 case if #

-module(misc).
-export([filter/2, max/2, filter2/2, acc/1, acc/3]).

filter(P, [H|T]) ->
        case P(H) of
                true -> [H|filter(P, T)];
                false -> filter(P,T)
        end;
filter(P,[]) -> [].

max(X,Y) ->
Z = if %% 蠍磯ゼ 譯朱!! if 蟆郁骸螳 Z襦 覦企 .
        X< Y -> Y;
        true -> X
end, %% 碁語襦(;)  貊る(,)!!!!!
Z.

filter2(P, [H|T]) ->
ReturnVal = P(H),

if
        ReturnVal =:= true -> [H|filter2(P,T)];
        ReturnVal =:= false -> filter2(P,T)
end;
filter2(P, []) -> [].

%%襴ろ L  覯襷 曙伎 2螳 襴ろ 襷り鍵
acc(L) -> acc(L, [], []).

acc([H|T], Odds, Evens) ->
case (H rem 2) of
        1 -> acc(T, [H|Odds], Evens);
        0 -> acc(T, Odds, [H|Evens])
end;

acc([], Odds, Evens) -> {lists:reverse(Odds), lists:reverse(Evens)}.

5> L = [1,2,3,4,5,6,7,8,9].
* 2: syntax error before: 2
5> L = [1,2,3,4,5,6,7,8,9].
[1,2,3,4,5,6,7,8,9]
6> P = fun(X) -> X rem 2 =:= 0 end.
#Fun<erl_eval.6.13229925>
7> misc:filter(P,L).
[2,4,6,8]
8> misc:max(2,3).
3
9> misc:filter2(P,L).
[2,4,6,8]
10> misc:acc(L). 
{[1,3,5,7,9],[2,4,6,8]}

10 try ... catch #

-module(try_test).
-export([demo1/0, demo2/0, demo3/0]).


generate_exception(1) -> a;
generate_exception(2) -> throw(a);
generate_exception(3) -> exit(a);
generate_exception(4) -> {'EXIT', a};
generate_exception(5) -> erlang:error(a).

demo1() -> [catcher(I) || I <- [1,2,3,4,5]].
demo2() -> [{I, (catch generate_exception(I))} || I <- [1,2,3,4,5]].
demo3() ->
try generate_exception(5)
catch
        error:X -> {X, erlang:get_stacktrace()}
end.

catcher(N) ->
try generate_exception(N) of
        Val -> {N, normal, Val}
catch
        throw:X -> {N, caught, throw, X};
        exit:X -> {N, caught, exited, X};
        error:X -> {N, caught, error, X}
end.
蠍 蠍郁鍵..
企: : るジ讓曙 襦螻豺 企Ν 譯殊語. 襦螻豺
EditText : Print : Mobile : FindPage : DeletePage : LikePages : Powered by MoniWiki : Last modified 2018-04-13 23:12:53

蠍一 螳焔慨 る 螳煙 焔 襴旧給.