-module(lib_misc). -export([for/3]). for(Max, Max, F) -> [F(Max)]; for(I, Max, F) -> [F(I)|for(I+1, Max, F)].
-module(list_count). -export([count/1]). count([H|T]) -> 1 + count(T); count([]) -> 0.
-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
4> tuple_size(X). 3 5>
-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>
%%襴ろ語 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"]
-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>
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>
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>
> [{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]]
-module(mymap). -export([map/2]). map(_,[]) -> []; map(F,[H|T]) -> [F(H)|map(F,T)].
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>
-module(mymath). -export([factorial/1]). factorial(1) -> 1; factorial(N) -> N * factorial(N-1).
-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]}
-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.