Today I started with learning erlang. I’ll keep a log on my blog with the progress. On the first I’ve read the first 2 chapters of http://www.erlang.org/course. This language doesn’t look like anything else I’ve ever programmed. So don’t expect so much of a Erlang for PHP programmers kind of tutorial, since the whole thought is different. The first chapter was history which I pretty much skipped. The second chapter is ‘Sequential Programming’. Since all I’ve ever done is sequential programming it should be fairly understandable.

Values and variables
The type of values is slightly different than I’m use to. You’ve got:

Numbers
Which are integers or floats. They can be written in several notations (which is quite nice).
Atoms
This can be a string, but also a function or module name. They should start with a lower case character and only contain word chars or quoted with single quotes.
Tuples – {123, def}
A fixed length array. The items in a tuple can be anything.
Lists – [123, xyz, abc]
A dynamic length array like a php array. The items in a list can also be anything.
Variables
A variable looks almost like an atom, but start with an upper case char. Variables are untyped, they can be set to any value

Calling functions
In C++ and Java there is a nice feature that you can have 2 of the same functions with the same name, as long a the parameters are different. Erlang has taken that to the next level. It will try to match the function based on the passed arguments and execute that implementation (or body).

dosomething(N) ->
  N + 1;
dosomething({N, M}) ->
  N + M + 1.

As you’ve might noticed. The value of the last statement is automatically returned.

On the contrary of any programming language I’ve ever seen, erlang doesn’t only take variables as arguments, but values as well. It uses the values in finding the matching function body.

factorial(0) -> 1;
factorial(N) -> N * factorial(N-1).
 
area({square, Side}) ->
  Side * Side;
area({circle, Radius}) ->
  3.14 * Radius * Radius;
area({triangle, A, B, C}) ->
  S = (A + B + C)/2,
  math:sqrt(S*(S-A)*(S-B)*(S-C));
area(Other) ->
  {invalid_object, Other}.

Variables are loosely typed. There are things called guards to check if a variable has the correct type. For the factorial(N) function you would do factorial(N) when integer(N). You can also use a guard to specify a condition of the value as factorial(N) when N > 0.

Setting variables
The whole pattern matching idea is also taken to setting variables. Though it doesn’t appear that you can use the -> syntax. I’m unsure what happens on failure

A = 10
  % Succeeds - binds A to 10
 
[A,B,C] = [1,2,3] 
  % Succeeds - binds A to 1, B to 2, C to 3
 
[A,B,C,D] = [1,2,3]
  % Fails

There is a nice syntax to get elements from the beginning of a list or tuple.

[A,B|C] = [1,2,3,4,5,6,7]
  %Succeeds - binds A = 1, B = 2, C = [3,4,5,6,7]

This would translate in PHP to:

$C = array(1,2,3,4,5,6,7);
$A = array_shift($C);
$B = array_shift($C);

Next
The next chapter is ‘Concurrent Programming’. I’ve not worked with threads in a long time, so this might be a bit harder. However I got a 8.5/10 for concurrent programming in college, so maybe I’ll be fine.