Getting started with Erlang

“How long does it take for someone with very little programming experience to become an Erlang expert?” This is one of the most frequently asked questions when it comes to Erlang (and all other programming languages for that matter), but it’s one that doesn’t really have a single answer; it depends on each individual person.

Expecting to develop a million dollar Facebook Messenger of your own after a month of studying Erlang is simply not going to happen. One month is plenty of time to pick up the basics, but as they say, ‘one should not learn to achieve success but to attain knowledge – the success will follow soon after’. Luckily, we’ve written this blog to help you get started.

Let’s begin with how a simple ‘Hello World’ program looks in Erlang:

-module(hello).
-export([hello_world/0]).

hello_world() -> io:fwrite("hello, world\n").

We know what you’re thinking; ‘this doesn’t look like a traditional programming language. Something must be wrong’. True, it’s not like an imperative programming language such as C, C++ or Java. However, it’s not a million miles away from declarative languages such as Lisp, Prolog, and Haskell – if you have any experience with the latter group, you’ll find Erlang much more understandable. If not, you may need a week or two to get to grips with it (but it’s worth it).

Key Points to note:

1. An Erlang file must be saved with .erl extension.
2. The name of the file should be the same as the name of the module.
3. The use of ‘Period’ [.] at the end of each line is critical to the functioning of the code.

How should I run this code?

The first step of running any Erlang code is compiling it. This can be achieved using the Erlang shell. Once inside the Erlang shell, all we need to do is type c(*module_name*). When this is done, the file having that module will be compiled successfully provided it doesn’t have any syntactical errors.

Eg: To compile the file that has the “Hello World” statement, we need to type the following command on the Erlang shell:

c(hello).

{ok, hello} is printed on the shell once the compilation is complete which indicates that the code compiled without any error. Upon successful compilation, to run the code module we must type the following line into Erlang shell.

hello:hello_world().

Expected Output: hello, world

Key Points to note:

1. To access the Erlang shell, we must type the following on the command line:
● Linux : erl
● Windows: werl

2. We can quit the Erlang shell by typing init:stop(). CNTRL+C and CNTRL+G can be used for accessing menus.

(Visited 430 times, 1 visits today)