Iain Munro

Iain Munro

Software engineer

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.

Installation

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.

Modules

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:

Module:Function(Arguments)

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:

Exercises

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.

DROP A COMMENT

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