(From the Erlang home page)
“Erlang is a programming language used to build massively scalable soft real-time systems with requirements on high availability. Some of its uses are in telecoms, banking, e-commerce, computer telephony and instant messaging. Erlang’s runtime system has built-in support for concurrency, distribution and fault tolerance.”
Erlang is one of those languages like Factor or Scala or Forth that were designed to solve a very specific set of problems. As such, it’s never going to enjoy wide popularity.
That’s not to say that Erlang isn’t important. Your favorite web site may not run on Erlang code any time soon but it’s quite possible that your bank is using it.
There are two features of Erlang that I think I should explain in more detail.
“soft real-time” – A real-time system is one where data is processed and the response has to be immediate. The anti-lock braking system on your car is a good example. You don’t want to stomp on the brakes and have to wait for the software that controls your satellite radio to finish scanning channels. A soft real-time system is one where we need a quick response but not as quickly as a full-on “hard” real-time system like your car’s brakes. A good example is the software that updates flight plans for airlines. We want the plans to be kept current but we’re willing to accept a few seconds delay for processing time.
“concurrency” – We normally think of program code as executing one command after another in sequence, ie A then B then C. But what if A, B and C weren’t dependent on each other? We could do all three at the same time and get things done that much faster. Concurrent programming is designed to make it easy to split independent tasks and distribute them to different processors. The processors can be on the same computer or on multiple computers. This is also referred to as ‘parallel processing’. It’s useful if you’re processing a massive amount of data, like rendering CGI for a movie.
Erlang is also described as a declarative language. In declarative programming, the code describes what needs to be done without telling the computer how to do it. Declarative languages tend to have code that is very tight and fast but not very readable. Fans of declarative programming like it because it maps very closely to mathematical logic. So if you enjoyed doing proofs in geometry class, you’re going to love declarative programming.
Since Erlang programs are just plain text files, you can use almost any IDE you want. There are plugins for Eclipse, NetBeans, UltraEdit and other programming editors. I’m going to use a plain old text editor, good old TextWrangler.
First I have to install the language software. It’s available for Linux, Windows and Mac OS X. You can also compile it up from source. Since it doesn’t come with Apple’s Developer Tools, I had to install Erlang using MacPorts.
MacPorts is a project that lets you install free, open source software on Mac OS X. Most of them run from the command line so they’re not for casual users. But if you’re coming from a UNIX or Linux environment you can find all of your favorite tools and make yourself at home.
Normally, Erlang code needs to be compiled (converted to machine code) but you can also run it directly using a tool called Escript.
That being said, this is the hardest programming language I’ve approached yet. I mainly have experience with traditional imperative languages so I still have trouble changing my thinking to accommodate a declarative language like Erlang. The other problem is one of documentation.
Technical documentation falls into two broad categories. The first is what I call the ‘howto’. It walks you step-by-step through a procedure to accomplish a task. There is no context for what you’re doing and if there’s a typo or a missing step you’re stuck.
The second category is technical reference. These give you more information about what’s going on ‘under the hood’, but there’s no applied knowledge. In addition, they are written for people who don’t actually need or want to read them.
So far, Erlang documentation falls into the second category. Clearly if you want to write even a simple program in Erlang you need to be willing to make a substantial commitment. Don’t expect a lot of hand-holding.
For that reason, I’m going to just do a ‘Hello World’ program as a proof-of-concept and walk you through the steps I had to take to get there.
First the code:
Let’s take this one line at a time.
defines a function (or module) called hello. This has to be the first line.
This tells Erlang what part of this code can be called from outside the program. This lets you have some code that is internal to your program.
Remember that start we exported in the previous line? The code we’re exporting begins here.
The io module has a built-in function fwrite and we’re running it with the argument “Hello, world\n”. (The \n means print a newline at the end.)
I save this in a file called hello.erl.
I compile it.
erl -compile hello.erl
Now I can run the program.
erl -noshell -s hello start -s init stop
And the result?
Mission complete! Now I have to go lie down….