Erlang Introduction (For the Ruby Guy) part 1
Wednesday, August 6, 2008 at 07:41PM The purpose here is to write a basic beginners info for erlang. It's not in any way a tutorial but it's aim is to peak your interest in the language. This is aimed at Ruby programmers and tries to give a good overview of the basic differences and similarities. Both in programming and thinking.
Erlang is a wonderful tool to learn and gives you the same enjoyable programming experience as most have with Ruby. And it's a great match to use them together in a product. Ruby for the front end to ease making a good user experience with trusty old Erlang in the background making sure everything is rock stable.
The power of Erlang lies in it's ability to be scalable and stable. To put it bluntly. If your Erlang application is unstable and does not scale well then you have done something wrong.
The Shell
We start off on a familiar ground. The main Erlang executable is the command erl. This should instantly feel as home to Rubyist and remind of trusty old irb. What it can not do however is create the modules and functions. It can only call them. However it's powerful and you should expect to spend a lot of your time inside there. Remember to end your statements with a period to execute them.
Variables
Variable start with a capital letter. The first thing that shocks people is the fact that you can only set a variables once. Thus typing Var1 = 1 and then immediately Var1 = 2. would give you an error. Think of it as a strength though rather than a problem. The simple, money back, guarantee is that this will never be a problem for you and you won't need to think about this since functions are short and variables only exist inside the function. What usually happens is that people start actually liking having the variables single-assigned since it tends to simplify things and reduce errors.
Atoms are Symbols
Atoms start with a downcase letter or are enclosed in single quotes. They can not hold any value and represent only themselves. They are basically the same as symbols in Ruby. One thing to remember is that atoms do not disappear from memory after being created so if you in some way create them dynamically you may fill up your memory after a while. This may not sound like a problem at first but remember that your goal is probably to create an application that will run for months or even years without being restarted.
Lists and Tuples
Lists and Tuples are similar in many ways. Both represent a collection of elements. The difference is that lists have variable number of elements while tuples have fixed number of elements. Lists are defined between [...] while tuples are between {...} I read somewhere that lists are like arrays while tuples are like hashes. That is 100% totally incorrect. Tuples are nothing like hashes. The most common use of tuples you will see is that the first element is a atom that explains the rest of the tuple. For example the tuple {error, 404, "File Not Found"}. Often you will also use a list of tuples. For example [{person,"Steve"},{person,"Joe"}]. How this all matters is best explained in the next section.
Matching (or how = is not =)
The equal sign behaves quite a bit different from what you are probably used to. It's behavior is called Pattern Matching. What it does is to try to make the statement true and the simplest example is X = 1. Here erlang finds that the only way to make the statement true is if the variable X stores the value 1. Here we also see the difference in using tuples and lists. If we make the statement {error, ErrorCode, ErrorMessage} = {error, 404, "File Not Found"} it will find that to make this statement true it has to assign the variable ErrorCode to "404" and ErrorMessage to "File Not Found". Pretty nifty eh? With lists you can do things like [FirstElement|TheRest] = [1,2,3,4]. Here the variable FirstElement will become 1 and TheRest will become [2,3,4].
Conclusion
This is it for now. In the next article I will explain amongst other things modules and functions along with the real power of Erlang that lies in processes.
Or if you want to dive into actually learning Erlang I would recommend Kevin Smith's excellent screencast Erlang in Practice.
References (1)
-
Response: monroe shocksat monroe shocks on November 7, 2008When this series was first teased as to being started again in JANUARY and it finally came to fruition in AUGUST, I had my hopes set extremely high. This issue was not up to my expectations and there are several reasons for this, but let me begin with the positives. First ...


Reader Comments (3)
Thanks for this! You've done an amazing job at summarizing some of Erlang's features. I read Joe Armstrong's book some time ago, and this is a great review. I'm looking forward to reading the next part.
Thanks..
This is really just the basic beginning. And all the really interesting stuff that make Erlang a joy is left.
I have simplified things here though. The atom name rules are slightly more complex. Arrays and tuple are also simplified. I delibaratly try to say how it works from the beginners perspective. How it really works under the hood is something people find out soon enough. If they get interested they will buy a book, watch Kevin Smiths great screencasts or read Erlang's great documentation. I'm just not trying to directly teach anything here.
well jon that's exactly what i like to know when learning something new!
a gentle and short introduction with just enough information to let you comfortably think about!
thanks