Everything you need to know to get started with Erlang

Erlang is a programming language built for distributed systems from the ground up. Scalable, robust that's Erlang for you.

Published Date
05 - Jan - 2016
| Last Updated
06 - Jan - 2016
Everything you need to know to get started with Erlang

Erlang is one of those early languages which were based on the functional programming paradigm which was developed back in somewhere 1986 internally within Ericsson for proprietary use. The immediate target for it, being a platform for distributed and fault tolerant telecommunications applications. This was one of the few places when Ericsson was seen in the software technology scene, and this is one of the reasons why this language was open sourced in 1998 after it was banned for further product development internally.

The most important factor being that Ericsson intended to focus on developing hardware products, rather than being a producer of the software stack that powers them. But this turned out to be for the better in one way, specially considering the fact that the people who were responsible for developing and writing such a language i.e. Joe Armstrong, Robert Virding and Mike Williams, promptly quit the organization due to this reason and worked towards its wider adoption once it was open sourced. However, they were rehired later and the ban was lifted as well. As of today, it has a thriving community which actively develops it for numerous production applications. 
The name for this language is ostensibly inspired from the unit with the same name, which was used to measure the load on telecommunication switches and other devices. Originally, its usage was obviously restricted mostly to telecoms systems, but today it works under the hood for many large scale distributed systems. 

Learn erlang tutorial
Joe Armstrong, the co-founder of Erlang

While, it may not be as ubiquitous in the wider technology sector  as are other languages like C or Java, its relevance grows day by day as our systems grow more and more complex and distributed at the same time. It’s also getting more and more dominant because of the increasing focus and availability of multicore processors, which makes concurrency and multiprocessing even more important. Erlang’s usage has been more than dominant in developing modern software which are used in systems like chatting, commenting, managing transactional data and networking devices. 
The success of Erlang in modern distributed systems has been largely untold, despite the fact that companies like Facebook, Whatsapp, Amazon, Rackspace etc., along with popular open source software like RabbitMQ and CouchDB actively use it as part of the underlying stack in their software. It’s also used as part of the Disco MapReduce framework and the YAWS web server. 

Learn erlang tutorial
Riak is a NoSQL database that uses Erlag

How, What and Why Erlang?

A conventional programmer would look at Erlang as a language which tackles concurrency, resilience and flow of data in a very unique manner and this may seem unusual or difficult to some of you. But some of these features along with excellent learning material makes the language very attractive once you are in the loop with all of these aspects. OTP or Open Telecom Platform forms the most important part of this language. It is a set of libraries and a full-fledged framework that provides communication protocols, static analysis tool, interpreter, compiler and a distributed database called Mnesia. The language can be compiled and run on top of a virtual machine just like Java, or it can also be used in an interactive environment like Python. The compiler, known as beam, converts the source code into a byte code, which is then converted to threaded code at the time of loading. Some of the core principles behind how the language works can be summed up with the help of below points: 

Concurrency and Inter-Process Communication
Concurrency and distributed computing is one of the primary facets on which the language has been built from the ground up. A major factor that helps in concurrency is the fact that all data structures in Erlang are immutable and cannot be changed, this avoids having to focus on locks, semaphores and other ways of trying to ensure the sanctity of shared data. Processes are like the primary building block that form a part of an Erlang program, and each process communicates with another using an asynchronous message passing protocol. No data is shared between any two processes and this is irrespective of the fact whether these processes are running on the same node or are part of a larger distributed cluster.  This also makes it more secure and predictable, because data is shared only when there is clear and explicit communication between the two processes. 

Pattern Matching
Pattern matching is another feature that is inherited from the concept of functional programming. This practice promotes declarative syntax and makes working with complex data structures much easier. List Comprehensions for example, which are inspired from Lisp and other such languages also form an important part of Erlang. 

Erlang has been made to ensure fault tolerance and robustness of a system. The messaging system in Erlang has been implemented in such a way that even if a process fails or stops executing, it can communicate the reason before it is killed, so that the remaining processes can bypass or take appropriate action. These error handling mechanisms allow the developers to create the program in such a way that processes are allowed to fail and this makes the code cleaner and shorter because most of these things are handled by the in built libraries themselves, rather than the programmer having to code them explicitly.

Hot swapping or code loading
Keeping the concept of distributed computing in mind, Erlang ensures that any part of its code or program can be replaced or updated without any problems or hiccups. This makes scalability an important feature of the language and you can take advantage of multicore processors, add new nodes or change things transparently without any impact on the rest of the system. This is made possible because of the fact that a process can contain a reference to an old as well as a newer version of the same module and can cleanly make the switch at a specific time with the help of an external call that is made to the module.

The Next Step

Given that there isn’t as big of a community around Erlang, as is for other languages, there are limited outlets where you are able to look at Erlang code and learn it from scratch. But some of these resources including the official documentation  can help you get an overview. You can give it a test run here in the form of an online interpreter where you can try and run some of the basic commands. However, one of the best and easily the most popular Erlang book in our opinion is available online here written by Fred Hebert. It includes an excellent introduction and takes you step by step with the help of some funny cartoons. Some other good books include Programming Erlang by Joe Armstrong and O’Reilly’s Erlang Programming by Francesco Cesarini and Simon Thompson. To get started you can either download it from here or get it from your distribution’s repository if you are on a linux system. 
A good way to start learning would be to start looking at functional programming concepts, move towards various data structures and then look at distributed and concurrency features at the last. A sample program to show you how to define the fibonacci function would be 
as follows: 

fibonacci(0) -> 0 ; 
fibonacci(1) -> 1 ; 
fibonacci(N) when N > 0 -> fibo(N-1) + fibo(N-2) .
An example of immutable variables can be shown by trying to assign different values to a variable at different points in time: -
1> A = 456.
2> A = 345.
** exception error: no match of right hand side value 345

Lists in Erlang are ever present and can be used for many operations. They can be defined by square brackets and can contain anything, even functions : -
3> [a,b,c,34,45,[1,2,3]].
Tuples are another data type which can be used for comparison operations and are defined with curly braces: -
4> {a,b,c,34,45,{1,2,3}}.

Atoms are static string literals which are used internally for message passing and other such functions : -
5> hello.
This concludes our discussion and overview for this language but you can go through the links and documentation in the references section. 

Joe Armstrong’s Thesis
Erlang Handbook

For tutorials on 15 other hot programming languages go here.

Ankit MathurAnkit Mathur