erlang内置函数
123表示数字'123'表示原子"123"表示列表
whereis(RegName)->pid()|port()|undefinedReturnsthepidorportidentifierwiththeregisterednameRegName
unregister(RegName)->true
RemovestheregisterednameRegName,associatedwithapidoraportidentifier.
time()->Time{Hour,Minute,Second}.
now()->Timestamp{MegaSecs,Secs,MicroSecs}
localtime()->DateTime{{Year,Month,Day},{Hour,Minute,Second}}
throw(Any)->no_return()
catchthrow({hello,there}).//{hello,there}
Failure:nocatchifnotevaluatedwithinacatch.
Bin=term_to_binary(Tuple).//<<131,104,3,100,0,1,97,100,0,1,98,100,0,1,99>>
binary_to_term(Bin).//{a,b,c}
statistics(wall_clock).//统计运行时间
erlang:start_timer(Time,Dest,Msg)->TimerRef
Startsatimerwhichwillsendthemessage{timeout,..}toDestafterTimemilliseconds.
erlang:send_after(Time,Dest,Msg)->TimerRef
StartsatimerwhichwillsendthemessageMsgtoDestafterTimemilliseconds.
split_binary(Bin,Pos)->{binary(),binary()}
list_to_binary("0123456789").
{B1,B2}=split_binary(B,3).->B1=<<"012">>.
插入新元素到元组中
insert_element(2,{one,two,three},new).->{one,new,two,three}
append_element({one,two},three).->{one,two,three}
element(1,{a,b,[c,d]})->a获取元组的第1个元素
tuple_size({a,b,{c,d}}).->3
is_tuple({})->trueis_tuple(a)->false
is_atom(Term)
is_binary(Term)
is_bitstring(Term)
is_function(Term)->boolean()
is_list(Term)->boolean()
is_integer(Term)->boolean()等is_xxx函数
float(55).->55.0
round(5.5)->6
tuple_to_list({a,b,[c,d]}).->[a,b,[c,d]]
list_to_tuple([a,b,{d,e}]).->{a,b,{d,e}}
list_to_integer("123").->123
list_to_integer("3FF",16).->1023
integer_to_list(13).->"13"注意不能用13.0badargument异常
integer_to_binary(77).-><<"77">>
binary_to_integer(<<"123">>).->123
binary_to_integer(<<"3FF">>,16).->1023
list_to_float("1.34").->1.34注意使用"1"作为参数badargment异常
float_to_list(1.34)->"1.34000000000000007994e+00"
float_to_binary(7.12,[{decimals,4}]).-><<"7.1200">>
float_to_binary(7.12,[{decimals,4},compact]).-><<"7.12">>
atom_to_list(abc).->"abc"
list_to_atom([1,2,3])->'\001\002\003'但[a,b,c]则badargument异常
list_to_atom([$1,$2,$3])->'123'
list_to_binary([1,2,3])-><<1,2,3>>但[a,b,c]则badargument异常
list_to_binary([$1,$2,$3])-><<"123">>.<<1,2,3>>不等同于<<"123">>
list_to_bitstring([1,2,3])-><<1,2,3>>但[a,b,c]则badargument异常
list_to_bitstring([$1,$2,$3])-><<"123">>.
正确的用法list_to_atom([$a,$b,$c]).
binary_to_term(Binary)->term()
hd([a,b,c])->a
tl([a,b,c]).->[b,c].返回tail部分,空列表badargument异常
length([a,c,b])->3
spawn(Fun)->pid()
spawn(Node,Fun)->pid()
spawn(Module,Function,Args)->pid()
spawn(Node,Module,Function,Args)->pid()
spawn_link(Module,Function,Args)->pid()
spawn_link(Node,Module,Function,Args)->pid()
exit(Reason)->no_return()
StopstheexecutionofthecallingprocesswiththeexitreasonReason
catchexit(foobar).->{'EXIT',foobar}
exit(Pid,Reason)->true
SendsanexitsignalwithexitreasonReasontotheprocessorportidentifiedbyPid.
link(PidOrPort)->true
Createsalinkbetweenthecallingprocessandanotherprocess(orport)
unlink(Id)->true
Removesthelink,ifthereisone,betweenthecallingprocessandtheprocessorportreferredtobyId.
register(RegName,PidOrPort)->true
registered()->[RegName]
processes()->[pid()]Returnsalistofprocessidentifierscorrespondingtoalltheprocessescurrentlyexistingonthelocalnode.
purge_module(Module)->true
RemovesoldcodeforModule.BeforethisBIFisused,erlang:check_process_code/2shouldbecalledtocheckthatnoprocessesareexecutingoldcodeinthemodule.
erlang:loaded()->[Module]
ReturnsalistofallloadedErlangmodules(currentand/oroldcode)
erlang:send(Dest,Msg)->Msg等同于Dest!Msg
self()->pid()
nodes()Returnsalistofallvisiblenodesinthesystem,excludingthelocalnode
node()->NodeReturnsthenameofthelocalnode.
进程字典相关函数:
put(name,sky).
put(sex,1).
get()->[{Key,Val}]
get(name)->sky
erase(name).
erase().