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

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Reddit
  • Technorati
  • YahooMyWeb
    Read More   

Comments

Planet Erlang on 16 April, 2007 at 2:48 pm #

Your first Erlang program in less than 1 minute! (Lesson 1)


[…] You’re first Erlang program in less than 1 minute! (Lesson 1) Posted in Erlang, Programming by admin on the April 26th, 2007 […]


Raju on 27 April, 2007 at 12:30 am #

Hey! Great article. I have been thinking of getting into Erlang for some time now, and now, am just waiting for Armstrong’s book to be published. I did enjoy your article, and it gives me a way to get my hands dirty while I wait. I did have a comment, and it has nothing to do with the article, but you have a sidebar panel on your blog, which I believe should be “Categories”, and not “Categoires” (unless I am missing something). Again, great article, looking forward to your “advanced” posts…

- Raju


한RSS on 27 April, 2007 at 3:36 am #

[…] } Federico Feroldi: You’re first Erlang program in less than 1 minute! (Lesson 1) 2007-04-26 20:50 작성 Begin to learn Erlang programming starting from a small Erlang program. […]


Federico Feroldi on 27 April, 2007 at 12:43 pm #

Thanks for spotting the typo, Raju. Fixed!


Planet Erlang - Trapexit on 27 April, 2007 at 5:02 pm #

[…] ErlangFederico Feroldi: Your first Erlang program in less than 1 minute! (Lesson 1)Begin to learn Erlang programming starting from a small Erlang program. It just takes you less than […]


AlreadyDone on 27 April, 2007 at 8:47 pm #

Federico Feroldi: Your first Erlang program in less than 1 minute! (Lesson 1) Process-one: Web development in Erlang: SlideAware experience Federico Feroldi: Erlang helps Rails to scale Philip Robinson: Erlang Macro Processor (v2), Part V Philip Robinson: Erlang Macro Processor (v2), Part IV


fooflerm on 28 April, 2007 at 7:03 am #

There are two things that make erlang fun - and by fun I really mean a complete pain in my ass. First is the notion of functional programing. Second is the “concurrent” aspect. Each of these ideas have precious little “foo made easy” docs. Both these are gonna cause offense to Object Nazis; however, both these ideas seems to have powerful traction with the notion of business applications. It some sense, I’d say this is Cobol 2.0 - the bastard child.

Seriously, these functional languages make way more sense in the business world than any object oriented concepts. I’m so glad that none seems to acknowledge the functional alternative. It validatea the idea that my professor really did have his head where the sun don’t shine.

I wish there was more talk about applying erlang to more typical business applications. For example, get a request, log the input, do the action, log the response, send the response.

Erlang does have a hump on its syntax. I would be real hot to see some merging of Haskel, R and Erlang - of course that is both syntax and libraries. As a user, I would rather stuff just work. I don’t wanna learn more syntax, but perhaps I’m mal adjusted.


Michael on 30 April, 2007 at 8:20 am #

Hey, thanks for the article - nice to see more entry-level articles about Erlang :)
One thing I spotted though - in your instructions to run the code, you have:
hello:helloworld().
I think you’re missing an underscore in there i.e.:
hello:hello_world().

thanks again for the article, I’m looking forward to seeing more :)

Michael


Nala Regeork on 30 April, 2007 at 7:22 pm #

Funny yes indeed lots of errors but simple enough to deal with even for someone even running Erlang for the first time to deal with ;D

I tried it in Eclipse ErlIde and got the errors immediately but ran it via shell after creating a new text file.
You did know you specified the command line incorrectly, right or was that also part of the test? ;D


Federico Feroldi on 1 May, 2007 at 3:33 am #

Michael, Nala: thanks for having spotted these issues, I’m now sure about the law that says “all programs have bugs”, even the Hello World programs! Thanks indeed! :)


Quartz/Wally on 1 May, 2007 at 1:02 pm #

Toh guarda chi si becca con linkedin… ecco dove eri finito! Ci hai mica uno straccio di mail? :)
Ora mi incuriosisci con l’erlang, e dire che dovevo studiarmi già apl e lisp…

Saludos

Piero


Nala Regeork on 1 May, 2007 at 3:45 pm #

Well enjoyed it, it was a good first program to try.
It did however take me more then a minute but that was because I had to install Erlang first. Ain’t Linux great if you have your package manager setup right you can read this article, install some of the required software, and test it out all in a matter of minutes. Let’s see someone get that done in less then ten minutes under windows ;D
I definitely am enthused about this Erlang I was considering Lisp but think Erlang is a better choice.


Stifflog - Having Fun With Erlang on 3 May, 2007 at 2:00 pm #

Post a Comment
Name:
Email:
Website:
Comments: