adrift on a cosmic ocean

Writings on various topics (mostly technical) from Oliver Hookins and Angela Collins. We have lived in Berlin since 2009, have two kids, and have far too little time to really justify having a blog.

Seven Languages - Erlang

Posted by Oliver on the 27th of September, 2013 in category Tech
Tagged with: erlangprogrammingsevenlanguages

Well, well, well. One virtual "week" later, but two real world months later, I have finished the chapter on Erlang from Seven Languages in Seven Weeks. Based on the Git repository where I am recording my coding efforts through the book, I've been working at it for almost 11 months, which is slightly disappointing, but I have noticed during the chapter on Erlang a subtle shift in how my brain is functioning. Perhaps it is Erlang, perhaps it is the book as a whole, but I do notice that I am considering programming languages in a more balanced, perhaps philosophical way.

After five chapters it is starting to become evident that the languages were presented in a very well-crafted order. Ruby is undeniably the easiest and most familiar to the hordes of programmers out there largely experienced in imperative or even vaguely C-like syntax languages. As a side-note, since we are hosting several RailsGirls teams in the building, we recently started a "book club" going through this book chapter by chapter with these new software developers and some of my coworkers. It is proving very stimulating discussing languages, especially when we have different opinions about them and different experiences. I'm looking forward to hearing others share their delight in features I've hated, and vice versa.

So far we have only just finished Ruby and started on Io (which I covered what feels like an age ago). From there it gets progressively more diverse, more challenging by the chapter but each time you reach a new language your thinking has been altered in such a way that the challenges are within reach. This is perhaps a bit abstract so I'll get back to talking about Erlang, and perhaps my point will become clear.

I find it terribly ironic that I almost bitterly finished the chapter on Prolog, thankful I'd never see that syntax or computing model again, and found it staring my in the face with Erlang. Erlang originally grew out of Prolog so a lot of the syntax is very similar, but somehow after conquering the chapter on Prolog previously, the problems framed around learning Erlang didn't seem as hard.

That being said, familiarity with the basic syntax only got me so far. The concurrency model, OTP services and supervisors/monitors are a lot to wrap your head around and I found this chapter utterly mind-bending, but in a good way. I gave myself plenty of time (about two months in total) but made sure I finished every exercise, and I believe it was well worth the effort, as I feel I have a much better understanding of how it all works now. It's not a language I would reach for instinctively, but at least I feel I could navigate my way around it (and we do in fact have some services written in Erlang at SoundCloud). It does intrigue me to know that robust services such as CouchDB and the latest Chef Server have been written successfully in this language.

What specifically did I find interesting about the language?

  • The use of Prolog-inspired pattern-matching, guard statements and the catch-all underscore all make for some very interesting ways of expressing data-driven decision-making or branching.
  • Function overloading by using different pattern matchers in the input parameters.
  • The actor- and message-based concurrency model.
  • The various ways of linking and monitoring processes to ensure they robustly resist entire system failure.
  • Atoms. I'm not sure what I make of them exactly - they feel a bit like symbols in Ruby, but evidently have a much wider namespace as you can register a process with an atom as its name, then refer to it in different places to where you registered it. But they are definitely convenient.
  • The fact that you can send messages asynchronously or synchronously, either by expecting a response only as a return message to another process, or by waiting for return value.
  • The slightly different syntax to io:format.
  • How you have to do something that feels like force-quitting the Erlang VM to exit (Ctrl-C then q).

Of course these are all observations based on very beginner-usage of Erlang. It has its differences which I find both challenging and interesting, not necessarily flaws. If you're playing along at home, feel free to check out the code I managed to create while working through this chapter, and of course I'm always interested to read your opinions on the subject so feel free to leave a comment. Onward and upward to Clojure!

© 2010-2018 Oliver Hookins and Angela Collins