Hello Erlang

I was talking with someone and the subject of Erlang came up and I realized that I have been hacking on Erlang for about a year now. So I thought I would do a quick post to encourage other folks to check it out.

Erlang is a programming language designed for building highly parallel, distributed, fault-tolerant systems. It has been used commercially for many years to build massive fault-tolerant systems which run for years with minimal failures.

Erlang programs run seamlessly on multi-core computers. This means your Erlang program should run 4 times faster on a 4 core processor than on a single core processor, all without you having having to change a line of code. Erlang combines ideas from the world of functional programming with techniques for building fault-tolerant systems to make a powerful language for building the massively parallel networked applications of the future.

In Erlang, variables are just like they are in math. When you associate a value with a variable, you’re making an assertion-a statement of fact. This variable has that value. And that’s that.

Erlang has single assignment variables. As the name suggests, single assignment variables can be given a value only once. If you try to change the value of a variable once it has been set, then you’ll get an error. A variable that has had a value assigned to it is called a bound variable; otherwise, it is called an unbound variable. All variables start off unbound.

For example, when Erlang sees a statement such as:

X = 1234

It binds the variable X to the value 1234. Before being bound, X could take any value: it’s just an empty hole waiting to be filled. However, once it gets a value, it holds on to it forever.

To get started with Erlang, you will need to download the latest tar and install from source:

{~}$ mkdir erlang
{~}$ cd erlang
{~}$ cd ./erlang/
{~/erlang}$ curl -O http://erlang.org/download/otp_src_R13B01.tar.gz
{~/erlang}$ tar -xzf otp_src_R13B01.tar.gz
{~/erlang}$ cd otp_src_R13B01
{~/erlang/otp_src_R13B01}$ ./configure
{~/erlang/otp_src_R13B01}$ make
{~/erlang/otp_src_R13B01}$ sudo make install
{~/erlang/otp_src_R13B01}$ cd
{~}$ sudo rm -rf ./erlang/

Now a simple program, at your terminal prompt type:

{~}$ erl
Erlang R13B01 (erts-5.7.2) [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]

Eshell V5.7.2 (abort with ^G)
1>

This starts up the Erlang shell which will let your write simple programs. Next type the following:

1> Hacker={person,{name,{first,carlos},{last,gabaldon}},{languages,ruby,python,java,c,lisp}}.

So what we have done is created complex tuple that we have bound to the variable *Hacker*. A Tuple is a fixed number of items grouped into a single entity, similar to structs in C. The shell will spit back the entity that you created:

{person,{name,{first,carlos},{last,gabaldon}},
{languages,ruby,python,java,c,lisp}}

Next we want to extract some value from our entity. To do this type the following:

2> {_,{_,{_,Who},_},_} = Hacker.

What we have done is use the Erlang pattern matching operator which is the *=* and *_* placeholders for variables that we are not interested in. Which we bind to the Who variable.
Next type the following to see the value:

3> Who.
carlos

Well that is a very quick syntactical introduction to the powerful concurrency-oriented programming language called Erlang.

Happy Hacking..

Leave a comment