57

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?

2240
  • 1,547
  • 2
  • 12
  • 30
samoz
  • 56,849
  • 55
  • 141
  • 195

5 Answers5

60

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.

cthulahoops
  • 3,805
  • 20
  • 17
  • 2
    Im just starting out with Erlang and this was extremely useful. Thank you. – Sid Aug 27 '12 at 16:33
  • 2
    Good example, but when would `X > 0; X < 7` ever be false? – Jamie Forrest Dec 28 '12 at 19:06
  • Very helpful method, very natural. – liuyanghejerry Jul 02 '13 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 '15 at 01: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
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
marcc
  • 12,295
  • 7
  • 49
  • 59
  • 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). – Adam Lindberg Jul 31 '09 at 07: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. – Jeremy Wall Aug 10 '09 at 01: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.
Jamie Forrest
  • 10,895
  • 6
  • 51
  • 68
  • 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 '16 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
Ben Hughes
  • 14,075
  • 1
  • 41
  • 34
  • 3
    Love the Vonnegut quote! I'll make sure to accent my usage of semi-colons; they really should be used more. – samoz Jul 10 '09 at 16:47
  • +1. Erlang punctuation was clearly inspired by prose punctuation, [to Erlang's detriment](http://www.reddit.com/r/programming/comments/ck63l/a_bit_of_heresy_functional_languages_are_overrated/c0t795d). – Warren Young Jan 02 '13 at 15:28
3

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

rvirding
  • 20,848
  • 2
  • 37
  • 56
  • I'd guess you'd endorse [LFE](https://github.com/rvirding/lfe#readme) as a fix for this except that [it hasn't been touched in a year](https://github.com/rvirding/lfe/tree/master/src). [Reia](http://reia-lang.org/) is [defunct](https://github.com/tarcieri/reia#readme). [Efene](http://www.efenelang.org/) hasn't been touched in 7 months, and the "[1.0 last call](http://www.efenelang.org/2011/10/last-call-for-efene-10-talk-now.html)" was answered by crickets 15 months ago. Of the compiles-to-BEAM languages, only [Elixir](http://elixir-lang.org/) seems to still be in development. – Warren Young Jan 02 '13 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 03 '13 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%](http://www.tiobe.com/index.php/content/paperinfo/tpci/), 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. – Warren Young Jan 03 '13 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 04 '13 at 02:49
  • We can agree on this much: C++FE is a project that should never exist. :) – Warren Young Jan 04 '13 at 02:53
  • That we can most definitely agree on. I happen to like Lisp syntax, it's simple and extremely flexible, hence LFE. And I seriously don't see alternate Erlang syntaxes, even LFE, being really viable alternatives to the standard syntax; mainly because of lack of support in the standard distribution. – rvirding Jan 04 '13 at 03:05