Learning a functional language: Erlang

Last years I’ve had some contact with Ruby, Python or Objective C, but it never has been something more than a first contact, it’s now since assisting to the WeLovePHP talks at Softonic (Barcelona) when my interest for a more serious approach to functional languages has come. After some days reading about functional languages Erlang was the chosen, maybe because the hype of this days because of being used at Facebook, at Amazon, or Whatsapp, or maybe because some of it’s unique characteristics.

Other functional languages I was considering:

  • Scala Runs on the JVM, access to java libraries, strongly typed, combines functional language with object oriented.
  • Clojure Runs on the JVM, access to java libraries.
  • Haskell Strong static typed.

Some info on Erlang

  • Framework OTP, it comes bundled with Erlang
  • Web Framework Chicago Boss is probably the most important
  • Databases We can use relational databases as MySQL through Erlang ODBC, there are also NoSQL databases created with Erlang, as CouchDB or Riak

Install Erlang

Erlang can be installed on Mac (my notebook is a Macbook), but I choose installing it on an Ubuntu’s virtual machine that I already have for Laravel testing. In this case it installs really ease using the terminal, in Erlang Solutions there are the instructions for installing on any Operating System.

Ubuntu (probably Debian is the same) Erlang installation:

wget http://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb
sudo dpkg -i erlang-solutions_1.0_all.deb
wget http://packages.erlang-solutions.com/ubuntu/erlang_solutions.asc
sudo apt-key add erlang_solutions.asc
sudo apt-get update
sudo apt-get install erlang

and only with this, we already have the Erlang interpreter working!

Erlang

First steps with the Erlang shell

Variable assignment

In Erlang variables can be assigned only once

Erlang

if we try to change it’s value after assign it for the first time, we’ll get an error. Other important thing in Erlang is that the expressions are finished with a dot “.”

Erlang

The “b()” function will show us the variables already assigned:

Erlang

Functions

A simple function in Erlang would be for example to divide any amount by the half:

TheHalf = fun(Amount) -> Amount / 2 end.

And to call the function:

TheHalf(5).

On Erlang shell would be something like this:

Erlang

Did you test Erlang? Did you choose any different functional language? leave your comment!!!


Leave a Reply

Your email address will not be published. Required fields are marked *