NOGDUS

Articles, Tutorials, and other things. => General Programming => : Richard Marks March 30, 2009, 05:00:05 PM



: Getting Started with Erlang on Linux
: Richard Marks March 30, 2009, 05:00:05 PM
Getting Started with Erlang on Linux

First you need Erlang installed:

:
$ sudo apt-get install erlang

Now we need a place to organize our projects:

:
$ mkdir -p ~/Projects/erlang/learn/

Let's start with a simple hello world.

:
$ cd ~/Projects/erlang/learn/
$ touch hello.erl
$ gedit hello.erl &

Now enter this code into the hello.erl file in gedit:

:
%% hello.erl
%% A simple Hello World program by <Your Name Here>
-module(hello).
-export([hello/0]).
hello() -> "Hello World".

The %% lines are comments. They are ignored by Erlang.
-module(hello). says that the module is called hello.
Note the period at the end of the line.
The files in which you write the module must have the same name as the module but with the extension ".erl".
When we use a function in another module, we need to use the syntax: module_name:function_name(arguments)

-export([hello/0]). says that the module contains a function called hello which takes one argument.

hello() -> "Hello World". is our hello function body. We simply output the string "Hello World".

Let's compile it.
:
$ erl -compile hello

And run it:
:
$ erl hello.beam
Erlang (BEAM) emulator version 5.5.5 [source] [64-bit] [async-threads:0] [kernel-poll:false]

Eshell V5.5.5  (abort with ^G)
1> hello:hello().
"Hello World"
2> halt().
$

 8)


Sorry, the copyright must be in the template.
Please notify this forum's administrator that this site is missing the copyright message for SMF so they can rectify the situation. Display of copyright is a legal requirement. For more information on this please visit the Simple Machines website.