1、将列表中的integer,float,atom转成字符串并合并成一个字个字符串:[1,a,4.9,"sdfds"] 结果:"1a4.9sdfds"(禁用++ -- append concat实现)
-module(test1).
-export([list_to_string/1, get_str/1]).
list_to_string([]) -> "";
list_to_string([H|T]) ->
if
is_integer(H) -> [integer_to_list(H)|list_to_string(T)];
is_float(H) -> [float_to_list(H, [{decimals, 2}])|list_to_string(T)];
is_atom(H) -> [atom_to_list(H)|list_to_string(T)];
true -> [H|list_to_string(T)]
end.
get_str(L) -> binary_to_list(list_to_binary(list_to_string(L))).
2、得到列表或元组中的指定位的元素 {a,g,c,e} 第1位a [a,g,c,e] 第1位a(禁用erlang lists API实现)
-module(test2).
-export([get_num/2, get_num1/2]).
get_num(L, K) ->
case is_tuple(L) of
true -> get_num1(tuple_to_list(L), K);
false -> get_num1(L, K)
end.
get_num1([], K) -> -1;
get_num1([H|T], K) ->
if
K =< 0 -> -1;
K =:= 1 -> H;
true -> get_num1(T, K - 1)
end.
3、根据偶数奇数过淲列表或元组中的元素(禁用API实现)
-module(test3).
-export([odds_and_evens/1]).
odds_and_evens([]) -> [];
odds_and_evens({}) -> {};
odds_and_evens(L) ->
case is_tuple(L) of
true -> odds_and_evens1(tuple_to_list(L), [], []);
false -> odds_and_evens1(L, [], [])
end.
odds_and_evens1([H|T], Odds, Evens) ->
case (H rem 2) of
1 -> odds_and_evens1(T, [H|Odds], Evens);
0 -> odds_and_evens1(T, Odds, [H|Evens])
end;
odds_and_evens1([], Odds, Evens) ->
{lists:reverse(Odds), lists:reverse(Evens)}.
4、便用匿名函数对列表奇数或偶数过淲
-module(test4).
-export([odds_and_evens/2]).
odds_and_evens(F, []) -> [];
odds_and_evens(F, {}) -> {};
odds_and_evens(F, L) ->
case is_tuple(L) of
true -> odds_and_evens1(tuple_to_list(L), F);
false -> odds_and_evens1(L, F)
end.
odds_and_evens1(L, F) ->
[X || X <- L, F(X)].
5、计算数字列表[1,2,3,4,5,6,7,8,9]索引N到M的和
-module(test5).
-export([sum/3]).
sum([], N, M) -> [];
sum(L, N, M) -> for(L, 1, N, M).
for([], I, N, M) -> 0;
for([H|T], I, N, M) ->
if
I >= N, I =< M ->
H + for(T, I + 1, N, M);
true -> for(T, I + 1, N, M)
end.
6、查询List1是为List2的前缀(禁用string API实现)
-module(test6).
-export([is_pre/2]).
is_pre([], []) -> true;
is_pre([], L2) -> true;
is_pre(L1, []) -> false;
is_pre([H1|T1], [H2|T2]) ->
case H1 =:= H2 of
true -> is_pre(T1, T2);
false -> false
end.
7、逆转列表或元组(禁用lists API实现)
-module(test7).
-export([reverse/1]).
reverse([]) -> [];
reverse({}) -> {};
reverse(L) ->
if
is_tuple(L) -> reverse1(tuple_to_list(L), [], true);
true -> reverse1(L, [], false)
end.
reverse1([H|T], L1, Flag) ->
reverse1(T, [H|L1], Flag);
reverse1([], L1, Flag) ->
case Flag of
true -> list_to_tuple(L1);
false -> L1
end.
8、对列表进行排序
-module(test8).
-export([qsort/1]).
qsort([]) -> [];
qsort([Pivot|T]) ->
qsort([X || X <- T, X =< Pivot])
++ [Pivot] ++
qsort([X || X <- T, X >= Pivot]).
9、对数字列表进行求和再除以指定的参数,得到商余
-module(test9).
-export([sum_rem/2]).
sum_rem(L, K) ->
{sum(L) / K, sum(L) rem K}.
sum([]) -> 0;
sum([H|T]) -> H + sum(T).
10、获得当前的堆栈
Trace = try throw(ap114) catch
ap114 -> erlang:get_stacktrace()
end,
erlang:display(Trace)
11、获得列表或元组中的最大最小值(禁用API实现)
-module(test11).
-export([max_min/1]).
max_min([]) -> [];
max_min({}) -> {};
max_min(L) ->
case is_tuple(L) of
true -> L1 = tuple_to_list(L),
[H|T]=L1,
max_min1(T, H, H);
false ->
[H|T]=L,
max_min1(T, H, H)
end.
max_min1([], Max, Min) -> {Max, Min};
max_min1([H|T], Max, Min) ->
if
H > Max -> A = H;
true -> A = Max
end,
if
H < Min -> B = H;
true -> B = Min
end,
max_min1(T, A, B).
12、查找元素在元组或列表中的位置
-module(test12).
-export([find_index/2]).
find_index([], _) -> -1;
find_index({}, _) -> -1;
find_index(L, K) ->
case is_tuple(L) of
true -> find_index1(tuple_to_list(L), K, 1);
false -> find_index1(L, K, 1)
end.
find_index1([], K, Index) -> -1;
find_index1([H|T], K, Index) ->
if
H =:= K -> Index;
true -> find_index1(T, K, Index + 1)
end.
14、判断A列表([3,5,7,3])是否在B列表([8,3,5,3,5,7,3,9,3,5,6,3])中出现,出现则输出在B列表第几位开始
-module(test14).
-export([is_match/2]).
is_match(L1, L2) ->
case is_tuple(L1) of
true -> A = tuple_to_list(L1);
false -> A = L1
end,
case is_tuple(L2) of
true -> B = tuple_to_list(L2);
false -> B = L2
end,
is_match1(A, B, 1).
is_match1([], _, _) -> false;
is_match1(_, [], _) -> false;
is_match1([H1|T1], [H2|T2], Index) ->
if
is_match2([H1|T1], [H2|T2]) -> {true, Index};
true -> is_match1(T1, [H2|T2], Index + 1)
end.
is_match2(_, []) -> true;
is_match2([], _) -> false;
is_match2([H1|T1], [H2|T2]) ->
if
H1 =:= H2 -> is_match2(T1, T2);
true -> false
end.
15、{8,5,2,9,6,4,3,7,1} 将此元组中的奇数进行求和后除3的商(得值A),并将偶数求和后剩3(得值B),然后求A+B结果
-module(test15).
-export([a_and_b/1]).
a_and_b(L) ->
{Odds, Evens} = odds_and_evens_acc(tuple_to_list(L), [], []),
{sum(Odds), sum(Evens)}.
odds_and_evens_acc([H|T], Odds, Evens) ->
case (H rem 2) of
1 -> odds_and_evens_acc(T, [H|Odds], Evens);
0 -> odds_and_evens_acc(T, Odds, [H|Evens])
end;
odds_and_evens_acc([], Odds, Evens) ->
{Odds, Evens}.
sum([]) -> 0;
sum([H|T]) -> H + sum(T).
16、在shell中将unicode码显示为中文
io:format("cnkizy是个帅哥").
unicode:characters_to_list(list_to_binary("cnkizy是个帅哥")).
17、传入列表L1=[K|_]、L2=[V|_]、L3=[{K,V}|_],L1和L2一一对应,L1为键列表,L2为值列表,L3存在重复键,把L3对应键的值加到L2
-module(test17).
-export([function/3]).
function(L1, L2, L3) ->
maps:to_list(function1(maps:from_list(merge(L1, L2)), L3)).
merge([], []) -> [];
merge([H1|T1], [H2|T2]) ->
[{H1, H2}|merge(T1, T2)].
function1(Map, []) -> Map;
function1(Map, [{Key, Val}|T]) ->
case maps:is_key(Key, Map) of
true -> function1(Map#{Key := maps:get(Key, Map) + Val}, T);
false -> function1(Map, T)
end.
18、删除或查询元组中第N个位置的值等于Key的tuple(禁用lists API实现)
-module(test18).
-export([find/2, delete/2]).
find({}, _) -> false;
find(L, Key) ->
find1(tuple_to_list(L), Key, 1).
find1([], _, _) -> false;
find1([H|T], Key, Index) ->
case H =:= Key of
true -> {true, Index};
false -> find1(T, Key, Index + 1)
end.
delete({}, _) -> {};
delete(L, Key) ->
delete1(tuple_to_list(L), Key, []).
delete1([], Key, L1) -> lists:reverse(L1);
delete1([H|T], Key, L1) ->
if
H =:= Key -> delete1(T, Key, L1);
true -> delete1(T, Key, [H|L1])
end.
19、对一个字符串按指定符字劈分(禁用string API实现)
-module(test19).
-export([cut_string/2]).
cut_string(S, C) ->
cut_string1(S, C, [], []).
cut_string1([], _, S1, S2) -> {S1, S2};
cut_string1(_, _, S1, S2) when S2 =/= [] -> {S1, S2};
cut_string1([H|T], C, S1, S2) ->
if
H =:= C -> cut_string1(T, C, S1, S2 ++ T);
true -> cut_string1(T, C, S1 ++ [H], S2)
end.
20、合并多个列表或元组
-module(test20).
-export([merge/1]).
merge([]) -> [];
merge(L) -> merge_acc(L, []).
merge_acc([], L) -> L;
merge_acc([H|T], L) ->
if
is_tuple(H) ->
merge_acc(T, L ++ tuple_to_list(H));
is_list(H) ->
merge_acc(T, L ++ H);
true ->
merge_acc(T, L ++ [H])
end.
21、{5,1,8,7,3,9,2,6,4} 将偶数剩以它在此元组中的偶数位数, 比如,8所在此元组中的偶数位数为1,2所在此元组中的偶数位数为2
-module(test21).
-export([function/1]).
function({}) -> {};
function(L) ->
function1(tuple_to_list(L), [], 1).
function1([], L1, _) ->
list_to_tuple(lists:reverse(L1));
function1([H|T], L1, Index) ->
case H rem 2 =:= 0 of
true -> function1(T, [H*Index|L1], Index + 1);
false -> function1(T, [H|L1], Index)
end.
22、排序[{"a",5},{"b",1},{"c",8},{"d",7},{"e",3},{"f",9},{"g",2},{"h",6},{"i",4}], 以元组的值进行降序 优先用API
-module(test22).
-export([sort/1]).
sort(L) ->
lists:sort(fun({A1, B1}, {A2, B2}) -> B1 > B2 end, L).
23、{8,5,2,9,6,4,3,7,1} 将此元组中的奇数进行求和
-module(test23).
-export([odd_sum/1]).
odd_sum(L) ->
odd_sum1(tuple_to_list(L)).
odd_sum1(L) ->
sum([X || X <- L, X rem 2 =:= 1]).
sum([]) -> 0;
sum([H|T]) -> H + sum(T).
24、传入任意I1、I2、D、Tuple四个参数,检查元组Tuple在索引I1、I2位置的值V1、V2,如果V1等于V2则删除V1,把D插入V2前面,返回新元组,如果V1不等于V2则把V2替换为V1,返回新元组
-module(test24).
-export([function/4]).
function(Pos1, Pos2, D, L) ->
list_to_tuple(function_acc(Pos1, Pos2, D, tuple_to_list(L))).
function_acc(Pos1, Pos2, D, L) ->
V1 = get_element(L, Pos1),
V2 = get_element(L, Pos2),
case (V1 =:= false) or (V2 =:= false) of
true -> "索引值错误!";
false -> case V1 =:= V2 of
true -> insert(delete(Pos1, L), case Pos2 > Pos1 of
true -> Pos2 - 1;
false -> Pos2 end, D);
false -> updata(L, Pos1, V2)
end
end.
get_element(L, Pos) ->
get_element_acc(L, Pos, 1).
get_element_acc([], _, _) -> false;
get_element_acc([H|T], Pos, Index) ->
case Pos =:= Index of
true -> H;
false -> get_element_acc(T, Pos, Index + 1)
end.
delete(Pos, L) ->
delete_acc(L, [], Pos, 1).
delete_acc([], L1, _, _) -> lists:reverse(L1);
delete_acc([H|T], L1, Pos, Index) ->
case Index =:= Pos of
true -> delete_acc(T, L1, Pos, Index + 1);
false -> delete_acc(T, [H|L1], Pos, Index + 1)
end.
insert(L, Pos, V) ->
insert_acc(L, [], Pos, V, 1).
insert_acc([], L1, _, _, _) -> lists:reverse(L1);
insert_acc([H|T], L1, Pos, V, Index) ->
case Index =:= Pos of
true -> insert_acc([H|T], [V|L1], Pos, V, Index + 1);
false -> insert_acc(T, [H|L1], Pos, V, Index + 1)
end.
updata(L, Pos, V) ->
updata_acc(L, [], Pos, V, 1).
updata_acc([], L1, _, _, _) -> lists:reverse(L1);
updata_acc([H|T], L1, Pos, V, Index) ->
case Index =:= Pos of
true -> updata_acc(T, [V|L1], Pos, V, Index + 1);
false -> updata_acc(T, [H|L1], Pos, V, Index + 1)
end.
25、删除列表或元组中全部符合指定键的元素
-module(test25).
-export([delete/2]).
delete(L, Key) ->
case is_tuple(L) of
true ->
list_to_tuple(delete_acc(tuple_to_list(L), [], Key));
false ->
delete_acc(L, [], Key)
end.
delete_acc([], L1, _) -> lists:reverse(L1);
delete_acc([H|T], L1, Key) ->
case H =:= Key of
true -> delete_acc(T, L1, Key);
false -> delete_acc(T, [H|L1], Key)
end.
26、替换列表或元组中第一个符合指定键的元素
-module(test26).
-export([updata/3]).
updata(L, Key, X) ->
case is_tuple(L) of
true ->
list_to_tuple(updata_acc(tuple_to_list(L), [], Key, X));
false ->
updata_acc(L, [], Key, X)
end.
updata_acc([], L1, _, _) -> lists:reverse(L1);
updata_acc([H|T], L1, Key, X) ->
case H =:= Key of
true -> updata_acc(T, [X|L1], Key, X);
false -> updata_acc(T, [H|L1], Key, X)
end.
27、将指定的元素插入到列表中指定的位置,列表中后面的元素依次向后挪动
-module(test27).
-export([insert/3]).
insert(L, Pos, Ele) ->
insert_acc(L, [], Pos, Ele, 1).
insert_acc([], L1, _, _, _) -> lists:reverse(L1);
insert_acc([H|T], L1, Pos, Ele, Index) ->
case Pos =:= Index of
true -> insert_acc([H|T], [Ele|L1], Pos, Ele, Index + 1);
false -> insert_acc(T, [H|L1], Pos, Ele, Index + 1)
end.
28、对[6,4,5,7,1,8,2,3,9]进行排序(降序--冒泡排序)或API排序
-module(test28).
-export([sort/1, bubble_sort/1]).
sort(L) ->
lists:sort(fun(A, B) -> A > B end, L).
bubble_sort(L)->
bubble_sort(L,length(L)).
bubble_sort(L,0)-> L;
bubble_sort(L,N)->
bubble_sort(do_bubble_sort(L),N-1).
do_bubble_sort([A])-> [A];
do_bubble_sort([A,B|T])->
case A > B of
true -> [A|do_bubble_sort([B|T])];
false -> [B|do_bubble_sort([A|T])]
end.
30、移除元组或列表中指定位置的元素
-module(test30).
-export([delete/2]).
delete(L, Pos) ->
case is_tuple(L) of
true ->
list_to_tuple(delete_acc(tuple_to_list(L), [], Pos, 1));
false ->
delete_acc(L, [], Pos, 1)
end.
delete_acc([], L1, _, _) -> lists:reverse(L1);
delete_acc([H|T], L1, Pos, Index) ->
case Pos =:= Index of
true -> delete_acc(T, L1, Pos, Index + 1);
false -> delete_acc(T, [H|L1], Pos, Index + 1)
end.
31、指定列表第几位之后的数据进行反转。如:指定[2,3,5,6,7,2]第3位后进行反转(谢绝一切API(lists:concat,lists:reverse等)、++、--方法)
-module(test31).
-export([part_reverse/2]).
part_reverse(L, Pos) ->
{L1, L2} = part_reverse_acc(L, [], Pos, 1),
merge(L1, L2).
part_reverse_acc([], L1, _, _) -> {L1, []};
part_reverse_acc([H|T], L1, Pos, Index) ->
case Pos =:= Index of
true -> {L1, [H|T]};
false -> part_reverse_acc(T, [H|L1], Pos, Index + 1)
end.
merge(L1, L2) ->
merge_acc(L1, L2, []).
merge_acc([], [], L3) -> L3;
merge_acc([H1|T1], [], L3) -> merge_acc(T1, [], [H1|L3]);
merge_acc([H1|T1], [H2|T2], L3) ->
merge_acc([H1|T1], T2, [H2|L3]).
32、使用“匿名函数”检查列表的每个元素是否符合传入的最大最小值(都是闭区间)
-module(test32).
-export([is_interval/3]).
is_interval(L, Min, Max) ->
lists:map(fun(X) -> (Min =< X) and (X =< Max) end, L).
33、获得列表或元组的最大最小值
-module(test33).
-export([max_min/1]).
max_min({}) -> {};
max_min([]) -> [];
max_min(L) ->
case is_tuple(L) of
true ->
L1 = tuple_to_list(L),
[H|T] = L1,
max_min_acc(tuple_to_list(L), H, H);
false ->
[H|T] = L,
max_min_acc(L, H, H)
end.
max_min_acc([], Max, Min) -> {Max, Min};
max_min_acc([H|T], Max, Min) ->
case H > Max of
true -> A = H;
false -> A = Max
end,
case H < Min of
true -> B = H;
false -> B = Min
end,
max_min_acc(T, A, B).
34、成生指定数量元组,每个元素为随机integer元素, 元素不能有相同的
-module(test34).
-export([function/1]).
function(Num) ->
list_to_tuple(function_acc(Num, [])).
function_acc(0, List) -> List;
function_acc(Num, List) ->
{_,_,BigInt} = os:timestamp(),
Random = rand:uniform(BigInt),
case lists:member(Random, List) of
true -> function_acc(Num, List);
false -> function_acc(Num - 1, [Random|List])
end.