May
09
Posted on 09-05-2007
Filed Under (Erlang, Programming) by Federico Feroldi on 09-05-2007

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!

(1) Comment    Read More   
May
07
Posted on 07-05-2007
Filed Under (Erlang, Programming) by Federico Feroldi on 07-05-2007

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.

Numbers

Erlang supports integer and floating point numbers. Integer numbers can have almost unlimited size. Try this on the Erlang shell (erl):

2> 4534535*543636345*3445343*6564574523432. 55754546761558695184405188371500200

You can also have floating point numbers, but they have limited precision and size:

3> 3 / 2. 1.50000 4> 2 * 1.5. 3.00000

Strings

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

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

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

Lists are similar to tuples but they can contain an undefined amount of items of any type:

10> [1, 2, 3, {this, is, a, tuple}, anatom, [a, list]]. [1,2,3,{this,is,a,tuple},anatom,[a,list]]

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):

11> [Head | Tail] = [a, b, c, d]. [a,b,c,d] 12> Head. a 13> Tail. [b,c,d]

You can also create new lists by adding elements in front or at the end of an existing one:

14> [1, 2, 3 | Tail]. [1,2,3,b,c,d] 15> [Head|[1,2,3]]. [a,1,2,3]

Funs

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:

15> MyFun = fun(X) -> X * 2 end. #Fun 16> MyFun(5). 10 17> lists:map(MyFun, [1, 2, 3]). [2,4,6]

That’s all for now! I hope to have made less mistakes than the last time! :)

(7) Comments    Read More   
May
03
Posted on 03-05-2007
Filed Under (Erlang, Programming) by Federico Feroldi on 03-05-2007

Computer ProgrammingI’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! :D

(3) Comments    Read More   
Apr
26
Posted on 26-04-2007
Filed Under (Erlang, Programming) by Federico Feroldi on 26-04-2007

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:

-module(hello).
-export([hello_world/0]).

hello_world()->
    io:format("Hello World ~n").

What does that means? Briefly:

-module(hello).
Create a module for the following functions, think of it like a namespace declaration.

-export([hello_world/0]).
This tells the compiler that we want to export the

hello_world/0

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.

hello_world()->
Declare a new function named “hello_world” that accept no parameters, the body will follow the arrow.

    io:format(“Hello World ~n”).
This calls the “format” function from the module “io” that prints out the Hello World message. The “~n” characters prints the Carriage Return, the same as “/n” in other languages.

To run your program you must start the Erlang shell first:

$ erl Erlang (BEAM) emulator version 5.5.4 [async-threads:0] Eshell V5.5.4 (abort with ^G) 1> c(hello). {ok,hello} 2> hello:hello_world(). Hello World ok 3>

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! :)

(14) Comments    Read More