This is a blog about me, Seinfeld, okonomiyaki, japanese toilet seats, and other things of interest

Monday, July 23, 2007

Erlang

So I've finally written my first Erlang program:


diaspora:~/projects/erlang lars$ cat test.erl
-module(test).
-export([fac/1]).

fac(0) -> 1;
fac(N) -> N * fac(N - 1).
diaspora:~/projects/erlang lars$ erl
1> c(test).
{ok,test}
2> test:fac(5).
120
3>

I used to think that the name Erlang came from the words Ericsson + language, but the language is named after the father of queue theory, Agner Krarup Erlang. It looks a lot like Prolog, and I have a feeling that has to with that the first version of Erlang was implemented in Prolog. As you can see, functions are defined as a collection of clauses where the clause to execute is found using pattern matching. Here's another classic example:
sum([H|T]) -> H + sum(T);
sum([]) -> 0.

The function sum returns 0 if it is called with an empty list ([]), and if the list is non empty the head of the list (H) is added to the sum of the rest (tail) of the list (T). This is of course equivalent to car and cdr in Lisp.

0 Comments:

Post a Comment

<< Home