Recently I’ve been teaching myself how to program in Erlang. What is Erlang? From the Erlang FAQ they explain it this way: “Erlang is a general-purpose programming language and runtime environment. Erlang has built-in support for concurrency, distribution and fault tolerance.”
Perhaps explaining why you might use Erlang will make it more clear. I’ll give two of the stronger arguments for learning Erlang.
If your like me you probably have a computer of some sort and chances are if the computer is fairly new (3 years or so) then it probably has two or more “cores“. Chances are pretty good the applications you run don’t make full use of the extra core(s). Erlang is a programming language that will let you write multicore applications to make use of that extra CPU power your computer has but may not be using to it’s full potential.
Another reason is perhaps you need to write mission critical applications and might even need to “swap out code” in a production system without taking the system down, Erlang can do this. Erlang is renown for its ability to run fault-tolerant applications.
Erlang runs on all major operating systems (Linux, OS X, Windows). You can download Erlang from the main site. I’m running Windows Vista so my screen shots and assumptions will be from a Windows perspective. Regardless you should be able to follow along with Linux or OS X just as easily with minor modifications.
I’ll assume you’ve now downloaded and installed Erlang. On Windows simply go into your start menu and locate the “Erlang” shell icon, on my system it mapped to here:
Fire up the shell and you should see something similar to this:
You can see from the screen shot that Erlang detected my two CPUs. If you have a quad core then you should see: smp:4
The first thing I will do is create a directory to store the tutorial files we are going to generate. Lets create a new folder here: C:\Erlang-Tutorial
Now we need to move to that folder from the Erlang shell. Type in the following command to get moved into that folder:
Before we actually start putting our programs into files, lets first mess around with the shell. At the prompt type in “2 + 3.” and press enter. Don’t forget the period at the end!
Keep in mind your commands need to end in a period or the shell will sit and wait until you finish. Go ahead and type in “5 + 4″ and press enter. Nothing. Now press “.” and press enter. We get our answer now of “9″.
Variables are interesting in Erlang because their values cannot change. This ties into the whole concurrency thing as well as less error prone Erlang applications, but for now you just need to know once a variable has a value you can no longer change it. Lets try this out – oh, and keep in mind variable names must start with an uppercase letter.
The first thing we did was assign the variable “X” the value “10″. Then we told the shell to print the value of “X” which it did. I then tried to assign the variable “X” another value which is against the rules and it gave me an error like it should. Finally, I used the “b()” command from the shell which prints out all the bound variables, in this case we just have the one which is “X = 10″. The last thing I did was “10 = X.” which is true so the shell prints “10″.
Here is the tricky part with the “10 = X”. The “=” sign is for a pattern matching operation in Erlang whereas in most other languages it is for assignment. Lets take a look at this some more.
I called the “f()” command to “flush” the variables in the shell so Erlang will forget about them, then I called “b()” just to be sure there were no extra variables hanging about. I then bind the value “10″ to “X” and then bind X’s value plus 5 to “C”. This gives us “15″. Therefore X cannot pattern match to C, but X can be pattern matched to “C – 5″.
Lets move into a text editor. Currently there are a few editors that support Erlang (JEdit, ErlyBird Erlide – just to name a few. I’m going to use Notepad++. Also, keep in mind both VIM and EMACS have good support for Erlang too.
Open up your editor of choice and save the blank file as “hello.erl” (Erlang calls this a module and they have the file extension of .erl) and put it into the directory we created at the start of this tutorial. Now enter in the following:
-module(hello). -export([say_hello/0]). say_hello() -> io:fwrite("hello\n").
Lets compile the file now and run it.
I called the “ls()” command to list the directory’s contents, it shows me that the “hello.erl” file is in the folder we are in. I then call the “c()” command to compile the file. Once the file is compiled we can run it, which when we do it prints out “hello”. Lets go through the code now.
The first line we setup our module with the name “hello”. We then export the only function in this module which happens to be called “say_hello”. The “/0″ simply means that the function doesn’t take any arguments. We move onto the actual function “say_hello” where we call the io library’s fwrite function and pass it “hello\” which prints out “hello” and a newline.
If we wanted to pass in an argument we could do this:
-module(hello). -export([say_hello/1]). say_hello(Name) -> io:fwrite("hello: ~s\n", [Name]).
You can see we have to modify our source code to take the parameter now, hence the “say_hello/1″ instead of “say_hello/0″ and adding the “Name” to the function signature.
Compile and run the program:
Lets close down the shell now. Type in this at the command prompt: halt().
We touched the simplest basics only and we got away without having to understand: atoms, tuples, or lists – or the fact Erlang is a functional language. You will need to learn about them though if you are to proceed in learning Erlang. I suggest the free tutorial.
Now, if your ready to build truly scalable and fault tolerant applications, then Erlang is certainly a possibility. Keep in mind though there are other languages that do pretty much the same thing like Scala, Haskel, and several others I haven’t mentioned.
There are several applications built in Erlang including CouchDB, ejabberd (Jabber server), Yaws, RabbitMQ, and its even been said that Amazon’s SimpleDB was written in Erlang. How is that for industrial strength?