Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top