Programming Erlang - Chapter 1: Getting Started
July 22, 2007 on 12:54 am | von Alexander Lang |
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.
9 Comments »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^
post to del.icio.us


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.
Comment by Ricky Clarkson — July 23, 2007 #
yes, you can call f() (forget), which cleans all your variables.
Comment by Alexander Lang — July 23, 2007 #
You can also use f(X). to just forget the variable X.
Comment by Ulf Eliasson — July 23, 2007 #
cool, thanks.
Comment by Alexander Lang — July 23, 2007 #
[…] 4. Programming Erlang - Chapter 1: Getting Started. (link) Erlang是将并发和函数编程融会的语言,cool tutorial! To be added. This entry was […]
Pingback by Short but interesting FP tutorials « Robot Brain — July 23, 2007 #
[…] hopefully be some kind of erlang tutorial, for your pleasure and my reference Here’s the first post and now comes the […]
Pingback by upstream - agile web (2.0) softwareentwicklung » Programming Erlang: Functional Programming — July 24, 2007 #
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.
Comment by iWantToKeepAnon — July 31, 2007 #
yes the book’s great. if i only had more time to read it.
next chapter over the weekedn (hopefully).
Comment by Alexander Lang — August 1, 2007 #
very interesting, but I don’t agree with you
Idetrorce
Comment by Idetrorce — December 16, 2007 #