July 22, 2007 by alex
I just started reading the latest book from the pragmatic programmers: Programming Erlang. In this series of articles, I’m going to summarize and comment on every chapter I read. Enjoy. (The reason I’m doing this in English is that I simply feel like writing some English again and that I hope to reach a larger audience :) )
Erlang is a programming language which has things like parallel processing and functional programming features built right into the language. Software written in Erlang is therefore supposed to utilize the power of multi core processors found in today’s and future computers much better than software written in … my precious Ruby? omg. Anyway, Erlang seems to be a language with features completely different from what I’ve seen before, so that’s my main reason to start reading about it, and we’re supposed to learn a new new language every year anyway. Let’s get started.
The Erlang SDK can be installed on all the big 3, i.e. OSX, Linux and Windows. For OSX, just use MacPorts and after a sudo port install erlang we’re set to go.
For getting started with Erlang, we can use the Erlang shell. which can be started using the erl command. Now we can start entering Erlang expressions and the shell will evaluate these. (Note: Every line ends with a period)
This is a simple addition and pretty boring so let’s move on.
Variables are the first thing Erlang handles differently than the “normal” languages. First, all variables have to start with an uppercase latter. Second and more importantly, all variables can only be assigned a value once, so they’re actually constants, after we assign them a value. Example: (The % denotes a comment.)
The book states that having immutable variables makes our programs less error prone, because we know exactly which variable has which value - so far soo good. I feel the more important consequence from having only constants is the other advantage mentioned: it makes things much easier when it comes to parallel processing, because you can avoid sharing memory among the processes running in parallel. Makes sense, somehow.
The other important feature of Erlang mentioned in chapter 1 is this: The “=” does not represent an assignment operator but is a pattern matching operator. This means that when using the “=”, Erlang doesn’t simply assign the value of the right hand side to the variable on the left hand side but it tries to match both sides of the equation.
This is of course just a very basic example, the more interesting stuff comes with the different data types. Read on.
Erlang of course has the basic data types such as integers, floats and strings. (where string are actually lists of integers). In addition we get tuples, which are like C-structs:
This creates a tuple with 2 values and stores it into the variable MyTuple. To read the second value of the tuple, we again use the pattern matching operator:
Now Erlang tries to match the two tuples and hence puts “doe” into the LastName variable. The underscore represents something like /dev/null, so “joe” is not assigned to anything here.
Another basic type are lists. Lists are defined using square brackets:
Another way to retrieve the first value in the list is the | operator: |
It stores the first value in First and the rest of the list in Rest. Sounds like good old recursive list processing, we’ll see.
That’s it for now - I hope to read at least the next chapter over the weekend. Stay tuned for more.