erlang精要(14)-列表(1)
生活随笔
收集整理的這篇文章主要介紹了
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 of1->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 of1->true;0->falseend.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) oftrue->NewSum=Sum+Fst,NewOddLst=[Fst|OddLst];false->NewSum=Sum,NewOddLst=OddLstend,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> 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的erlang精要(14)-列表(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring.factories 的妙用
- 下一篇: Java中List的contains方法