We are a software consultancy based in Berlin, Germany. We deliver
high quality web apps in short timespans.

Upstream Agile GmbH

Programming Erlang - Chapter 1: Getting Started

July 22, 2007 by alex

programming erlang

I just started reading the latest book from the pragmatic programmers: Programming Erlang. In this series of articles, I’m going to summarize and comment on every chapter I read. Enjoy. (The reason I’m doing this in English is that I simply feel like writing some English again and that I hope to reach a larger audience :) )

What is Erlang and why read that book?

Erlang is a programming language which has things like parallel processing and functional programming features built right into the language. Software written in Erlang is therefore supposed to utilize the power of multi core processors found in today’s and future computers much better than software written in … my precious Ruby? omg. Anyway, Erlang seems to be a language with features completely different from what I’ve seen before, so that’s my main reason to start reading about it, and we’re supposed to learn a new new language every year anyway. Let’s get started.

Installation

The Erlang SDK can be installed on all the big 3, i.e. OSX, Linux and Windows. For OSX, just use MacPorts and after a sudo port install erlang we’re set to go.

Getting started

For getting started with Erlang, we can use the Erlang shell. which can be started using the erl command. Now we can start entering Erlang expressions and the shell will evaluate these. (Note: Every line ends with a period)

1> 5 + 10.
15

This is a simple addition and pretty boring so let’s move on.

Variables

Variables are the first thing Erlang handles differently than the “normal” languages. First, all variables have to start with an uppercase latter. Second and more importantly, all variables can only be assigned a value once, so they’re actually constants, after we assign them a value. Example: (The % denotes a comment.)

1> X = 3. % ok
2> X = 4. % not ok, X is already 3

The book states that having immutable variables makes our programs less error prone, because we know exactly which variable has which value - so far soo good. I feel the more important consequence from having only constants is the other advantage mentioned: it makes things much easier when it comes to parallel processing, because you can avoid sharing memory among the processes running in parallel. Makes sense, somehow.

Pattern matching

The other important feature of Erlang mentioned in chapter 1 is this: The “=” does not represent an assignment operator but is a pattern matching operator. This means that when using the “=”, Erlang doesn’t simply assign the value of the right hand side to the variable on the left hand side but it tries to match both sides of the equation.

1> X = 1 + 1. % if X gets assigned 2, the equation is true, so X gets assigned 2

This is of course just a very basic example, the more interesting stuff comes with the different data types. Read on.

Data types

Erlang of course has the basic data types such as integers, floats and strings. (where string are actually lists of integers). In addition we get tuples, which are like C-structs:

1> MyTuple = {"joe", "doe"}.

This creates a tuple with 2 values and stores it into the variable MyTuple. To read the second value of the tuple, we again use the pattern matching operator:

1> {_, LastName} = MyTuple.

Now Erlang tries to match the two tuples and hence puts “doe” into the LastName variable. The underscore represents something like /dev/null, so “joe” is not assigned to anything here.

Another basic type are lists. Lists are defined using square brackets:

1> MyList = [1, 2, 3].
2> [First, _, _] = MyList.
3> First.
1
Another way to retrieve the first value in the list is the operator:
1> MyList = [1, 2, 3].
2> [First|Rest] = MyList.
3> First.
1
4> Rest.
[2, 3]

It stores the first value in First and the rest of the list in Rest. Sounds like good old recursive list processing, we’ll see.

That’s it for now - I hope to read at least the next chapter over the weekend. Stay tuned for more.