In Erlang, when do I use ; or , or .?

拈花ヽ惹草 提交于 2020-04-29 05:18:25

问题


I've been trying to learn Erlang and have been running into some problems with ending lines in functions and case statements.

Namely, when do I use a semicolon, comma, or period inside my functions or case statements?

I've gotten stuff to work, but I don't really understand why and was looking for a little more information.


回答1:


Comma at the end of a line of normal code.
Semicolon at the end of case statement, or if statement, etc. The last case or if statement doesn't have anything at the end. A period at the end of a function.

example (sorry for the random variable names, clearly this doesn't do anything, but illustrates a point):

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



回答2:


I like to read semicolon as OR, comma as AND, full stop as END. So

foo(X) when X > 0; X < 7 ->
    Y = X * 2,
    case Y of
        12 -> bar;
        _  -> ook
    end;
foo(0) -> zero.

reads as

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*

This should make it clear why there is no ; after the last clause of a case.




回答3:


Period (.)

In modules, the period is used to terminate module attributes and function declarations (a.k.a. 'forms'). You can remember this because forms aren't expressions (no value is returned from them), and therefore the period represents the end of a statement.

Keep in mind that definitions of functions with different arities are considered separate statements, so each would be terminated by a period.

For example, the function definitions for hello/0 and hello/1:

hello() -> hello_world.

hello(Greeting) -> Greeting.

(Note that in the erlang shell the period is used to terminate and evaluate expressions, but that is an anomaly.)

Semicolon (;)

The semicolon acts as a clause separator, both for function clauses and expression branches.

Example 1, function clauses:

factorial(0) -> 1;
factorial(N) -> N * fac(N-1).

Example 2, expression branches:

if X < 0  -> negative;
   X > 0  -> positive;
   X == 0 -> zero
end

Comma (,)

The comma is an expression separator. If a comma follows an expression, it means there's another expression after it in the clause.

hello(Greeting, Name) -> 
    FullGreeting = Greeting ++ ", " ++ Name,
    FullGreeting.



回答4:


You can think of it like english punctuation. Commas are used to separate things in a series, semicolons are used to separate two very closely related independent clauses[1] (e.g. the different cases of the case statement, function clauses of the same name and arity that match different patterns), and periods are used to end a sentence (complete thought).

  1. Or to prove you went to college. "Do not use semicolons. They are transvestite hermaphrodites representing absolutely nothing. All they do is show you've been to college." -- Kurt Vonnegut



回答5:


The comma separates expressions, or arguments, or elements of a list/tuple or binary. It is overworked.



来源:https://stackoverflow.com/questions/1110601/in-erlang-when-do-i-use-or-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!