Iain Munro

Iain Munro

Software engineer

Erlang – Challenge: project euler

To finalize the school assignment I have to complete a final challenge. The challenge was completely up to me as long as it fits the language (Erlang) and its paradigms. Also, the challenge must be somewhat challenging for me. With a limited amount of time, I came up with the following challenge:

Solve 2 problems from project Euler challenges using Erlang.


Erlang day 3 – Concurrency

Being a Gopher this was the most interesting part. Concurrency with Erlang. Similar to go, you have a function called spawn. Using spawn we can spawn a basic receive loop that will pick message of a queue and match them to some function within receive.

Processes communicate between one another with message passing. It’s no accident that Dr. Armstrong calls Erlang a true object-oriented language! It gives you message passing and encapsulation of behavior. We’re just losing mutable state and inheritance, though it’s possible to simulate inheritance, and more, through higher-order functions.Bruce Tate

Then it goes on about how to improve reliability by signaling a linked twin process when a process is about to die (a doctor process, to revive it). So that we can react accordingly (similar to a panic defer in Go). The most robust fault-tolerant systems are built by accepting failure and restarting the process if it dies (the “Let it crash” philosophy), instead of handling errors constantly.

All of this was very important for the Erlang challenge I’ve got in mind. But first, the exercises!


Translate service with simple recovery

Monitor the translate_service and restart it should it die.

Translate service with doctor monitor

Make the Doctor process restart itself if it should die. 


Make a monitor for the Doctor monitor. If either monitor dies, restart it.

All this is an abstract erlang module we’ll call the doctor that will check our service, instead of our service checking itself. So we remove the watch_loop from our translator_service module.

Erlang Day 2 – High-order functions and advanced list concepts

I’m starting to get into functional programming. The best thing I like so far of it all is that all of this is extremely easy to test! The prolog-based syntax of Erlang is still quite annoying, I constantly forget to end lines with dots and forget when to make use of semi-colons. Probably that because I was most familiar with the C-style languages and its on top of the functional programming paradigm that has its own set of challenges. But I’ll slowly get used to it!

Another interesting read is probably: Don’t drink too much Kool-Aid It talks about how you should never go full retard and think that Erlang is the answer to all your problems 🙂 It did get me excited for functional programming though! (I think might try F# and Haskell next!)


List key lookup

Consider a list of keyword-value tuples, such as:
[{erlang, "a functional language"}, {ruby, "an OO language"}]
Write a function that accepts the list and a keyword and returns the associated value for the keyword.

Looking back to it, this could also have been done with just a pipe and filter. Which I’ll use in the next exercise.

Total price

Consider a shopping list that looks like [{item quantity price}, …]. Write a list comprehension that builds a list of items of the form [{item total_price}, …], where total_price is quantity times price.

So we’ll order some beer and some snack to go with it!

Bonus: Tic-tac-toe

Due to a far too busy week, full of exams, deadlines I wasn’t able to get around making this exercise. However, I would probably make a solution using the switch mapping all the possible outcomes.

Erlang Day 1 – Functional goodness

Functional languages are at a higher level of abstraction than object-oriented languages. Though they are more difficult to understand, you can express bigger ideas with less code. Bruce Tate

Erlang is a functional language. It is strongly, dynamically typed. There is not a lot of syntax, but what is there is not at all like the typical object-oriented languages. If you are a pure object-oriented programmer, you may struggle a little bit, but don’t fight it. This isn’t the case for me though and I was quick to understand the syntax and foundational principles in Erlang. However, is my first pure functional language (Scala is a hybrid functional/object-oriented language.), that means:

  • My programs are going to be built entirely out of functions, with no objects anywhere.
  • Those functions will usually return the same values, given the same inputs.
  • Those functions will not modify the programs state or have any side effects.
  • You will only be able to assign any variable once.


Using Mac OS the first thing I did was use the package manager brew to install Erlang. Sure enough, it found a package and proceeded to download and install the package.

The final step is choosing an IDE. I’ve always heard good things about Visual Code and wanted to try it out. So instead of my usual Jetbrain IDE I opted for an open source Microsoft one! Yeah, you read that right. If you haven’t caught on, Google it. It’s an excellent alternative with lovely packages for any Language under the sun.


Erlang has a things called Modules. All functions in Erlang must be defined in modules. Erlang comes with a bunch of standard modules, much like you have existing packages in Java. The difference is that modules are a bunch of functions saved under in one, rather than a group of files. Every other function defined in a module you will ever use needs to be called using the form:


An Erlang file starts with defining the name of the module. The second line defines a function that you want to use outside of the module. The function is called “example”, and the /1 means it has one parameter. Finally, you get to the function itself. You can see the influence of the Prolog-style rule. The function definition names the function and determines the arguments. Afterward, you have the -> symbol, which simply returns the first argument.

Running this piece goes as follows:


Apart from the book, I used learn you some erlang website to quickly get started with Erlang.

Recursive string count

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

If this exercise was hard to understand. This is the video that explains the whole recursion, tail thing trick you’re seeing put to use here (start 33 minutes in):

Recursive count

Write a function that uses recursion to count to ten.

Form error handling

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

Erlang Day 0 – Introduction to Erlang

In the course Algorithms Programming language & Paradigms, I was taught the most important programming paradigms and some core languages. However the course can’t cover all of them, so in order to complete it, I received an assignment which included reading the book Seven Languages in Seven Weeks by Bruce Tate.

The goal of this assignment is to take me on an enlightening journey that will challenge the way I look at programming. The instructions were simple: Choose a language covered in the book, do the exercises of that language and create your own challenge. The result of all of this was to be put in a few blog posts, which you are reading now.

The languages that are covered in the book are Ruby, Io, Prolog, Scala, Erlang, Clojure, and Haskell. Out of all these languages, I chose Erlang.


The name might sound strange, but it is an acronym for Ericsson Language. In 1986, Joe Armstrong developed the first version at Ericsson, continuing to develop and polish it through the last half of the decade. Through the 1990s, it grew and gained more traction in the 2000s. It is the language that powers projects such as CouchDB and SimpleDB, popular databases for cloud-based computing. Erlang also powers Facebook’s chat. The buzz for Erlang is growing steadily because it provides what many other languages can’t: scalable concurrency and reliability.

A video I watched years back which first referenced Erlang and joked about the hipster languages like NodeJS and Ruby. It completely changed my view on programming languages and it’s heroin for the soul.

Why Erlang?

Erlang was the kind of language I always wanted to try. The philosophy of lightweight processes and the way it accepted failure always was a big allure to me. Few languages have the mystique of Erlang, the concurrency language that makes hard things easy and easy things hard. Its virtual machine, called BEAM, is rivaled only by the Java virtual machine, for robust enterprise deployment. BEAM compiles Erlang to C using a mix of natively compiled the code and threaded code to strike a balance between performance and disk space.

You can call Erlang efficient, but Erlang’s syntax lacks the beauty and simplicity of, say a programming language like Golang or Ruby.

For a school assignment, the safe option would’ve been Scala. This wouldn’t have been much of a challenge for me, after writing Java applications for the better part of my career. It also wouldn’t force me to write in a functional way, the way Erlang would. Here are some reasons I could think of why I think Erlang is worth a trying and learning from:

  • The problem with languages like Scala is that you can write the same code in 15 different ways. That just gives us 15 different ways to screw up. With Erlang, you don’t have this issue.
  • Hot-swap code. Meaning you can replace pieces of your application without stopping your code.
  • Erlang combines the robust “Let it crash” error strategies.
  • Lightweight as the primary concern. This means, no threads. A different way of concurrency.