Jarosław Rzeszótko wrote this great article where he builds a POP3 to RSS gateway in Erlang. Very nice stuff and well written article on the topic. He’s also running for the Erlang blogging contest and I’m sure he has good chances to win!
The first thing you should learn about a new language is its small bricks, the data types. Once you know how to structure your data you can build your application on it.
So, let’s take a brief look at Erlang’s data types.
Erlang supports integer and floating point numbers. Integer numbers can have almost unlimited size. Try this on the Erlang shell (erl):
You can also have floating point numbers, but they have limited precision and size:
Strings are made of quoted text like “This is a String!”. Erlang deals with strings as they were lists of characters. Unfortunately when Erlang was born, his father didn’t knew about Unicode, so we can just have Latin-1 characters (ASCII 0-255) so far.
Atoms are names, constant text strings that you can use like C’s enums or Ruby’s symbols.
Atoms must be unquoted alphanumeric strings that begins with a lower case letter. You can also have non alphanumeric atoms that begins with any letter by quoting the string with a single quote.
Examples: apple, mice, dog, banana, ‘Valentina’, ‘This is an Atom!’.
Tuples are fixed sets of data. You cannot add anything to a tuple once you created it. Tuples can contain other tuples, atom, strings and lists.
Examples: {this, is, “a tuple”, with, 6, ‘Elements!’}, {1 + 2, milk, banana, [ 1, 2, 3 ]}.
Lists are similar to tuples but they can contain an undefined amount of items of any type:
It’s also very easy to split a list into his head and his tail, and that’s also the only way to access his elements (you cannot extract the nth element before the nth-1 elements):
You can also create new lists by adding elements in front or at the end of an existing one:
Last but not least we have funs that are anonymous functions. Erlang deals with funs as they were like any other data type. You can assign funs to variables, pass funs to as parameter to function calls and return funs as function results:
That’s all for now! I hope to have made less mistakes than the last time!
I’ve been a little busy in the past couple days, I was writing two articles for what I consider the most important italian programming magazine: Computer Programming. I remember when more than 15 years ago I bought the 1st issue of CP and I started learning how to program 8086 assembly. Ahhh, great times… ![]()
Anyway, these two articles will be probably published in the next months issues. The first one is an introduction to Erlang language and features and the second one talks more in deep about list management and advanced stuff like anonymous functions and lists comprehensions.
During the next month I’ll write one or two more of them on the most interesting topics like concurrent and distributed programming.
I hope to spread the Erlang verb a little more! ![]()
That’s what it takes to write an Hello World program in Erlang, after you installed the Erlang system on your machine.
Here’s the program itself:
What does that means? Briefly:
function to the outer world. It means the other modules can call this function. What does the “/0″ means? It means the “hello_world” function with 0 parameters. In Erlang, you can have different functions with the same name and different number of parameters.
To run your program you must start the Erlang shell first:
Try it, and understand it. Next time will be a little bit more complex!
Note: if you guess why by copying and pasting the code you get a lot of errors, that’s because SciTe, the editor I use, exports the code with fancy double quotes. That’s a good reason to not being lazy and write the program by yourself! Writing it’s a nice way of learning!