Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

When do I use a semicolon (;), comma (,), or period inside my functions or case statements?

like image 935
samoz Avatar asked Jul 10 '09 16:07

samoz


People also ask

When to use semicolon in erlang?

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. You also use the semicolon at the end of a function clause which doesn't end the function (you end a function by using a dot, as marcc explained).

When to use commas vs semicolons?

Use semicolons to replace commas when individual items in a series are long or contain commas. Use a semicolon instead of a comma to separate the items.

What is the use of a semicolon?

A semicolon is most commonly used to link (in a single sentence) two independent clauses that are closely related in thought. When a semicolon is used to join two or more ideas (parts) in a sentence, those ideas are then given equal position or rank.

Which symbol is used to declare unbound values in Erlang?

In Erlang, all the variables are bound with the '=' statement. All variables need to start with the upper case character. In other programming languages, the '=' sign is used for the assignment, but not in the case of Erlang.


5 Answers

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.

like image 130
cthulahoops Avatar answered Sep 23 '22 21:09

cthulahoops


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
like image 43
marcc Avatar answered Sep 24 '22 21:09

marcc


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.
like image 20
Jamie Forrest Avatar answered Sep 24 '22 21:09

Jamie Forrest


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
like image 40
Ben Hughes Avatar answered Sep 21 '22 21:09

Ben Hughes


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

like image 26
rvirding Avatar answered Sep 24 '22 21:09

rvirding