A super quick hands-on introduction to Erlang

Madeleine Malmsten
4 min readSep 19, 2019

One of my top 3 favourite programming languages is Erlang — a functional programming language with tons of excellent features when it comes to concurrency, distribution, hot-swapping, supervision, an outstanding “actor model” functionality... The list of cool features is nearly endless.

Photo by Jakub Gorajek on Unsplash

In this blog post, I’ll describe as short and concise as I can about how to download Erlang, and how to create your first Erlang module. Since I’m stripping out more or less all theoretical parts, I’ll also add a few useful links here and there.

The “Get started” section

Download Erlang

Let’s download all the necessary things we need to run Erlang! Open the terminal and enter following (depending on which package manager you use):

For Homebrew on OS X: brew install erlang
For MacPorts on OS X: port install erlang
For Ubuntu and Debian: apt-get install erlang
For Fedora: yum install erlang
For FreeBSD: pkg install erlang

For installation on RaspberryPi (or if above doesn’t work for some reason), follow this guide: https://elinux.org/Erlang

Confirm your installation by typing…

erl

… in the terminal (and then exit Erlang by typing ctrl + c).

Done!

Photo by Matt ODell on Unsplash

Create your first Erlang application

In the terminal, create a folder in which you want to create your project. In this folder, create a new file called todo.erl. Add the following lines to create the module.

-module(todo).-export([start/0]).
-export([init/0]).
start() ->
Pid = spawn(?MODULE, init, []),
register(todo, Pid).

The exported functions are public. Later on, we’ll call the function I named start, for starting up the application. The spawn function means creating a new process and in this process, run the function init. We also register the process and name it “todo”, which will make the message sending easier later on. The register function is native.

The process name must be of the type atom, that will be seen here and there. It’s neither a string or a boolean, but something in between.

If a process dies, the processes created from the now-dead process will also die, so keep your “parent” process alive (https://learnyousomeerlang.com/errors-and-processes).

Let’s continue with the init function that should be added below the first lines. The init function creates an “ets” table, before running loop. The “ets” is a really cool and fast internal database that supports concurrent reading and writing (https://learnyousomeerlang.com/ets). I just love the fact of having the data and the code at the same place.

Side note: When the program is stopped, all ets tables are gone too. So if you, later on, need to save your data, you can save the ets tables to dets files (http://erlang.org/doc/man/dets.html).

init() -> 
ets:new(todo, [ordered_set, named_table, public]),
loop().

Time to add loop; a recursive function that will run forever. The receive works as a “mailbox” and gets all messages sent to the process registered as “todo” (https://erlangbyexample.org/send-receive).

Messages that arrive in the mailbox are then pattern matched. First, to see if the message is a so-called “tuple” including the atom create, and a Message that could be anything.

If the pattern doesn’t match, the next pattern will be tried, and then the next until a working pattern is found. In this case, the last pattern, the variable X, will match anything received in the mailbox.

loop() ->
receive
{create, Message} ->
Id = os:system_time(),
ets:insert(todo, {Id, Message}),
io:format("Added ~p ~p~n", [Id, Message]),
loop();
{delete, Id} ->
ets:delete(todo, Id),
io:format("Deleted ~p~n", [Id]),
loop();
list ->
io:format("~p~n", [ets:tab2list(todo)]),
loop();
X ->
io:format("Something else received ~p~n",[X]),
loop()
end.

And then we’re done writing the first skeleton! All of the code in todo.erl can be found here:

Start Erlang, compile the file, and start the application:

erlc(todo).todo:start().

You can send the “todo” process a message and by doing that adding a task to the todo list — typing the following command:

todo ! {create, "Take the cat for a walk"}.

You can also list all task by the following command

todo ! list.

I added a delete command as well

todo ! {delete, id123}.

More info about message passing is to be found here: https://www.erlang.org/course/concurrent-programming

To wrap it up…

Please feel free to use, misuse and abuse the code in my Gist as much as you want. If you’d like to dive more into depth or read the documentation, I strongly recommend a visit to https://learnyousomeerlang.com/ and https://www.erlang.org/.

Have fun and happy coding! :)

--

--

Madeleine Malmsten

Developer / Staff Engineer with a soft spot for functional and logic programming.