First steps with Erlang

6 min. read

Caution! This article is 7 years old. It may be obsolete or show old techniques. It may also still be relevant, and you may find it useful! So it has been marked as deprecated, just in case.

Everyone is excited about Elixir, the programming language built on top of Erlang that shares the same abstractions as Erlang but is inspired by Ruby and it's developer friendliness.

I've been thinking for a while about giving it a try. However, since it's most important features are related with concurrency and that's the reason Erlang was created to begin with, I thought it would be better to start with Erlang.

Here I put together some of my notes about the language, although to really get it into my system I have to find a fun project to build with it. Preferably a project that needs concurrency!

Installing

To install it:

sudo apt install erlang
sudo apt install erlang-base

It comes with a linter. If you are using atom, there is a language-erlang plugin for syntax highlighting and a erlang-linter too.

Erlang also comes with a shell prompt that you start like this:


erl
Erlang (BEAM) emulator version 4.9.1.2

Eshell V4.9.1.2 (abort with ^G)
1>

It says to press Ctrl + G to exit, but the only way I could achieve it is by hitting Ctrl + C and then a for "abort".

Just like in a SQL shells you often have to finish everything with a ; end all sentences with a dot, and if you have several lines in a function, each finishes with a comma and the last one with a dot, as if it was a sentence in English. It does throw an error, just silently returns the prompt.

Environment

Erlang code is compiled to bytecode so it can be used by the VM:


erlc -v filename.erl

this will generate filename.beam. Now you can run it inside the erl bash:


erl
1> filename:function(argument)

The Erlang package manager is hex, for which you need rebar3, which is the official build tool for Erlang.

I couldn't find any official Erlang version managers, but this Github repository created in 2012 seems to still be maintained. Not sure if this is what Erlang people use though.

For debugging, a native debugger is shipped with the language, as well as a trace facility called dbg.

Apparently it looks like it also comes with a testing framework, EUnit.

Finally, the gitignore site has an Erlang specific gitignore file to use in your projects.

The language

There is a learn X in Y minutes dedicated page for Erlang.

BIFs are functions that are built into Erlang to do tasks that are impossible to program in Erlang.

In Erlang functions should always return something, if you are not, then Erlang will return the atom ok.

Also, the last logical expression of a function to be executed will be returned to the caller automatically, like in Ruby and other imperative languages. Comments are marked with a % symbol, like in LaTeX. Function notation is -> like in JavaScript ES6. Functions are called with parentheses even if they take no arguments, just like python there is no end.

It's a best practice to not use import for external modules and rather make the dependency explicit by using io:format(foo), for example.

Module name and file name have to be the same, extension is erl.

There are function declarations made up of function clauses, which are separated by ; rather than using if-else.


greet(male, Name) ->
  io:format("Hello, Mr. ~s!", [Name]);
greet(female, Name) ->
  io:format("Hello, Miss. ~s!", [Name]);
greet(_, Name) ->
  io:format("Hello, ~s!", [Name]).

Comments