sam*_*moz 47 erlang punctuation
我一直在努力学习Erlang,并且遇到了函数和case语句中的结尾行的一些问题.
也就是说,我什么时候在我的函数或case语句中使用分号,逗号或句点?
我已经有了工作的东西,但我真的不明白为什么,并且正在寻找更多的信息.
cth*_*ops 50
我喜欢读分号为OR,逗号为AND,完全停止为END.所以
foo(X) when X > 0; X < 7 ->
Y = X * 2,
case Y of
12 -> bar;
_ -> ook
end;
foo(0) -> zero.
Run Code Online (Sandbox Code Playgroud)
读为
foo(X) when X > 0 *OR* X < 7 ->
Y = X * 2 *AND*
case Y of
12 -> bar *OR*
_ -> ok
end *OR*
foo(0) -> zero *END*
Run Code Online (Sandbox Code Playgroud)
这应该说清楚为什么没有; 在案件的最后一个条款之后.
mar*_*rcc 48
逗号在一行正常代码的末尾.
案例结束时的分号或if语句等.最后一个案例或if语句最后没有任何内容.功能结束时的一段时间.
抱歉,对于随机变量名称,显然这没有做任何事情,但说明了一点:
case Something of
ok ->
R = 1, %% comma, end of a line inside a case
T = 2; %% semi colon, end of a case, but not the end of the last
error ->
P = 1, %% comma, end of a line inside a case
M = 2 %% nothing, end of the last case
end. %% period, assuming this is the end of the function, comma if not the end of the function
Run Code Online (Sandbox Code Playgroud)
Jam*_*est 24
在模块中,句点用于终止模块属性和函数声明(也称为"表单").您可以记住这一点,因为表单不是表达式(没有从它们返回值),因此句点表示语句的结尾.
请记住,具有不同arities的函数的定义被视为单独的语句,因此每个函数都将以句点终止.
例如,函数定义hello/0和hello/1:
hello() -> hello_world.
hello(Greeting) -> Greeting.
Run Code Online (Sandbox Code Playgroud)
(注意,在erlang shell中,句点用于终止和计算表达式,但这是一个异常.)
分号充当子句分隔符,用于函数子句和表达式分支.
例1,函数子句:
factorial(0) -> 1;
factorial(N) -> N * fac(N-1).
Run Code Online (Sandbox Code Playgroud)
例2,表达分支:
if X < 0 -> negative;
X > 0 -> positive;
X == 0 -> zero
end
Run Code Online (Sandbox Code Playgroud)
逗号是表达式分隔符.如果逗号跟在表达式后面,则表示在子句后面有另一个表达式.
hello(Greeting, Name) ->
FullGreeting = Greeting ++ ", " ++ Name,
FullGreeting.
Run Code Online (Sandbox Code Playgroud)