Understanding punctuation in erlang

Akshat Jiwan Sharma, Wed Nov 13 2013

One thing that troubled me while I was learning erlang was the punctuation syntax of the language. While most languages have a single terminator between statements erlang has 3. Punctuation can seem a bit daunting to the new comers so with this post I will try to make it as simple as possible.

Erlang has 3 punctuation symbols

  1. comma (,) -> This is read as and. As in the execution continues to the statement following the comma.
  2. semicolon (;) -> This is read as or. A semicolon marks the end of execution in a clause.
  3. Full stop (.)-> The end. Marks the end of execution of a code block.

Time for an example (courtesy Joe Armstrong)

-module(geometry).
-export([area/1]).

area({rectangle, Width, Ht}) -> erlang:display("Illustrating comma"),
                                 Width * Ht;
area({square, X}) -> X * X;
area({circle, R}) -> 3.14159 * R * R.

Let us for an instant forget about what is happening in this program and just focus on the punctuation symbols.

  1. The module and export statements are followed by a (.) indicating their end.
  2. The first area({rectangle,Width,Ht}) function clause has two statements in it. erlang:display() terminated by a comma and Width*height terminated by a semicolon. The comma termination indicates that "this is not the end of the block. There are more statements to follow so execute them". When a ; is encountered it tells the compiler that the clause has ended and move on to the next one. Any expression after ; will be illegal.
  3. area({square, X}) Again ; tells the compiler to move on to the next clause in case the current clause is not matched.
  4. area({circle, R}) Here a . is encountered which tells the compiler that the block has finished. There are no more clauses.

Expressions like case, if and fun() etc which are terminated by an end follow the same rules of punctuation except that the statement immediately before the end should not have any terminator. The end statement can be followed by a ; indicating the end of the clause or a . or with a , . Time for some more examples

add_pid(Pid,Name)->
Member = pg2:get_members(Name),
    case Member of
    [Pid]-> ok;
    {error,_} ->pg2:create(Name),
            pg2:join(Name,Pid)
    end.

Again don't try to think about what the code does and just focus on the punctuation. This is a basic case statement. There are two clauses in the case.

  1. First [Pid] clause is evaluated. Like a function clause it is terminated with a ; . Nothing special here.
  2. The second pattern {error,_} does something different. Besides providing a pattern it tells the compiler that this is the last clause of the case which is why there is no terminator after the last statement. It ends with an end and .

Notice that immediately before the end statement there is no punctuation mark. Here is a more complex example

deliver_message(Channels,Msg) ->
    lists:foreach( 
      fun(N)->
          Member = pg2:get_members(N),
          case Member of
          [Pid] -> Pid ! {jiffy:encode({[{status,200},{msg,Msg}]})};
          {error,_}->self()!{jiffy:encode({[{status,404}]})}
          end
      end,Channels).

So let us break this one down now.

  1. lists:foreach takes an anonymous function. An anonymous function terminates with an end.
  2. To complicate matters the anonymous functions also has a case statement which also terminates with an end.
  3. The principle is still the same. The last statement of the final pattern ends without a punctuation.
  4. The program does nothing after evaluating the case and hence the end of the anonymous function.
  5. There can be no punctuation on the statement immediately before end and hence the end of the case statement has nothing after it.

    To recap. There are three punctuation symbols in erlang. A , is read as an and (indicating continuity of execution). A ; is read as an or (indicating end of a clause). A . which marks the end. The end statement has the same rules with the exception that any statement immediately preceding it can not have any punctuation in it.

Hopefully this article will make erlag syntax a bit easier for you. I am still new to erlang so if any regular reading this post finds any error in my examples or explanation please let me know in the comments.


comments powered by Disqus