<< Our DL380 G5 Servers Turn Two Years Old | Home | Stack Overflow: Simple Data Dump Download Archive >>

My first Erlang program: Hello World!

Also, 'Hello Erlang!' and 'Hello YAWS!'
Bookmark and Share

Erlang LogoSigh...It's not every day that a programmer tries out a new language, but that is exactly what I did this evening! 

A few months ago some of the hype around Erlang caught my eye.  Specifically, Erlang's approach to parallel processing and multi-threading are interesting to me because 1) multiple cores are becoming the norm and increasing in number, 2) the existing approaches by popular platforms and architectures, including my beloved Java, may not scale out as well with more cores, and 3) streaming media servers, which butter my bread, seem like a great application for Erlang.

The first thing I did was download the source to Erlang, compile it, and then install.  Tim Dysinger has a wonderfully concise blog post (Compiling Erlang on Mac OS X Leopard from Scratch) which I have reproduced below slight altered to my way of doing things:
cd /tmp
wget http://www.erlang.org/download/otp_src_R12B-5.tar.gz
tar xzf
otp_src_R12B-5.tar.gz
cd otp_src_R12B-5
./configure --enable-hipe --enable-smp-support --enable-threads
make
sudo make install
cd ~
And with that Erlang is ready to go.  Next I found a great "Hello World!" blog post from Edward Garson's blog: The *Real* Erlang "Hello, World!".  Garson has an opinion on what should be a learner's first Erlang program, that it should use the Actor Model, a core approach of the language.  So, the code:
--module(hello).
-export([start/0]).

start() ->
    spawn(fun() -> loop() end).

loop() ->
    receive
        hello ->
        io:format("Hello, World!~n"),
        loop();

    goodbye ->
        ok

end.
With a quick copy from Garson's blog into a new text file, hello.erg, in my home directory, I was ready to compile and run in the shell:
manoa:~ stu$ erl
Erlang (BEAM) emulator version 5.6.5 [source] [smp:2] [async-threads:0]
[hipe] [kernel-poll:false]

Eshell V5.6.5 (abort with ^G)
1> c(hello).
{ok,hello}
2>
2>
2> Pid = hello:start().
<0.37.0>
3> Pid ! hello.
Hello, World!
hello
4> Pid ! goodbye.
goodbye
5> halt()
5> .
manoa:~ stu$
Fantastic!  Afterward, I went through the official "Getting Started with Erlang" page at erlang.org, which touches lightly on deeper topics, such as the Erlang shell, job control, distributed Erlang, and debugging. 

Also on my googles this evening I found criticisms of Erlang, which is healthy to see in the community.  An example would be Damien Katz's (creator of CouchDB, which is written in Erlang) "What Sucks About Erlang".  It was good to see the ugly side of Erlang up front before I become too enamored. 

Next steps?  I need to go through the Hello World and Getting Started pages again, write some little programs, and they try to do something with YAWS, a web server written in Erlang.  I have a project in mind already which is one one hand very motivating, but on the other probably means I will skip or miss some basic concepts and learn them the hard way.

What is the project you ask? Well, I'd like to write an FLV pseudo-streaming module for YAWS and potentially an accompanying FLV indexing tool. I've written one from scratch in Java for xtendx, and there are plenty of other implementations in PHP, C, etc for other web servers.

Hello Erlang!
Tags :


Re: My first Erlang program: Hello World!

Congratulations on trying out Erlang. I'll be interested to follow your experience if you decide to dig deeper into it. Particularly interesting to note that the basic 'Hello World' program incorporates actors - I've just blogged about the role they can play in parallel programming.

Add a comment Send a TrackBack