56

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?

5 Answers 5

59

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.

4
  • 2
    Im just starting out with Erlang and this was extremely useful. Thank you.
    – Sid
    Aug 27, 2012 at 16:33
  • 2
    Good example, but when would X > 0; X < 7 ever be false? Dec 28, 2012 at 19:06
  • Very helpful method, very natural. Jul 2, 2013 at 10:54
  • 1
    I also only start erlang, but could I suggest such a comment: there is no ; after the last clause of a case because semicolon is not after clauses -- it is between. Semicolon and coma state how the clauses are connected. The period marks the end of the clause (so, it is indeed at the end of stuff). It seems this logic comes from Prolog origin of Erlang, however I can't say, I only read "learnyousomeerlang." Cheers!
    – xealits
    Apr 12, 2015 at 1:44
56

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
  • 4
    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). Jul 31, 2009 at 7:58
  • you end a named function with a dot. Anonymous functions are terminated with an end keyword. Also you use dots after -include() and -compiler directives. It's best to think of the period as a final terminator. This is the "END" where as the comma and semicolon 's are seperators. As chthulahoops below says. the comma works as a seperator of a series, and the semicolon works a seperator for alternatives. , = and & ; = or is a pretty decent way to remember. Aug 10, 2009 at 1:55
29

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.
1
  • yeah one thing that bit me when learning erlang was putting the ; at the end of the last clause, e.g., after your X == 0 -> zero in Example 2.
    – Tommy
    Jan 21, 2016 at 18:13
3

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
2
  • 2
    Love the Vonnegut quote! I'll make sure to accent my usage of semi-colons; they really should be used more.
    – samoz
    Jul 10, 2009 at 16:47
  • +1. Erlang punctuation was clearly inspired by prose punctuation, to Erlang's detriment. Jan 2, 2013 at 15:28
3

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

6
  • I'd guess you'd endorse LFE as a fix for this except that it hasn't been touched in a year. Reia is defunct. Efene hasn't been touched in 7 months, and the "1.0 last call" was answered by crickets 15 months ago. Of the compiles-to-BEAM languages, only Elixir seems to still be in development. Jan 2, 2013 at 15:23
  • @WarrenYoung Seeing I was one of the people behind the Erlang syntax I don't really have any problems with it. :-) LFE is not dead, it has just reached the stage where it needs some serious users to get input for improvements and development. An alternative lisp syntax for Erlang is Joxa. I personally don't see the charm with a ruby-like syntax.
    – rvirding
    Jan 3, 2013 at 11:20
  • Yes, I know who you are. ("Hello, Robert!") I was just thinking if you had a chance to do it all over again, you might not base your new language's syntax on Prolog's this time. As for LFE, Lisp has ~1% mindshare and Erlang ~1/3%, so LFE's max mindshare potential is ~0.0033%. Its likely potential is further cut down by the fraction of Erlang system users who are willing to use a secondary front end to begin with. Dislike Ruby if you will, but it and languages like it are more popular at the moment. Jan 3, 2013 at 18:41
  • @WarrenYoung I don't know, perhaps. Or pick a syntax more like other functional languages. I wouldn't base it on a C/Java like base as I think the semantic "fit" is way too bad. And I have done a lot of C programming so I am very used to that style of syntax and have no problems with either. I don't see learning a new syntax as a problem, it is the least problematic part of learning a new language. It's just a RTFM.
    – rvirding
    Jan 4, 2013 at 2:49
  • We can agree on this much: C++FE is a project that should never exist. :) Jan 4, 2013 at 2:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.