What is the difference between Elixir and Erlang

Daniel Koch10

Despite its tender age of just three years, Elixir is already worth a look because of its roots. Especially since language has a number of properties that could prove beneficial for the networked future.

A recent McKinsey study confirms that the Internet of Things has a bright future. The authors of the study assume an economic gain of eleven trillion euros. For example, cities and municipalities can benefit from the fact that garbage cans are only emptied when they are actually full. But road traffic should also be able to be controlled more efficiently with automatic traffic jam avoidance.

The ideas for this brave new world are almost unlimited - however, particularly effective programming languages ​​are required to implement them. One of them is currently preparing to massively advance the development of the Internet of Things: Elixir, a comparatively new language that relies on Erlang.

Until a few years ago, the Internet was very simple. Typically, devices would send signals to a server, which would return a response when needed. The communication mostly ran via HTTP. However, that is no longer enough for the requirements of the Internet of Things. Because now very different devices should be able to communicate with each other independently. Modern houses are often already equipped with numerous sensors for light, temperature, kitchen appliances, TV and access controls that need to be networked with one another.

With IPv6, the devices can also be equipped with their own IP address in the future. How many there will ultimately be can only be estimated at the moment. Gartner assumes up to 26 billion, IDC even 212 billion IP-enabled devices worldwide.

Erlang as the origin

When it comes to Erlang, it is usually not just the language itself that is meant. Rather, the term almost always includes the library and the runtime environment. Strictly speaking, this package is Erlang / OTP, where OTP stands for The Open Telecom Platform. For the sake of simplicity, the term Erlang is mostly used, as is the case in this article.

Erlang has been around for more than 25 years. That is surprising, after all, the language is still largely unknown. However, this is more due to the traditionally weak position of functional languages ​​than to certain weaknesses of Erlang. Despite the comparatively low level of awareness, Erlang can be found in many applications such as Facebook, GitHub, T-Mobile and WhatsApp.

The programming language was originally developed for use in telecommunications. The requirements that are placed on a language in this industry can be transferred almost unchanged to the Internet of Things. Here, as here, the decisive factors are high availability and parallelism. Since Erlang code can be changed at runtime, applications written with it do not have to be stopped when making adjustments. As a result, the applications are comparatively easy to access: According to official information, the availability of Erlang systems is assumed to be around 99 percent per year. It should be noted, however, that while this may sound good the first time you listen, three to four days of downtime per year can be a lot depending on the application.

A core element of Erlang are the so-called processes, which can best be compared with threads, but are less extensive. The language enables several thousand processes to be started, which can then be distributed across CPUs.

Hello Erlang!

As is customary in the developer community, a hello world example should also be used in this article to get a first impression of Erlang. But be careful: The syntax for this very simple example, or the resulting result, is comparatively complex and cumbersome. This is not because of the complexity of Erlangen, but because the language specializes in complex applications.

Here is the assumed content of a welt.erl:

-modules (hello). % This is the module name.
-export ([hello_world / 0]). % The function is declared.

hello_world () -> io: format ("Hello, world! ~ n").
% Specifies what should happen after the function is called.

The code describes a very simple Erlang module. It only outputs the defined welcome text. You can then use the Erlang shell via ~ $ ext is called, compile the module and execute the function.

Eshell V5.5.5 (abort with ^ G)
1> c (hello).
{ok, hello}
2> hello: hello_world ().
Hello World!

With c (hello). users start the compilation process, hello: hello_world (). calls the function hello from the compiled module Hello World() on.

Of course, the example only shows a simple application, which should nevertheless convey how Erlang behaves fundamentally. Detailed information on the language and current developments is available on the project website. Incidentally, there is not only detailed documentation there, but also numerous practical examples.


And then came Elixir

Functional languages ​​are numerous and have existed for a long time; JavaScript, Python, Ruby and XSLT are mentioned for classification. All of them have advantages and disadvantages. Erlang also belongs to the group of functional languages ​​and its syntax is reminiscent of the prologue, which many developers do not like. This is where Elixir comes in: The language uses the advantages of Erlang, but comes with a much better and more elegant syntax.

