Erlang小结

时间:2012-9-28     作者:smarteng     分类: 编程


1、数据类型
Numbers
Invariable Variables
Atoms
Boolean Algebra & Comparison operators
Tuples {4,5}
Lists [1, 2, 3, {numbers,[4,5,6]}, 5.34, atom]
List Comprehensions
Bit Syntax
Binary Comprehensions
//尾递归
 tail recursive
len([]) -> 0;
len([_|T]) -> 1 + len(T).
becomes:
tail_len(L) -> tail_len(L,0).
tail_len([], Acc) -> Acc;
tail_len([_|T], Acc) -> tail_len(T,Acc+1).

duplicate(0,_) ->[];
duplicate(N,Term) when N > 0 ->[Term|duplicate(N-1,Term)].
//尾递归
tail_duplicate(N,Term) ->tail_duplicate(N,Term,[]).
 tail_duplicate(0,_,List) ->List;
tail_duplicate(N,Term,List) when N > 0 ->
tail_duplicate(N-1, Term, [Term|List]).
//case 语句模板
case Expression of 
Pattern1 [when Guard1] -> Expr_seq1;
Pattern2 [when Guard2] -> Expr_seq2;
……
end.
//if 语句
if
Guard1 ->
Expr_seq1;
Guard2 ->
Expr_seq2;
end.
//try 语句模板
try FuncOrExpressionSequence of
Patten1 [when Guard1] -> Expressions1;
Patten2 [when Guard2] ->Expressions2;
catch
ExceptionType: Expattern1 [when ExGuard1] -> ExExpressions1;
ExceptionType: ExPattern2 [when ExGuard2] -> ExExpressions2;
after 
AfterExpressions 
end.
//宏定义,包括宏的内部流程控制 
-ifdef(debug).
-define(...).
-else.
-define(...).
-endif
//全局唯一引用
erlang:make_ref().
//表达式
x /= y x不等于y
x =:= y x全等于y
x =/= x不全等于y
Erlang按照 number< atom <reference < fun <port <pid <tuple <list < binary 大小比较的顺序。
数值比较时,除了 =:= 和 =/= 之外,整数先转换成 浮点数。