Tim Disney

Learning Erlang

In my never ending quest to know more than is strictly necessary about this best of all possible worlds, I have recently taken to learning a new programming language: Erlang!

Why learn a new language? Because different languages let you solve different problems. Because new languages expand your mind. Because new languages make you more marketable. Mostly though because it is fun! (my neurosis revealed at last!)

I’ve been picking up Erlang with this book written by one of the creators of Erlang. I usually don’t go for dead trees when learning new languages (online docs and tutorials have usually sufficed) but this one was well worth it. The author has a bunch of extended apps which do a great job of giving a feel for the flow of Erlang.

Why Erlang? Because Erlang has been specifically designed to ease development of concurrent software. This is good because the one striking trend from the processor peeps is that the only thing guaranteed to increase in CPUs are the number of cores. Gone are the days of ever increasing clock speeds. It doesn’t matter how long Intel expands it’s pipeline, the clocks aren’t going anywhere in a hurry. And so, we must start dealing with concurrent programming.

Erlang deals with the concurrency problem firstly by being a functional programming language and has no side effects. This means that the biggest issue traditional languages face (dealing with shared memory) simply does not exist in Erlang. Secondly, Erlang has the concept of processes. Processes are like threads except extremely lightweight and managed by the Erlang system instead of the OS. This means that you can spawn thousands of processes extremely quickly. To give you an idea of how quickly, on my laptop I spawned 32,000 processes and the average time it took to spawn each was 2 microseconds.

Here is a grab bag of interesting Erlang features. In an effort to lock in what I’m learning by writing (both code and “real” words) these may wind up being expanded into full on posts.
<ul><li>Functional language
</li><li>First class functions
</li><li>No side effects (mostly)
</li><li>Data Structures: Atoms, Tuples, Lists
</li><li>Lightweight processes (as opposed to say threads in Java)
</li><li>Simple communication between processes (Pid ! {Message})</li><li>Pattern matching
</li><li>List comprehensions [2*X
  X <- List]
</li><li>Mnesisa DBMS uses list comprehensions (select * from foo becomes [row
  row <- foo])</li><li>Hot swapping code</li><li>Fault tolerance
</li></ul>