Posts Tagged ‘erlang’

Introduction to Erlang : Control Flow

This entry is part 8 of 16 in the series Introduction to Erlang

Control Flow

As we saw in the previous post, pattern matching with different function clauses can be used in order to control the execution flow in Erlang. Erlang also provides the if, case, and receive control flow constructs that can be used in a function body. In this post I will only present the if and casestatements since receive is used for message passing and I will write a dedicated post about the subject. Both if and case are similar to the equivalent statements of other programming languages.

if statement

The format of an if statement in Erlang is the following:

    Boolean_Expression1 ->
    Boolean_Expression2 ->
    true ->

So the different clauses, except the last one, are like else if in other languages, while the last one (true ->) is like the else; it succeeds when all the previous clauses have failed.
Read the rest of this entry »

Introduction to Erlang : Declaring Functions

This entry is part 7 of 16 in the series Introduction to Erlang


As you know by now, Erlang is a functional programming language. As this suggests, functions are the basic “ingredient” of an Erlang program. In my point of view, different programming paradigms pose different problem solving philosophy:

  • Procedural: describe the steps needed to be taken to solve the problem
  • Object-orientation: design the objects that will lead you to the solution
  • Logical (Declarative): describe the problem properly and let the language solve it
  • Functional: define small and precise functions that alltogether solve the problem

With this in mind, lets continue on how to declare a function (in a module).


While introducing functions, I will use several examples that implement list functions, although there are built-ins (BIFs) that implement the same functionality. The reason I will do so is that most of these functions are small, easy to understand, and operate on lists; one of the most, if not the most, important type in Erlang.

Declaring a Function

A simple function declaration has the following format:

function_name(Argument1, Argument2, ...) ->
    ... .

Where a statement can be another function call, an assignement, a comparison, a control statement (if for example), or a statement called for its side effects.
Read the rest of this entry »

Introduction to Erlang : Modules & Compilation

This entry is part 6 of 16 in the series Introduction to Erlang


A module is a container for functions; it provided the contained functions with a common namespace. Modules are used to organize functions in Erlang. Usually, a program in Erlang spans over more than one modules. You can imagine a module as a package in Java, or a header file in C.

Calling a Function

The calling convention in Erlang is module:function(argument1, argument2, ...). For example:

1> lists:max([1,3,2]).

Defining Modules

Lets say we want to create a module that will contain our own implementation of list functions and name it mlists.
Read the rest of this entry »

Introduction to Erlang : Basic Types (2/2)

This entry is part 5 of 16 in the series Introduction to Erlang

More Data Types

Today we will see some more sophisticated Erlang’s data types; tuple, list, and fun.


Tuple is a compound data type; it consists of elements of any data type. A tuple has the form:

{Element1, Element2, ..., ElementN}

where N is called the size the tuple.

1> {1,2}.
2> {true, {value, whatever}}.
3> {1, a, 1.1, {{{{4}}}}}.
4> is_tuple({}). % built-in function (BIF)
5> element(3, {1,2,3}). % the element at position 3
6> size({1,2,3}).% the size of the tuple

Read the rest of this entry »

Introduction to Erlang : Basic Types (1/2)

This entry is part 4 of 16 in the series Introduction to Erlang

Command Terminator

Erlang uses a simple dot (.) as the command terminator. Consequently every correct Erlang statement should terminate with a dot.

1> 1
1> 2.
* 2: syntax error before: 2
1> 1.
2> 2.

The Most Basic Types

I will introduce the data types of Erlang and the basic operations on them by example. Most of the material presented today will look familiar to other programming languages. The next post will present some more sophisticated data types.
Read the rest of this entry »

Introduction to Erlang : Typing

This entry is part 3 of 16 in the series Introduction to Erlang

In this post I will present you the data typing in Erlang.

Dynamic Typing

Erlang is a dynamic typing programming language. That means that when “declaring” a variable you do not need to statically specify the type it will be (as in static typing languages). Read more about dynamic typing here. For example, this is how you would declare and initialize an integer in C (static typing):

int i = 17;

while in Erlang you would do: (we will see how to declare variables in a while)

I = 17.

This approach has both advantages and disadvantages. For example, when programming, it is fast and convenient not to declare the variables’ types, but in big projects it can lead to code readability problems unless well documented.

Variable Declaration

Erlang is heavily influnced by Prolog, so Prolog programmers should be ready to discover several commonalities. As with Prolog, the variable declaration is implicit; every character sequence generated by the regular expression [A-Z_]([0-9a-zA-Z_]*) is a variable. Some examples are A, Ab, A_b, Ab1, _, _A_b3. Just to mention that the ones starting with “_” has a special meaning; they are “don’t care” variables. When you use them, the value they will take will probably not be used later. Something like place keepers. You will meet them again and again, so don’t worry for now.

Variable Assignement

Another importand characteristic that Erlang inherited from Prolog is binding with pattern matching. In a nutshell, a value is not assigned to a variable, but bound with pattern matching. The most important consequence of this is that variables in Erlang are single assignement. Once bound to a value, their value cannot change for their lifetime. For example, in an emulator try the following:

1> A = 10.
2> A = 11.

The output you will get is:

** exception error: no match of right hand side value 11

What happens is that A is bound to the value 10, so Erlang tries to pattern match 10 with the value 11, which is impossible.


Since I want to keep the posts rather small, I will break this one here and I will present the actual Erlang types on the following post(s).

Introduction to Erlang : Installing Erlang

This entry is part 2 of 16 in the series Introduction to Erlang

About Erlang

Erlang is a functional programming language which has many features more commonly associated with an operating system than with a programming language: concurrent processes, scheduling, memory management, distribution, networking, etc.

The initial open-source Erlang release contains the implementation of Erlang, as well as a large part of Ericsson’s middleware for building distributed high-availability systems.
Read the rest of this entry »

Introduction to Erlang post series

This entry is part 1 of 16 in the series Introduction to Erlang

While I was writing my first actual (and extremely delayed :-S) article for the Parallelizing simple algorithms series, I realized that since Erlang is not a popular programming language it would be nice to start an Introduction to Erlang post series. I consider Erlang as a must-know language for an engineer that works with distributed systems and parallel programming. Believe me, in several cases, Erlang is a problem solver!

I will try to keep the posts short and example based. An approximation of the posts that I intend to write is:

  1. Basics: how to get a working Erlang environemt
  2. Basic Types: integers, floats, …
  3. Modules and Compiling: how to write and compile a module
  4. Functions: how to delcare functions
  5. Library Functions & BIFs
  6. Lists: list manipulation
  7. Processes: how to create new processes
  8. Message Passing: how to send messages between processes
  9. Debugging: how to debug Erlang programms
  10. Records: how to use records

I will keep this list updated in case that I come up with new ideas!

I hope I will convince you that Erlang worths every software engineer’s attention..