Erlang学习三

来源:互联网 发布:重庆网络口碑营销 编辑:程序博客网 时间:2024/05/16 14:33


http://www.erlang.org/doc/getting_started/intro.html#id60368



变量:

每个变量在它的作用域内只能给定一个值

<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;">1> M = 5.52> M = 6.** exception error: no match of right hand side value 63> {X,Y} = {paris,{f,27}}.{paris,{f,27}}4> X.paris5> Y.{f,27}6> </span></span></span>

revert:

<span style="font-size:18px;"><span style="font-size:18px;">-module(tut).-export([revert/1]).revert([]) ->ok;revert([Head | Rest]) ->revert(Rest,Head).revert([],TemLs) ->TemLs;revert([Head | Rest],TemLs) ->Tem = [Head | TemLs],revert(Rest,Tem).25> tut:revert([1,2,3]).[3,2|1]26> </span></span>


把Head当做元素,导致Tem[2|1] 

<span style="font-size:18px;"><span style="font-size:18px;">-module(tut).-export([revert/1]).revert([]) ->ok;revert(List) ->revert(List,[]).revert([],TemLs) ->TemLs;revert([Head | Rest],TemLs) ->Tem = [Head | TemLs],revert(Rest,Tem).27> c(tut).{ok,tut}28> tut:revert([1,2,3]).[3,2,1]29> </span></span>


list中[ ] 和[Head | Rest]区别

<span style="font-size:18px;"><span style="font-size:18px;">-module(tut).-export([revert/1]).revert([Head | Rest]) ->1.4> c(tut).tut.erl:8: Warning: variable 'Head' is unusedtut.erl:8: Warning: variable 'Rest' is unused{ok,tut}5> tut:revert([]).** exception error: no function clause matching tut:revert([]) (tut.erl, line 8)6></span></span>


查找最大最小值:

<span style="font-size:18px;">-module(tut).-export([findmaxandmin/1]).findmaxandmin([Head | Rest]) ->findmaxandmin(Rest,Head,Head).findmaxandmin([Head | Rest],Max,Min) ->if Head > Max ->TemMax = Head;true ->TemMax = Maxend,if Head < Min ->TemMin = Head;true ->TemMin = Minend,findmaxandmin(Rest,TemMax,TemMin);findmaxandmin([],Max,Min) ->{Max,Min}.3> c(tut).{ok,tut}4> tut:findmaxandmin([1,2,3,2,1]).{3,1}5> </span>

if and case:

end之前没有分号

<span style="font-size:18px;">-module(tut).-export([test/2]).test(A,B) ->ifA == 1 ->io:format("A == 1~n");B == 2 ->io:format("B == 2~n");A == 2,B == 3 ->io:format("A == 2,B == 3~n");A == 4;B == 5 ->io:format("A == 4;B == 5~n")end.24> c(tut).{ok,tut}25> tut:test(2,2).B == 2ok26> tut:test(2,4).** exception error: no true branch found when evaluating an if expression     in function  tut:test/2 (tut.erl, line 5)27> tut:test(2,3).A == 2,B == 3ok28> tut:test(4,5).A == 4;B == 5ok29> </span>


<span style="font-size:18px;">-module(tut).-export([convert/1]).convert(Val) ->case Val of{cent,X} ->{inch,X / 2.54};{inch,Y} ->{cent,Y * 2.54}end.31> c(tut).{ok,tut}32> tut:convert(1).** exception error: no case clause matching 1     in function  tut:convert/1 (tut.erl, line 6)33> tut:convert({cent,2}).{inch,0.7874015748031495}34> tut:convert({inch,2}).{cent,5.08}35> </span>

built in functions (BIFs)

因某些原因把函数构造在Erlang虚拟机中,例如为了实现在Erlang中不可能的实现或不高效的功能。

BIFs可以直接用其函数名,但是,他们属于erlang模块,例如调用trunc(向下取整)等价于erlang:trunc.


<span style="font-size:18px;"></span><pre name="code" class="plain">34> 2004 rem 400.435> 
36> trunc(5.5).537> round(5.5).638> length([a,b,2,3]).439> float(5).5.040> is_atom(H).* 1: variable 'H' is unbound41> is_atom(h).true42> is_atom("h").false43> is_atom({1,2}).false44> is_tuple({1,2}).true45> is_tuple([1,2]).false46> atom_to_list(hello)."hello"47> list_to_atom("hello47> ").'hello\n'48> list_to_atom("hello").hello49> integer_to_list(22)."22"50>



-module(tut).-export([monthdays/2]).monthdays(Year,Mon) ->Leap = iftrunc(Year / 400) * 400 == Year ->leap;trunc(Year / 100) * 100 == Year ->not_leap;trunc(Year / 4) * 4 == Year ->leap;true ->not_leapend,case Mon ofsep -> 30;apr -> 30;feb when Leap == leap -> 29;feb -> 28end.2> c(tut).{ok,tut}3> tut:monthdays(2004,feb).294> 


higher order functions(Funcs)

-module(tut).-export([foreach/2,map/2]).foreach(Fun,[First | Rest]) ->Fun(First),foreach(Fun,Rest);foreach(Fun,[]) ->ok.map(Fun,[First | Rest]) ->T= Fun(First),Tem = [T | map(Fun,Rest)],Tem;map(Fun,[]) ->[].4> Xf = fun(X) -> X * 2end.#Fun<erl_eval.6.90072148>5> Xf(3).66> c(tut).14> tut:map(Xf,[1,2,3]).[2,4,6]17> Fe = fun(X) -> io:format("~w",[X * 2])end.#Fun<erl_eval.6.90072148>18> tut:foreach(Fe,[1,2,3]).246ok21>


-module(tut).-export([convertlist/1]).convert({Name,{f,Temp}}) ->{Name,{c,trunc((Temp - 32)*5 / 9)}};convert({Name,{c,Temp}}) ->{Name,{c,Temp}}.convertlist(List) ->Newls = lists:map(fun convert/1,List),lists:sort(fun({_,{c,Temp1}},{_,{c,Temp2}}) ->Temp1 < Temp2 end,Newls).21> c(tut).{ok,tut}22> tut:convertlist([{moscow,{c,-10}},{bj,{f,2}}]).[{bj,{c,-16}},{moscow,{c,-10}}]23> 








0 0
原创粉丝点击