The Elixir development was initiated in 2013 by José Valim, who incidentally is also a member of the Ruby on Rails Core Team. The main reason for the desire for a new language was the expandability of the Erlang VM.

First, a quick look at the special features of Elixir. Above all, the possibilities for recursion and the so-called higher order functions are important. As can be seen in the following code snippet, they take other functions as arguments:

defmodule content do
def foursome (value, function) do
4 * function. (Value)

Elixir also supports the concept of reflection or metaprogramming. So language has the ability to analyze itself. There is an introductory example on GitHub.

Streams can also be used in Elixir: They are used to read data into an application or to output them from an application. The underlying concept is comparable to that from Java.

Pattern matching is also offered, with which data can be searched for a string.

Before use

Erlang 17.0 (or higher) is required to use Elixir. The language supports Linux, Mac OS X and Windows systems. The Web Installer can be used for installation under Windows. The familiar installation variants are available on Linux systems. Here's the way for Ubuntu and Debian:

wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb
&& sudo dpkg -i erlang-solutions_1.0_all.deb
sudo apt-get update
sudo apt-get install elixir

After successful installation, the interactive shell iex or, under Windows, iex.bat can be started. Anyone who works with Ruby will quickly feel at home, after all, iex is very reminiscent of irb. The tool offers extensive documentation. If you want to look at the description of the Enum module, for example h (enum) to enter.

The BREAK menu can be opened with Ctrl + C, via which the user can exit the shell and take a look at running processes. The key combination Ctrl + G is also important. With the menu that can be opened, you can, among other things, provide an additional shell.

User switch command
-> s 'Elixir.IEx'
-> c

The shell is personalized via h IEx.configure / 1. The configuration can be adapted for the current project or globally.

Develop together

A small but active community has established itself around Elixir, which makes working with language more comfortable with its own tools and applications. Particularly noteworthy is the package manager hex.pm, thanks to which Erlang and Elixir code can be installed directly as packages. The EEx template engine, which enables templates to be compiled into code, is also of interest. By the way, the proximity of Elixir and Ruby becomes clear again with her, as EEx is based on the well-known Ruby ERB:

<%= @content %>

As the code snippet shows, EEx works with the usual placeholder syntax. In order not to have to forego the convenience of other languages ​​when troubleshooting, there is a test framework with ExUnit. The bet follows the following pattern:


defmodule TestCase do
use ExUnit.Case

test "the truth" do
assert true

Finally, a build tool is also available for Elixir: mix is ​​the native tool of choice when it comes to compiling projects.

In order to keep up to date with the latest developments in the Elixir environment, there is now a mailing list, a Twitter account and regular events that the community informs about via GitHub.

Syntax, control structures, maps

Special features of the syntax

From the beginning, the goal of Elixir inventor José Valim was ambitious, after all he wanted to use everything positive about Erlang without foregoing the advantages of other functional languages. At the same time, the known disadvantages should not be included in the syntax. So it is hardly surprising that Elixir is syntactically largely based on Ruby. An interesting analysis can be found in a blog entry under the title "Closures: Elixir vs. Ruby vs. JavaScript".

The certain relationship to Ruby can be seen, for example, in the fact that threads can also be defined in Elixir. Also based on Ruby is the ability to generate code using so-called metaprogramming. By the way, both aspects are not only found in Ruby, but could also be taken from Java. But not only they were Elixir's godfather: For example, PHP adopted the concept of being able to write code that - executed as a separate process - generates a response to the current process. There are also references to NodeJS. For example, it is possible to work with messages asynchronously over sockets.

Although there is the greatest overlap with Erlang, there are still some syntax stumbling blocks lurking when switching to Elixir. It starts with the operators: those available in Erlang other- and or- There are no operators at all in Elixir. In addition, the developers andalso and orelse by other and or replaced.

