Newbie impressions: Elixir & Erlang

Published in Impressions

Welcome again my readers, to another issue of Newbie Impressions, a series where I look at various programming tools and relate my first impressions of them.

And today I'm not looking at a singular library, but rather at a programming language as a whole. Well, not as a whole, but as much as you can get after a couple weeks into the language.

First, a bit of a background. In 1986 (!), a Swedish company Ericsson created its own programming language for use in its telecom applications, called Erlang. It is a functional programming language with a heavy focus on distributed systems. Complete with its runtime system, the OTP, and its virtual machine, BEAM, Erlang enables designing distributed systems transparently and efficiently, achieving hot swapping of code, high availability, automatic restarting of faulty components and more.

Then, in 2011, José Valim of Plataformatec has created Elixir, a programming language that runs on the same BEAM virtual machine enjoying full access to Erlang's standard library, while also bringing its own things to the table, like macroses and better string handling.


A big thing about Erlang and Elixir, is that you discover things that are really awesome things when you're knee deep in the language. On the first glance, Erlang's (and Elixir's) sequential programming seems pretty unimpressive, especially if you're familiar with more "purely functional" languages like Haskell.

Like most functional languages, Erlang features pattern matching, first-order functions and immutable data. It doesn't feature any automatic currying though.

It's pretty easy to make a couple small modules, such as this (in Erlang):

-module(my_module).
-export([call/2]).

call(A, B) when A > 0 -> long_calc(A) + B;
call(A, B) -> B + long_calc(-2 * A).

long_calc(A) -> 3 * A + 2.

An equivalent module in Elixir would look like this:

defmodule MyModule do
  def call(a, b) when a > 0, do: long_calc(a) + b
  def call(a, b) do
    b + long_calc(-2 * a)
  end

  defp long_calc(a), do: 3 * a + 2
end

I won't go into much detail (they're pretty well covered by the documentation). The important thing is that Erlang (and Elixir) operate with modules. Modules are important. They contain function definitions, record definitions, macros (in Elixir) and, most importantly, they define behavior of actors.

Yes, that, in my opinion, is the interesting, "juicy" stuff that makes Erlang and Elixir so exciting for me. The way OTP enables developers to build those beautiful distributed auto-restarting easily-monitored applications is by implementing an actor model. By utilizing the built-in gen_server library, one can build a process that holds its state and handles synchronous and asynchronous messages from other processes. It reminds me a bit of Node.JS except much better designed (since messages are first-class language constructs) and each application contains multiple actors, so it's much more pleasant to work with.

Anyway, I'm gettig a bit rambly, so I think I'll finish here. Long story short, I do recommend every more or less experienced programmer to dig into Erlang or Elixir. If you're a web programmer, Elixir would probably feel more comfortable due to its superior string processing library.

You can look at Erlang's user guide here, and at Elixir's user guide here. In addition, I do recommend looking at the Learn you some Erlang for great good book since it's free to read online, even if it's a touch outdated.

Thank you very much for reading this article, dear reader! Did you like it? Did you dislike it? Is there anything you'd like me to cover? Please let me know in the comments below, and until next time!