1、将列表中的integer,float,atom转成字符串并合并成一个字个字符串:[1,a,4.9,“sdfds”] 结果:“1a4.9sdfds”(禁用++ – append concat实现)
-export([spliceString/1,getStringList/1]).
spliceString([])->"";
spliceString(List)->
L=getStringList(List),
binary_to_list(list_to_binary(L)).
getStringList([])->"";
getStringList([H|R])->
if
is_integer(H) ->IntString=erlang:integer_to_list(H),
[IntString|getStringList®] ;
is_float(H)->FloatString= erlang:float_to_list(H,[{decimals, 1}]),
[FloatString|getStringList®];
is_atom(H)->AtomString=erlang:atom_to_list(H),
[AtomString|getStringList®] ;
true -> [H|getStringList®]
end.
2、得到列表或无组中的指定位的元素 {a,g,c,e} 第1位a [a,g,c,e] 第1位a(禁用erlang lists API实现)
-export([getElementByPosition/2]).
getElementByPosition(List,Position)->
getElementByPosition(List,Position,1).
getElementByPosition([H|R],Position,Index) when Index == Position
->H;
getElementByPosition([H|R],Position,Index)
-> getElementByPosition(R,Position,Index+1).
3、根据偶数奇数过淲列表或元组中的元素
-export([filter/1]).
filter(List)->[X||X<-List,X rem 2 == 0].
4、便用匿名函数对列表奇数或偶数过淲
Even=fun(X)->(X rem 2)=:=0 end.//过滤掉奇数
L=[1,2,3,4,5,6,7,8,9].
lists:filter(Even,L).
5、计算数字列表[1,2,3,4,5,6,7,8,9]索引N到M的和
-export([sum/3]).
sum([H|T])->H+sum(T);
sum([])->0.
sum(List,N,M)->
sum(lists:sublist(List,N,M-N+1)).
6、查询List1是为List2的前缀(禁用string API实现)
-export([contains/2]).
-export([toList/3]).
-export([contains/3]).
toList(Str,S,[{return,list}])->re:split(Str,S,[{return,list}]).
contains(List1,List2)->
Delta=length(List1),
Delta>=0 andalso lists:sublist(List2,1,Delta)=:=List1.
contains(List1,List2,S)->
Delta=length(toList(List1,S,[{return,list}])),
Delta>=0 andalso lists:sublist(toList(List2,S,[{return,list}]),1,Delta)=:=toList(List1,S,[{return,list}]).
7、逆转列表或元组(禁用lists API实现)
reverse(List)->
reverse(List,[]).
reverse([Head|Rest],Reversed_List)->
reverse(Rest,[Head|Reversed_List]);
reverse([],Reversed_List)->Reversed_List.
8、对列表进行排序
-export([m_sort/1]).
m_sort(List)->d_sort(List,length(List)).
d_sort(List,0)->List;
d_sort(List,N)
->d_sort(sort(List),N-1).
sort([Head,Next|Rest]) when Head > Next
-> [Next|sort([Head|Rest])];
sort([Head,Next|Rest]) when Head=
sort([L])
->[L].
9、对数字列表进行求和再除以指定的参数,得到商余
-export([sum/1]).
-export([remAndDiv/2]).
sum([H|T])->H+sum(T);
sum([])->0.
remAndDiv(D,List)->
Consult=sum(List) div D,
Remainder=sum(List) rem D,
io:format(“the consult is:Bn”“the remainder is:Bn”,[Consult,Remainder]).
10、获得当前的堆栈
try generate_exception(5)
catch
error:X->
{X,erlang:get_stacktrace()}
end.
11、获得列表或元组中的最大最小值(禁用API实现)
-export([list_max/1]).
-export([print/0]).
list_max([Head|Rest]) ->
list_max(Rest, Head).
list_max([], Res) ->
Res;
list_max([Head|Rest], Result_so_far) when Head > Result_so_far ->
list_max(Rest, Head);
list_max([Head|Rest], Result_so_far) ->
list_max(Rest, Result_so_far).
print()->io:format("~B",[list_max([1,2,3,4,5])]).
12、查找元素在元组或列表中的位置
//获取第一个元素的位置
getPosition(Element,List)->getPosition(Element,List,1);
getPosition(Element,[])->getPosition(Element,[],1).
getPosition(Element,[],Index)->undefind;
getPosition(Element,[Head|Rest],Index)->
if Element =:=Head ->
Index;
true->
getPosition(Element,Rest,Index+1)
end.
//获取制定元素的所有位置
getPosition2(Element,List)->
N = lists:foldr(fun(X,Y) ->
case lists:nth(X,List) == Element of
true -> [X|Y];
false -> Y
end
end, [],lists:seq(1,length(List) )),
io:format(“The position is:pn”,[N]).
14、判断A列表([3,5,7,3])是否在B列表([8,3,5,3,5,7,3,9,3,5,6,3])中出现,出现则输出在B列表第几位开始
-export([isAppear/2,getPosition/2]).
getPosition(Element,List)->
Positions = lists:foldr(fun(X,Y) ->
case lists:nth(X,List) == Element of
true -> [X|Y];
false -> Y
end
end, [],lists:seq(1,length(List) )).
isAppear(List1,List2)->isAppear(List1,List2,1).
isAppear(List1,List2,Index) when Index=
FirstElementOfList1=lists:nth(1,List1),
Positions=getPosition(FirstElementOfList1,List2),
X= fun(Position)-> element(Position,list_to_tuple(Positions))end,
case
lists:sublist(List2,X(Index),length(List1)) == List1 of
true->io:format(“B contains A,the position is Bn”,[X(Index)]);
false->isAppear(List1,List2,Index+1)
end;
isAppear(List1,List2,Index)->io:format(“B doesn’t contains A~n”).
15、{8,5,2,9,6,4,3,7,1} 将此元组中的奇数进行求和后除3的商(得值A),并将偶数求和后剩3(得值B),然后求A+B结果
filterOdd(List)->[X||X<-List,X rem 2 == 0].
filterEven(List)->[X||X<-List,X rem 2 /= 0].
sum([H|T])->H+sum(T);
sum([])->0.
sumAndDiv(Tuple)->
List=tuple_to_list(Tuple),
Odd_List=filterEven(List),
Even_List=filterOdd(List),
Sum_Odd_List=sum(Odd_List),
Sum_Even_List=sum(Even_List),
A=Sum_Odd_List div 3,
B=Sum_Even_List * 3,
C=A+B,
io:format(“The sum of odd div 3 is A:BnThe sum of even muilt 3 is B:BnThe sum of A+B is C:Bn”,[A,B,C]).
16、在shell中将unicode码显示为中文
io:format(“test===pn”,[“测试”]).
io:format(“test====tsn”,[“测试”]).
unicode:characters_to_list(list_to_binary([230,181,139,232,175,149])).
io:format(“test===tsn”,[[27979,35797]]).
17、传入列表L1=[K|]、L2=[V|]、L3=[{K,V}|_]、,L1和L2一一对应,L1为键列表,L2为值列表,将L1和L2按位组合为[{k,v}]行式,完成后再和L3进行结合
mix([],[])->[];
mix([K|L1],[V|L2])->[{K,V}|mix(L1,L2)].
combination(List1,List2,List3)->
List3++mix(List1,List2).
18、删除或查询元组中第N个位置的值等于Key的tuple(禁用lists API实现)
-export([delete/3,select/3]).
delete(List,N,Key)->
delete3(List,N,Key).
delete3([H|T],N,Key) when element(N,H)==Key->
delete(T,N,Key);
delete3([H|T],N,Key)->[H|delete(T,N,Key)];
delete3([],,)->[].
select(List,N,Key)->
select3(List,N,Key).
select3([H|T],N,Key) when element(N,H)==Key->
[H|select(T,N,Key)];
select3([H|T],N,Key)->select(T,N,Key);
select3([],,)->[].
19、对一个字符串按指定符字劈分(禁用string API实现)
-export([piFenString/2,toList/3]).
toList(Str,S,[{return,list}])->re:split(Str,S,[{return,list}]).
piFenString(Str,S)->toList(Str,S,[{return,list}]).
20、合并多个列表或元组
-export([mergeList/1,mergeTuple/1]).
mergeList(List)->mergeList2(List).
mergeList2([H|R])->H ++ mergeList®;
mergeList2([])->[].
mergeTuple2(Tuple1List)->mergeTuple3(Tuple1List).
mergeTuple3([H|R])-> tuple_to_list(H) ++ mergeTuple2®;
mergeTuple3([])->[].
mergeTuple(Tuple1List)->
X=mergeTuple2(Tuple1List),
Y=list_to_tuple(X).
21、{5,1,8,7,3,9,2,6,4} 将偶数乘以它在此元组中的偶数位数, 比如,8所在此元组中的偶数位数为1,2所在此元组中的偶数位数为2
-export([multiEven/1]).
multiEven(List)->multiEven(List,1).
multiEven([],Index)->[];
multiEven([H|R],Index) -> if
H rem 2 == 0 ->
[H * Index|multiEven(R,Index+1)];
true->multiEven(R,Index)
end.
22、排序[{“a”,5},{“b”,1},{“c”,8},{“d”,7},{“e”,3},{“f”,9},{“g”,2},{“h”,6},{“i”,4}], 以元组的值进行降序 优先用API
-export([tupleSort/1]).
tupleSort(List)->tupleSort(List,2).
tupleSort(List,2)->lists:keysort(2,List).
23、{8,5,2,9,6,4,3,7,1} 将此元组中的奇数进行求和.
-export([tupleOddSum/1,sum/1,filterEven/1]).
sum([H|T])->H+sum(T);
sum([])->0.
filterEven(List)->[X||X<-List,X rem 2 /= 0].
tupleOddSum(Tuple)->
sum(filterEven(tuple_to_list(Tuple))).
24、传入任意I1、I2、D、Tuple四个参数,检查元组Tuple在索引 I1、I2位置的值V1、V2,如果V1等于V2则删除V1,把D插入V2前面,返回新元组,
如果V1不等于V2则把V2替换为V1,返回新元组
注意不能报异常,不能用try,不满足条件的,返回字符串提示
-export([deleteAndInsert/4]).
deleteAndInsert(P1,P2,D,Tuple) when is_integer(P1),is_integer(P2),is_tuple(Tuple)->
if
element(P1,Tuple)==element(P2,Tuple) ->
NewTuple=erlang:delete_element(P1,Tuple),
erlang:insert_element(P2-1,NewTuple,D);
true -> X=erlang:element(P2,Tuple),
NewTuple=erlang:delete_element(P1,Tuple),
erlang:insert_element(P2-1,NewTuple,X)
end;
deleteAndInsert(P1,P2,D,Tuple)->io:format(“illegal argument~n”).
25、删除列表或元组中全部符合指定键的元素
-export([deleteList/2,deleteTuple/2]).
deleteList(List,Key)->
deleteList2(List,Key).
deleteList2([H|T],Key) when H==Key->
deleteList(T,Key);
deleteList2([H|T],Key)->[H|deleteList(T,Key)];
deleteList2([],_)->[].
deleteTuple(Tuple,Key)->
list_to_tuple(deleteList(tuple_to_list(Tuple),Key)).
26、替换列表或元组中第一个符合指定键的元素。
-export([replaceElement/3]).
replaceElement([H|T],Key,NewElement) when H == Key->
[NewElement|T];
replaceElement([H|T],Key,NewElement)->[H|replaceElement(T,Key,NewElement)];
replaceElement([],Key,NewElement)->io:format(“There is no element can be matched”).
27、将指定的元素插入到列表中指定的位置,列表中后面的元素依次向后挪动
-export([insertElement/3]).
insertElement(List,Position,Element)->
tuple_to_list(erlang:insert_element(Position,list_to_tuple(List),Element)).
28、对[6,4,5,7,1,8,2,3,9]进行排序(降序–冒泡排序)或API排序
%%%%冒泡降序。
-export([m_sort/1]).
m_sort(List)->d_sort(List,length(List)).
d_sort(List,0)->List;
d_sort(List,N)
->d_sort(sort(List),N-1).
sort([Head,Next|Rest]) when Head =< Next
-> [Next|sort([Head|Rest])];
sort([Head,Next|Rest])
->[Head|sort([Next|Rest])];
sort([L])
->[L].
29、随机打乱元组,{z,1,y,3,x,5}
randomTuple(Tuple)->
list_to_tuple(shuffle(tuple_to_list(Tuple)));
shuffle(List)->
List_Temp1 = lists:map(fun(X)->
{random:uniform(length(List)),X}
end,List),
List_Temp2 = lists:sort(List_Temp1),
[Values || {,Values} <- List_Temp2];
shuffle()->
erlang:error("type mismatch ").
30、移除元组或列表中指定位置的元素
-export([deleteListByPosition/2,deleteTupleByPosition/2]).
deleteListByPosition(List,Position)->
tuple_to_list(erlang:delete_element(Position,list_to_tuple(List))).
deleteTupleByPosition(Tuple,Position)->erlang:delete_element(Position,Tuple).
31、指定列表第几位之后的数据进行反转。如:指定[2,3,5,6,7,2]第3位后进行反转(谢绝一切API(lists:concat,lists:reverse等)、++、–方法)
-export([reverseByPosition/2,reverse/1]).
reverse(List)->
reverse(List,[]).
reverse([Head|Rest],Reversed_List)->
reverse(Rest,[Head|Reversed_List]);
reverse([],Reversed_List)->Reversed_List.
reverseByPosition(List,Position)->
reverseByPosition(List,Position,[],1).
reverseByPosition([Head|Rest],Position,Reversed_List,Index) when Index==Position->
[Head|reverse(Rest)];
reverseByPosition([Head|Rest],Position,Reversed_List,Index)->
[Head|reverseByPosition(Rest,Position,Reversed_List,Index+1)].
32、使用“匿名函数”检查列表的每个元素是否符合传入的最大最小值(都是闭区间)
-export([belongMinToMax/3]).
belongMinToMax([H|R],Min,Max)->
Fun=fun(X,Min,Max)->
if X>=Min andalso X=
true -> false
end
end,
case Fun(H,Min,Max) of false
->io:format(“Isn’t ok~n”);
true->belongMinToMax(R,Min,Max)
end;
belongMinToMax([],Min,Max)->io:format(“Is ok~n”).
33、获得列表或元组的最大最小值
%%最大值
-export([list_max/1,tuple_max/1]).
list_max([Head|Rest]) ->
list_max(Rest, Head).
list_max([], Res) ->
Res;
list_max([Head|Rest], Result_so_far) when Head > Result_so_far ->
list_max(Rest, Head);
list_max([Head|Rest], Result_so_far) ->
list_max(Rest, Result_so_far).
tuple_max(Tuple)->list_max(tuple_to_list(Tuple)).
%%最小值
-export([list_min/1,tuple_min/1]).
list_min([Head|Rest]) ->
list_max(Rest, Head).
list_max([], Res) ->
Res;
list_max([Head|Rest], Result_so_far) when Head < Result_so_far ->
list_max(Rest, Head);
list_max([Head|Rest], Result_so_far) ->
list_max(Rest, Result_so_far).
tuple_min(Tuple)->list_min(tuple_to_list(Tuple)).
34、成生指定数量元组,每个元素为随机integer元素, 元素不能有相同的
-export([randomTuple/1]).
-export([isContainOfTuple/2]).
isContainOfTuple([],Element)->flase;
isContainOfTuple([H|R],Element)->
if
Element == H->true;
true -> isContainOfTuple(R,Element)
end.
randomTuple(Length)->randomTuple2({},Length).
randomTuple2(Tuple,Length) when Length>0->
X=random:uniform(Length+5),
Flag=isContainOfTuple(tuple_to_list(Tuple),X),
if Flag==true
->randomTuple2(Tuple,Length);
true->randomTuple2(erlang:append_element(Tuple,X),Length-1)
end;
randomTuple2(Tuple,Length)->Tuple.
35、对一个int进行位移操作,取出0到16位的数据,16到32位的数据,输出这两个数(提示:用bsr bsl实现)
-export([shiftOperation/1]).
shiftOperation(Number)->
Short1=(Number bsl 16) band 16#00000000,
Short2=(Number bsr 16) bor 16#0000ffff,
io:format(“The 0-16 is BnThe 16-32 is Bn”,[Short1,Short2]).
36、输入A(元子列表),B(数值列表)两个相同长度的参数进行随机匹配(要求字母为Key,数值为Value),支持指定的Key和Value在一组
-export([listMatch/2,randomNumber/1]).
randomNumber(Length)->
X=random:uniform(Length+1),
if
X=
true -> randomNumber(Length)
end.
listMatch(List1,List2)->
listMatch(List1,List2,1).
listMatch(List1,List2,Index) when Index =< length(List1)->
Position1=randomNumber(length(List1)),
Position2=randomNumber(length(List2)),
[{lists:nth(Position1,List1),lists:nth(Position2,List2)}|listMatch(List1,List2,Index+1)];
listMatch(List1,List2,Index)->[].
37、对相同类型的数据进行拼接,如binary(<<1,2>><<3,4>> =<<1,2,3,4>>) tuple({a,b},{c} ={a,b,c}) list([10],[20] =[10,20])
-export([spliceBinary/1,spliceTuple/1,spliceList/1]).
spliceBinary([H|R])->
list_to_binary(binary_to_list(H) ++ spliceBinary®);
spliceBinary([])->[].
spliceTuple(List)->
L=spliceTuple2(List),
list_to_tuple(L).
spliceTuple2([H|R])->
tuple_to_list(H)++ spliceTuple2®;
spliceTuple2([])->[].
spliceList([H|R])->
H++spliceList®;
spliceList([])->[].
38、将列表中相同key的值进行合并,[{a,1},{b,2},{c,3},{b,4},{b,5},{c,6},{d,7},{d,8}]
-export([mergeSameKey/1,isContainOfList/2]).
isContainOfList([],Element)->false;
isContainOfList([H|R],Element)->
if
Element == element(1,H)->true ;
true -> isContainOfList(R,Element)
end.
mergeSameKey(List)->mergeSameKey1(List,[]).
mergeSameKey1([H|R],Result_List)->
Key=element(1,H),
Tuple=list_to_tuple([Key]++proplists:append_values(Key,[H|R])),
case isContainOfList(Result_List,Key) of false
-> mergeSameKey1(R,[Tuple|Result_List]);
true -> mergeSameKey1(R,Result_List)
end;
mergeSameKey1([],Result_List)->Result_List.
39、Erlang程序设计(第2版)中5.3节的count_characters方法有错,自行修改证
count_characters(Str)->
count_characters(Str,#{}).
count_characters([H|T], X) ->
case maps:is_key(H,X) of
false -> count_characters(T, maps:put(H,1,X));
true -> Count = maps:get(H,X),
count_characters(T, maps:update(H,Count+1,X))
end;
count_characters([], X) ->
X.
40、使用“匿名函数”对参数列表中奇数乘以它所在奇数位。如{1,2,3,4,5} 结果 {1,2,6,4,15}
multiOddNumPos(List)->multiOddNumPos(List,1).
multiOddNumPos([],Index)->[];
multiOddNumPos([H|R],Index) ->
Fun=fun([H|R],Index)->
if
H rem 2 == 1 ->
[H * Index|multiOddNumPos(R,Index+1)];
true->[H|multiOddNumPos(R,Index)]
end
end,
L=Fun([H|R],Index).