Fun with Erlang, ACCU 2008

2008-04-01, , , Comments

I’ve just got back from a one day course on Erlang given by its inventor, Joe Armstrong, at day 0 of the ACCU 2008 conference. Actually, he crammed as much as he could from a three day course into a single day. I’m not too disappointed we didn’t reach the stated aim of the course, of developing and running a networked application and changing it on the fly: I’m happy enough to have written some Erlang code and been exposed to some new ideas.

Programming Erlang

Armstrong is affable and enthusiastic and not afraid to voice his opinions. He’s a good teacher. I do recommend his book, “Programming Erlang: Software for a Concurrent World”, but found the tone a bit matey in places. In person he’s much more direct and engaging.

Erlang is a functional programming language which builds in support for multiple processes — these are not operating system processes; and in some ways, Erlang is the operating system. You define functions and other rules and controls using pattern matching. When patterns are used to dispatch message responses in a receive statement, the code reads well, and functions can be defined using patterns in an elegant and concise way — no if-this-then-that-else-other.

-module(accum).
-export([evens_and_odds/1]).
-import(lists, [reverse/1]).

evens_and_odds(L) -> evens_and_odds(L,[], []).

evens_and_odds([H|T], E, O) when H rem 2 =:= 0 -> evens_and_odds(T, [H|E], O);
evens_and_odds([H|T], E, O) ->  evens_and_odds(T, E, [H|O]);
evens_and_odds([], E, O)    ->  {E, O}.

(I know this example should be coded using lists:partition, it’s just here to show the pattern syntax.)

Joe Armstrong and the big thumper

Erlang is no academic pure functional language, though. It originated at Ericsson over 20 years ago and has been used to develop extremely reliable distributed concurrent systems. Hence the current interest: Erlang can take advantage of multiple processor cores on multiple machines, which is why it’s been adopted by up and coming projects like CouchDB. Given its proprietary origins, I think we’re lucky to find the language available under an open source license. (Armstrong has some stories to tell about that!) On the other hand, Armstrong admitted that some of the documentation was weak — at Ericsson you could always ask one of the Erlang developers if you didn’t understand something. Personally, I’d be wary of the OTP platform, a full-featured distributed application framework built on top of Erlang.

Processes communicate by messages and by generating exceptions. You can link processes together. This doesn’t mean designing a distributed system is easy, but I’d say it gives us an appropriate language for such systems. Or as Armstrong puts it:

“You cannot describe concurrent systems in sequential languages.”


By the way, I’ll be back at the conference on Thursday, to hear what Simon Peyton Jones has to say about functional programming and Haskell. See some of you then.