Erlang Day 1

My experience with day one of exploring Erlang through Seven Languages in Seven Weeks.

Next up in the languages is Erlang a language developed by Ericsson during the 80’s because they couldn’t find a language which met their reliability needs. This included needing to be able to update the code on the fly without stopping and bringing down their systems. Some applications written in Erlang have run for years without ever coming down for maintenance.

I have to admit to not knowing much about Erlang before reading the book so I was interested to see it is used in Social Networking and Cloud Computing. It powers Facebook Chat and is behind CouchDB and SimpleDB , two databases used in cloud computing.

Let’s take a look at some of the characteristics of Erlang.


Like Io and Scala, Erlang uses Actors for concurrency. Whereas in Scala an Actor was an object backed by a thread pool in Erlang it is a lightweight process which reads inbound messages from a queue and uses pattern matching to decide how to process it. Erlang’s creators aim was to simplify the creation, management, and communication within applications by using lots of lightweight processes avoiding the pain of threading e.g. needing to handle sharing of resources using strategies such as locking.


I’m used to writing application with error handling. Erlang takes a different strategy, “Let it Crash”. This works for Erlang because it has good support for monitoring for the death of a process, killing related processes and starting new ones. As mentioned previously you can hot swap code in which makes maintenance a lot simpler as you don’t have to plan for an outage.

Functional Language

Erlang is the first Functional Language the book covers. Scala combined it with OO so there were still objects. In Erlang it is all functions with no objects. Functions normally return the same value given the same inputs and don’t have side effects i.e. they won’t modify state. In a functional language you can only assign any variable once. Erlang is not a pure functional language (otherwise the previous statements would say “Functions always return the same value given the same inputs” etc.) It has the odd exception. Haskell the last language in the book will let me see a purely functional language.

Let’s Get Coding!

Enough talk. Time to write some code and get a feel for the syntax. Starting off with some basic examples. You can see that there is basic type coercion in Erlang between number types but not between a number and a string.

String are actually just lists hence the following actually translates as “Ha Ha Ha”! I wasn’t expecting that!

One things that really caught me out at the start was that variables must start with a capital letter. Hence the following error when you forget to capitalise.

They are immutable. Once you’ve assigned a value that’s it. No changing it.

Atoms, Lists, and Tuples

A symbol is the most primitive data element in Erlang. It begins with a lowercase letter and is called an atom.

Below are some list examples. Lists can hold a range of types and can be any length

Tuples are fixed length heterogeneous lists.

In Erlang you can use tuples as you would maps or hashes in other languages

In the example below we use an atom as the key and a string as the value. Then we create a hash using atoms as keys and strings for values. We can access the individual elements using pattern matching like in Prolog.

Pattern Matching

Lets take a closer look at pattern matching in Erlang. In the example below we represent a person using a tuple.

We can then match the atoms person, name and profession to themselves in the Person tuple.

The variable Name is matched to ‘Jenson Button’ and Profession to ‘Motor Racing Driver’.

This feature is the key decision making construct. The initial atom, person, leads to a different stucture to what I’m used to in Java but including such an atom allows you to quickly match tuples of a particular type and disgard others.

List pattern matching is similar to Prolog’s. Again we can use head and tail to split a list into different variables.

If there are not enough elements in the list, the pattern won’t match:

This throw a bit of light on why we got the error message we did when we accidentally wrote a variable with a lowercase letter.

The = is not assigning it is pattern matching so you’re asking integer 1 to be matched with atom one which it can’t be hence the error.

Bit Matching

Being able to work at bit level is useful if you’re working with JPEGs or MPEGs or cramming data into minimal space. Here is Erlang’s syntax for working with a bitmap.

Packing the bitmap.

Unpacking the bitmap.


Erlang is dynamically typed, binding types at run time, based on syntactic clues such as quotes or decimal points.

Erlang files have a suffix of .erl which produces .beam executable when compiled. Why beam? Well this is the name of Erlang’s powerful VM.

Let’s look at an example

Compile using the following syntax in the console


Running the executable

% ** exception error: undefined shell command mirror/1
basic:mirror(smiling_mug). %smiling_mug
basic:mirror(1). %1

A couple of key things from above you need to include the module name and you can bind the function to two different types because Erlang is dynamically typed.

In the next example we create a matching function where each option has the function name, the argument you wish it to match and the value to be returned.

Executing it

-c(matching_function). % {ok,matching_function}
matching_function:number(one). % 1
matching_function:number(two). % 2
matching_function:number(three). % 3

Here are some examples of recursion

We’ve handled the special cases and then used recursion to calculate the rest. Similar idea to previous factorial and fibonacci examples just a slightly different syntax.

Self Study


The Erlang language’s official site

Official documentation for Erlang’s function library

The documentation for Erlang’s OTP library


  • Write a function that uses recursion to return the number of words in a string.

I split the string into a list of words and then use recursion to count the number of words.

  • Write a function that uses recursion to count to ten.

count is a basic version that only counts from a number to 10. count2 allows for different end numbers and carrys out a check to make sure the start number is smaller than the end number.

  • Write a function that uses matching to selectively print “success” or “error: message” given input of the form {error, Message} or success.

I print the appropriate format of message based on whether a string or tuple is passed.

Check out my code at github