Tiny Notes About Erlang Syntax

[b]Literal Term[/b]
int, float, atom, tuple, list, binary
in predefined attributes and user-defined attributes, the Value must be literal term. for example:
some_module.erl

...
-attr1(1).
-attr2(1.0).
-attr3(correct).
-attr4({1, 0}).
-attr5("good").
-attr6(<<"good">>).
...

all the attributes is normal.

[b]List Comprehension[/b]
for example:

L = [{cn, "Beijing"}, {cn, "Shanghai"}, {us, "New York"}, {jp, "Tokyo"}].
a) CnCity = [Ci || {cn, Ci} <- L] or
b) CnCity = [Ci || {Country, Ci} <-L, Country =:= cn]

a) we can use pattern match in generator to get some Values, this is simple.
b) we can also use filter to get the expected values.

[b]Macros With Same Name[/b]
In Erlang, if two macros with the same name, then we will get "redefing macro .." error info when compilation. so we must give the different names for all the macro.
for example:

-define(Log(S), (io:format("log:~s~n", [S]))).
-define(Log(F, D), (io:format("log:~s~n", [io_lib:format(F, D)]))).

change to

-define(Log(S), (io:format("log:~s~n", [S]))).
-define(Log2(F, D), (io:format("log:~s~n", [io_lib:format(F, D)]))).

[b]Macros Can't use in function internal[/b]
e.g.
some_fun() ->
-ifdef(debug).
io:format("some text~n"),
-endif.
....

will occur a syntax error.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值