Tiny Notes About Erlang Syntax

Literal Term
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.

List Comprehension
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.

Macros With Same Name
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)]))).

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

will occur a syntax error.


你可能感兴趣的:(erlang,F#)