erlang精要(14)-列表(1)
作者:互联网
1> X=[1,2,3].
[1,2,3]
2> Y=[4,5,6].
[4,5,6]
5> [X|Y].
[[1,2,3],4,5,6]
7> [Fst|Rest]=Y.
[4,5,6]
8> Fst.
4
9> Rest.
[5,6]
10> [One,Two,Three]=X.
[1,2,3]
11> One.
1
12> Three.
3
13>
奇数和偶数的判断
116> learnerl:loop([1,2,3]).
1 是奇数
2 是偶数
3 是奇数
byebye.
ok
117>
-module(learnerl).
-export([loop/1]).
is_odd(Num)->
Rn=Num rem 2,
case Rn of
1->io:format("~p 是奇数~n",[Num]);
0->io:format("~p 是偶数~n",[Num])
end.
loop([Fst|Rst])->
is_odd(Fst),
loop(Rst);
loop([]) ->io:format("byebye.~n").
所有奇数的和
-module(learnerl).
-export([total/3]).
is_odd(Num)->
Rn=Num rem 2,
case Rn of
1->true;
0->false
end.
output_lst([Fst|Rst])->
io:format("~p,",[Fst]),
output_lst(Rst);
output_lst([]) ->io:format("~n").
total([Fst|Rst],OddLst,Sum)->
case is_odd(Fst) of
true->NewSum=Sum+Fst,NewOddLst=[Fst|OddLst];
false->NewSum=Sum,NewOddLst=OddLst
end,
total(Rst,NewOddLst,NewSum);
total([],OddLst,Sum) ->
output_lst(OddLst),
OddLst,io:format("Sum=~p byebye.~n",[Sum]).
111> c(learnerl).
{ok,learnerl}
112> learnerl:total([1,2,3],[],0).
3,1,
Sum=4 byebye.
ok
113>
标签:14,format,Sum,Fst,io,Rst,精要,erlang,learnerl 来源: https://blog.csdn.net/AI_LX/article/details/113801242