Beginning with Erlang

A collection of bullet points I'm compiling during my reading of Programming Erlang by Joe Armstrong. This is a work in progress post which will finish once I finish reading the book and become an Erlang master of the universe.

Sun 20 Dec 09 9:33 a.m.

Joe Armstrong coined the term concurrency-oriented programming when referring to Erlang. There is an interesting interview with the author at InfoQ.

The Language

  • Concurrent and distributed programming by message passing.
  • Fault tolerance
  • Concurrency belongs to the programming language not the operative system like all other languages.
  • There is no shared memory between erlang processes, and therefore no locks and synchronized methods.
  • There is a Garbage collector like in Python and C#, so no need to worry about memory allocation.
  • = is not an assignment operator, it is a pattern matching operator. Pattern matching is a fundamental concept in Erlang.

Data Types

  • Variables starts with a capital letter, can be assigned only once and have scope inside the lexical unit in which the are define.
  • Atoms are used to represents non-numerical constant values, they starts with lowercase letter or can be quoted with a single quotation mark ('). Exaples are ciao or 'Ciao'.
  • Tuples are used to group a fixed number of items into a single entity. For example this tuple group together and atom and two variables:
Point = {point,2,8}.
  • To extract values from a tuple you need to use the pattern matching operator =.
  • The anonymous variable _ is used as placeorder of variables we are not interested in.
Trade = 
  • Lists like in Python are created using square brackets:
RandomList = [2,ciao,5+6,{bla,56}].
  • The first element a list the the head, while the rest is the tail. In the above example 2 is the head and [ciao,11,{bla,56}] is the tail. Accessing the head a list is a very efficient operation.
% Extract head and tail of the list (very efficient)
% H and T are unbound variables
[H|T] = RandomList.
  • Strings are lists of integers. They are created using the double quotation.
Name = "luca".
% and equivalently
Name2 = [$l,$u,$c,$a].
  • The dollar sign in front of a character is the integer which represents the character.
  • Typing f(). makes all variables to become unbound.

Functional Programming

  • Functions defines one or more clauses separated by semicolumn. Each clause has a head and a body. The clauses are tried in order they appear in the function definition. Clauses are similar to overloaded functions in C++ and other languages.
area({rectangle, Width, Height}) -> Width*Height;
area({square, Side}) -> Side*Side;
area({circle, R}) -> 3.14159*R*R.
  • The Arity of a function is the number of arguments that the function has. Two functions with same name but different arity represent entirely different functions. In the above example the area function has 1 arity.
  • Funs are anonymous functions with no name, They are similar to the lambda operator in Python.
Square = fun(X) -> X*X end.
Area = fun({rectangle, Width, Height}) -> {Width*Height};
          ({square, Side}) -> {Side*Side};
          ({circle, R}) -> {3.14159*R*R}
          end.
  • Funs are like variable and can be used as arguments to functions and functions and funs can return funs.

Abstractions like C++ template metaprogramming

Erlang is a functional programming language, therefore everything is defined using functions. Defining new controls in Erlang is very similar to template meta-programming in C++. Let's have a look at this example where I implement a for statement which is not available in erlang (No for statement in a programming language!?). To do that I write a function with arity three and two clauses (I feel an expert already, one day into the learning process)

for(End,End,F) -> [F(End)];
for(I,End,F)   -> [F(I)|for(I+1, End, F)].

Syntactic Constructs

LIst Comprehensions

LIst Comprehensions are expressions to create lists without having to use funs, maps or filters. They are available in various programming languages, including Python.

[[X || Qualifier1, Qulifier2, ... ]]

where X is an arbitrary expression, and each qualifier is either a generator or a filter. A generator is written as pattern <- listExpression while a filter functions or pridicates which evaluates to true or false.

Guards

Interesting stuff. A guard is a series of guard expressions separated by commas,

mygard = Gardexpr1, GardExpr2, ...

mygard is true only if all guards expressions are true. Easy enough.

STILL READING THE BOOK... page 140