Programming Erlang - Chapter 1: Getting Started

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
)
What is Erlang and why read that book?
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.
Installation
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.
Getting started
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
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.
Pattern matching
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.
Data types
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.
Tags: book, erlang, programming, programming-erlang, tutorial





July 23rd, 2007 at 8:32 am
What happens when you make a mistake? Do you end up defining a new variable, X1, then X2, etc.? I’d be surprised if Erlang didn’t let you clean up your environment in some way.
July 23rd, 2007 at 9:01 am
yes, you can call f() (forget), which cleans all your variables.
July 23rd, 2007 at 10:27 am
You can also use f(X). to just forget the variable X.
July 23rd, 2007 at 10:35 am
cool, thanks.
July 23rd, 2007 at 11:56 am
[...] 4. Programming Erlang - Chapter 1: Getting Started. (link) Erlang是将并发和函数编程融会的语言,cool tutorial! To be added. This entry was [...]
July 24th, 2007 at 12:12 am
[...] hopefully be some kind of erlang tutorial, for your pleasure and my reference Here’s the first post and now comes the [...]
July 31st, 2007 at 11:40 pm
What a cool blogging idea! Hope you like the book, I’ve read Joe’s dissertation and some other PDFs. I will be ordering my copy of “Programming Erlang” soon.
August 1st, 2007 at 9:27 am
yes the book’s great. if i only had more time to read it.
next chapter over the weekedn (hopefully).
December 16th, 2007 at 1:03 am
very interesting, but I don’t agree with you
Idetrorce
August 9th, 2008 at 6:05 pm
Rimsky went look closer buy cytotec dead hand held.