题目一
替换元组或列表中指定位置的元素,新元素作为参数和列表或元组一起传入函数内
解答
replaceIdx(List, Index, Val) ->replaceIdx(List, Index, Val, 1, []).replaceIdx([], _, _, _, Acc) ->lists:reverse(Acc);%% 到达替换位置的处理replaceIdx([_ | Rest], Index, Val, Index, Acc) ->io:format("[~p]~n", [[Val | Acc]]),replaceIdx(Rest, Index, Val, Index + 1, [Val | Acc]);replaceIdx([Element | Rest], Index, Val, CurrentIndex, Acc) ->io:format("[~p]~n", [[Element | Acc]]),replaceIdx(Rest, Index, Val, CurrentIndex + 1, [Element | Acc]).
题目二
指定列表第几位之后的数据进行反转。如:指定[2,3,5,6,7,2]第3位后进行反转
解答
%% 和并两个列表
merge_list(List1, List2) ->merge_list(List1, List2, []).merge_list([], [], Acc) -> lists:reverse(Acc);
merge_list([H | T], List2, Acc) ->merge_list(T, List2, [H | Acc]);
merge_list([], [H | T], Acc) ->merge_list([], T, [H | Acc]).%% 指定列表第几位之后的数据进行反转。如:指定[2,3,5,6,7,2]第3位后进行反转为 [2,3,5,2,7,6]
reverse_n(List, N) ->Sublist = lists:sublist(List, N + 1, length(List) - N),NewSublist = lists:reverse(Sublist),merge_list(lists:sublist(List, 3), NewSublist).
题目三
对列表进行过滤,输出列表所有的奇数和偶数
解答
filteroe(List) ->Odds = [X || X<-List, X rem 2 /= 0],Evens = [X || X<-List, X rem 2 == 0],io:format("Odds = ~p ~nEvens = ~p ~n", [Odds, Evens]).
题目四
使用匿名函数对列表进行过滤,输出列表所有的奇数和偶数(可以使用API)
解答
filter_odd_even(List) ->Odds = lists:filter(fun(X) -> X rem 2 /= 0 end, List),Evens = lists:filter(fun(X) -> X rem 2 =:= 0 end, List),{Odds, Evens}.
题目五
对数字列表或者元组中所有的奇数进行求和
解答
sum_odd(List) ->lists:sum([X || X<- List, X rem 2 /= 0]).
题目六
对数字列表或元组,输出所有偶数乘以它在此列表或元组中的偶数位数
比如在列表[3,4,8,9,7,2,5]中8所在此列表中的偶数位数为2,2所在此元组中的偶数位数为3
解答
get_evens_mul_Idx(List) ->get_evens_mul_Idx(List, 1, []).%% 递归终止get_evens_mul_Idx([], _, Acc) -> lists:reverse(Acc);%% 处理遇到偶数的情况get_evens_mul_Idx([Value | Rest], Index, Acc) when Value rem 2 == 0 ->get_evens_mul_Idx(Rest, Index + 1, [Value * Index | Acc]);%% 非偶数的情况,下标增加,其他不变get_evens_mul_Idx([_ | Rest], Index, Acc) ->get_evens_mul_Idx(Rest, Index + 1, Acc).
题目七
将指定的元素插入到列表或元组中指定的位置,列表或元组中后面的元素依次向后挪动
解答
insert_at(List, Index, Val) ->insert(List, Index, Val, 1, []).insert([], _, _, _, Acc) -> lists:reverse(Acc);%% 处理Index之前的元素,原样插入insert([Value | Rest], Index, Val, CurIdx, Acc) when CurIdx /= Index ->insert(Rest, Index, Val, CurIdx + 1, [Value | Acc]);%% 到达插入位置的处理insert(List, Index, Val, Index, Acc) ->insert(List, Index, Val, Index + 1, [Val | Acc]).
题目八
用列表输出在列表或元组中查找到的的所有重复元素
解答
find_dup(Items) ->find_dup(Items, []).find_dup([], Duplicates) ->Duplicates;%% Acc 是用来存储重复值只存一次find_dup([Item | Rest], Acc) ->case lists:member(Item, Rest) and not lists:member(Item, Acc) of % 如果答案集合已经存在了重复元素,就不要加入true -> find_dup(Rest, [Item | Acc]);false -> find_dup(Rest, Acc)end.
题目九
删出列表或元组中的所有重复元素
解答
%% 思路:就是把元素加入到新的列表中,重复的不加入 0delete_dup(Items) ->delete_dup(Items, []).delete_dup([], Acc) -> Acc;delete_dup([Item | Rest], Acc) ->case lists:member(Item, Acc) of % 元素没有出现在结果集中就加入false -> delete_dup(Rest, [Item | Acc]);true -> delete_dup(Rest, Acc)end.
题目十
使用冒泡排序对列表进行排序(升序)
解答
%% 取列表头作为最大值和bubble_sort(List) ->bubble_sort(List, length(List)).bubble_sort(List, 0) -> List; % 当迭代次数为 0 时,排序完成bubble_sort(List, N) ->SortedList = bubble_pass(List, N), % 对列表进行下一趟冒泡,一个元素到达最终位置bubble_sort(SortedList, N - 1). % 递归的进行下一趟冒泡bubble_pass([X, Y | Rest], N) when X > Y ->[Y | bubble_pass([X | Rest], N - 1)]; % 如果 X > Y 就交换他们bubble_pass([X | Rest], N) ->[X | bubble_pass(Rest, N - 1)]; % 否则位置保持不变bubble_pass([], _) -> [].