Programming Erlang读书笔记5: Advanced SP

所有的BIFs都属于erlang module,如erlang:tuple_to_list()、erlang:time(),但是大部分BIFs都auto-imported了,所以可以直接调用tuple_to_list()、time()
erlang module的所有BIFs列表见:[url]http://www.erlang.org/doc/man/erlang.html[/url]

Binary是用来存储大量raw data的数据结构
[code]
1> <<5, 10, 20>>.
<<5,10,20>>
[/code]

操作Binary的BIFs:
[code]
@spec list_to_binary(loList) -> binary()
@spec split_binary(Bin, Pos) -> {Bin1, Bin2}
@spec term_to_binary(Term) -> Bin
@spec binary_to_term(Bin) -> Term
@spec size(Bin) -> Int
[/code]

Bit Syntax:
[code]
1> Red = 2.
2
2> Green = 61.
61
3> Blue = 20.
20
4> Mem = <<Red:5, Green:6, Blue:5>>.
<<23,180>>
5> <<R1:5, G1:6, B1:5>> = Mem.
<<23,180>>
6> R1.
2
7> G1.
61
8> B1.
20
[/code]

预定义Module属性
[code]
-module(modname).
-import(Mod, [Name1/Arity1, Name2/Arity2,...]).
-export([Name1/Arity1, Name2/Arity2,...]).
-compile(Options).
-vsn(Version).
[/code]

用户自定义Module属性
[code]
-SomeTag(Value).
[/code]
SomeTag必须为一个atom,Value必须为literal term
[code]
%% attrs.erl
-module(attrs).
-vsn(1234).
-author({joe,armstrong}).
-purpose("example of attributes").
-export([fac/1]).
fac(1) -> 1;
fac(N) -> N * fac(N-1).

1> attrs:module_info(attributes).
[{vsn,[1234]},{author,[{joe,armstrong}]},{purpose,"example of attributes"}]
[/code]

Boolean Expressions
[code]
1> not true.
false.
2> true and flase.
false.
3> true or false.
true
4> true xor false.
true
[/code]

字符集
Erlang源代码按ISO-8859-1(Latin-1)编码处理
Erlang内部没有字符数据类型,字符串事实上并不存在而是由整数列表来表示
Erlang对Unicode解析有限,因为整数列表有限

注释
Erlang里的代码注释以%开始,为单行注释

epp
在Erlang模块编译之前,Erlang的预处理器epp先处理它
epp会扩展源代码里的macros并插入必要的头文件
可以使用命令cmopile:file(M, ['P'])来编译M.erl并将结果输出到M.P文件里

Escape Sequence
[code]
\b Backspace 8
\d Delete 127
\e Escape 27
\f Form feed 12
\n New line 10
\r Carriage return 13
\s Space 32
\t Tab 9
\v Vertical tab 11
\NNN \NN \N Octal characters(N is 0..7)
\^a..\^z or \^A..\^Z Ctrl+A to Ctrl+Z 1 to 26
\' Single quote 39
\" Double quote 34
\\ Backslash 92
\C The ASCII code for C An integer
[/code]

方法引用
[code]
-moduel(x1).
-export([square/1, ...]).
square(X) -> X * X.
double(L) -> lists:map(fun square/1, L).

-module(x2).
double(L) -> lists:map(fun x1:square/1, L).
[/code]

Include Files
Erlang里被引入的文件的扩展名为.hrl
[code]
-include(Filename).
-include_lib("kernel/include/file.hrl").
[/code]

List加减
[code]
1> [1,2,3] ++ [4,5,6].
[1,2,3,4,5,6]
2> [a,b,c,1,d,e,1,x,y,1] -- [1,1].
[a,b,c,d,e,x,y,1]
[/code]

Macros
[code]
-define(Constant, Replacement).
-define(Func(Var1, Var2, .. , Var), Replacement).
[/code]
当遇到形如?MacroName的表达式时Macros会被epp扩展:
[code]
-define(macro1(X, Y), {a, X, Y}).

foo(A) ->
?macro1(A+10, b)
[/code]
上面的foo(A)会被扩展为:
[code]
foo(A) ->
{a, A+10, b}
[/code]
一些预定义的macros提供当前module的信息:
[code]
?FILE 当前文件名
?MODULE 当前module名
?LINE 当前行数
[/code]

Macros的Control Flow:
[code]
-define(Macro).
-undef(Macro).
-ifdef(Macro).
-ifndef(Macro).
-else.
-endif.
[/code]

Precess dictionary--Erlang里的HashMap
[code]
1> erase().
[]
2> put(x, 20).
undefined
3> get(x).
20
4> get().
[{x, 20}]
5> erase(x).
20
6> get(x).
undefined
[/code]

Short-Circuit Boolean Expression
[code]
Expr1 orelse Expr2
Expr1 andalso Expr2
[/code]

Term Comparisons
[code]
X > Y
X < Y
X =< Y
X >= Y
X == Y
X /= Y
X =:= Y
X =/= Y
[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值