Learning erlang: Day 1 – Sequential Programming
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.
11 Jul 2008 Arnold Daniels




This is actually illegal:
dosomething(N) ->
N + 1;
dosomething(N, N) ->
N + N + 1.
Two functions with the same name but different numbers of arguments are actually different functions.
What happens if a pattern match fails depends on the context. If it’s in a function clause or case-, if- or receive clause, the match operation will fall through to the next clause pattern. If there is no more pattern, a runtime exception will be raised.
In all other cases, a runtime exception is raised. This can be caught inline (with catch), or you can let the process die and optionally have this be noticed by another process (more on that in the concurrency course).
Also, strictly speaking, only unbound variables are untyped. Once bound, they become strongly typed. There is no way to subvert the erlang type system.
Thanks for correcting me. I’ll change the function.
All beginning is difficult I guess.
Hi!
Just interesting, why did you start learn Erlang?
As for me – this is the best thing I have ever seen, and I hope I’ll fine an erlang job…
PS Scheme – is my second favorite, and than goes Java.
Hi Andrew,
I can write PHP which is ideal for web stuff. When performance counts I can write PHP extensions or MySQL UDFs in C. But PHP isn’t working well for a daemon and writing a whole app in C is just to much work. This is especially true when you want want concurrency (and with a daemon you usually do). I’ve heard that Erlang is great for this, so I’m taking my first baby steps.
I think different languages serve different purposes, so I’m learning a set of languages to get a job done quickly and well. I’m already noticing that the fact that I (and Javeline as company) can develop in a big set of languages, gives us a huge advantage to those who only know lets say Java.
Hi Arnold,
Agree, but I am talking about personal preferences – I am getting more fun from Erlang, Scheme, Java (in this order) than I can get using PHP. Sure I will not write any complicated web stuff with Scheme (but it is also possible) but most likely I’ll use for this PHP.
ps Wating for your next erlang post