There are also differences with regard to the variable names. Variables can only be assigned once in Erlang. In the Erlang shell there is with f a special command with which variable bindings can be canceled. In contrast, Elixir allows multiple assignments of a variable.

iex> a = 1
iex> a = 2
iex> ^ a = 3

** (MatchError) no match of right hand side value: 3

It is also worth taking a look at the different types of function calls. The Erlang variant of a summation:

sum ()

looks like this in Elixir:


When using function parameters there are between Erlang:

sum (a, b)

and Elixir:

sum a, b

also differences. The syntax is admittedly unfamiliar to Erlang.

The official Elixir documentation provides a complete overview of the syntax differences.

Under control

Control structures are of course available in Elixir. It refers to if, case and cound. Numerous expressions are also supported, including, for example

  • logical operators like ==, !=, > and so on,
  • the boolean operators other, or and not
  • as well as the arithmetic operators +, -, *, /


In addition, there are a number of type test functions such as is_atom / 1, is_binary / 1, is_bitstring / 1, is_boolean / 1, is_float / 1, is_function / 1 and is_integer / 1:

iex> is_boolean (true)
iex> is_boolean (1)

But also functions like abs (number), binary_part (binary, start, length), bit_size (bitstring), byte_size (bitstring), div (integer, integer) and elem (tuple, n) can be found in the language:

iex> << 0, 1, 2, 3 >>
<<0, 1, 2, 3>>
iex> byte_size << 0, 1, 2, 3 >>

The if-Construct is always interesting to check a condition:

if lang ==: PHP
IO.puts "I am a PHP professional"

In order to be able to react to different conditions, you can also else-Use blocks:

if lang ==: PHP
IO.puts "I am a PHP professional"
IO.puts "I like another language" end

Another way to control conditions is that cond do:

cond do
language ==: php and not is_javascript ->
IO.puts "I am a PHP expert"
language ==: php and is_javascript ->
IO.puts "I am a PHP and JavaScript expert"
language ==: SASS ->
IO.puts "I can only SASS"
true ->
IO.puts "Maybe I can do anything"

Maps and language combinations

The so-called maps are an interesting language feature. They represent nothing more than data structures in which keys point to values ​​and to which pattern matching can be applied. But be careful, here Elixir explicitly takes a different path than Erlang. Maps are used there as a replacement for records, whereas in Elixir records are tuples that are supported by modules.

Incidentally, Erlang and Elixir can also be combined with one another. For example, you could write functions in Erlang and then load them into Elixir. (How sensible this is in the end remains to be seen.) Blake Williams has taken this option and presented it on his blog using a few examples.

Phoenix, bottom line

A look into the future

The Phoenix project shows how powerful Elixir already is. The web framework was written entirely on the basis of Elixir and enables, among other things, the development of APIs and HTML5 apps. Phoenix was primarily developed to be able to write high-availability and high-performance web applications. A functioning Erlang environment on which Elixir runs is a prerequisite for its use. The Phoenix archive can be installed there.

$ mix archive.install https://github.com/phoenixframework/phoenix

By default, PostgreSQL is used as the database server, but MySQL can also be used. This is the flag --database mysql to be transferred when creating the desired application.

If required, Node.js can also be used in Phoenix. However, at least version 0.12.0 of the server-side JavaScript platform is required for this.

Phoenix is ​​still at the very beginning of its development, as the current version number 0.16 shows. Nevertheless, more than just a few good approaches can already be seen. The development of the framework remains exciting. His website provides detailed information on the project.


Elixir, it seems at least for now, will be one of the decisive factors when it comes to developing applications for the Internet of Things. Above all, it is the high availability that should be decisive here.

Elixir uses the strengths of Erlang and cleverly expands them. At just three years of age, it is still comparatively young for a programming language; the current version is 1.0.5. You should still keep an eye on the coming developments, as they should give the language a further boost. (jul)

Daniel Koch
works as a freelance developer and writer.