Jump to content
xisto Community
Sign in to follow this  
Giniu

Mastering Erlang Part 1 – Introduction Or: Erlang, installation and basic usage

Recommended Posts

Mastering Erlang part 1 – Introduction

Or: Erlang Virtual Machine – what it is, installation, usage...

 

Warning! This tutorial is written for Erlang R10B4 – If you want use it with other release – consult with official documentation for possible incompatibilities and changes!

 

What is this all about? - or Background

 

Erlang is concurrent functional programming language developed in Ericsson Computer Science Laboratory, its development was started in 1987 after five years of researches on Lisp, Prolog and Parlog. Erlang was designed to be a very high level symbolic language with concurrency and error recovery. In 1990 Erlang was presented on ISS'90, one year later after Telecom conference Erlang was released to users with many new features like compiler and graphical interface. In 1993 Erlang was improved with distribution and after that Erlang started to grow even faster. Right now most popular Erlang implementation is Open Source Erlang which is distributed with OPT (which originally stands for Open Telecom Platform – a division of Ericsson which had to provide support for Erlang software) – a large collection of libraries for Erlang to do everything from compiling ASN.1 to providing a WWW server. Now when you use Erlang, you almost for sure use Erlang/OPT. As you mentioned main Erlang purpose is telecommunication, but it also have some success in for example 3D graphics world – Wings 3D – one of best Open Source modelling application is entirely written in Erlang. Current version of Open Source Erlang/OPT is 5.4.5 which is distributed in package called “Release 10-B, patched 4” or short “R10B4”. Release 11 is under high development and probably would see daylight soon.

 

Why I'm writing this tutorial? Because I think Erlang is good language that isn't well known and I think it can be easily changed when you would know what it can do and how easy it is when you know it's philosophy. This tutorial would be divided into few parts, since my time doesn't allow to write everything at once... so what parts should you expect? Here is my plan:

 

Introduction (who knows... you are reading this :D)

Erlang Sequential (about sequential programming in Erlang)

Erlang Concurrent (about concurrency in Erlang)

Erlang Everyday (about headers, macros and records in Erlang)

Erlang Robust (about error handling and time-outs in Erlang)

Erlang Distributed (about distribution, ports and security in Erlang)

Erlang C (about cooperation of C and Erlang)

Erlang Graphical (about using GUI, Open GL and SDL in Erlang)

Perhaps more if there would be any feedback OR I would have free time :P

 

Further reading: This tutorial is based upon original documentation, if you want to find out more than I wrote, look [there].

 

What you can suspect after this part? There is what it contains:

 

Some background – look above... I think this is enough for it :D

Terminology – I would explain terminology I would use later in this and other parts

Installation of Erlang – I would explain installation of Erlang (based on Linux, other *nix requires modifications)

Installation of ESDL – It would be needed later... (based on Linux)

Short note on Windows compilation – just that it is possible...

Note on Wings 3D – great Erlang application

Basic usage – I would explain basic usage of Erlang Virtual Machine and one example program.

 

So after this background I think it is time for start this show...

 

You say what? - or Terminology

 

Most important in Erlang is to understand its features, so let me explain most important things, they’re not sorted alphabetically, but by importance and terms:

 

Programming language – I think that you’ve already known what it is, but maybe not, so there is strict definition... A programming language is language which programmers use to create programs – this language is then translated to binary code of machine so users can run program. This is simplest definition because there is no need to give you more complex one.

 

Source code – Code of program/application written in specified programming language.

 

Compilation – An automated process in which source code is translated to binary code that can be executed.

 

Library – Pre made sets of functions that may be used in code to simplify creation of some effects. Usage of libraries best described by sentence: “Why need to re invite wheel?”.

 

Module – Part of program that can work separately but almost always are parts of larger programs – for example key listeners (they can wait for key press and when they spot it, they send which key was pressed to standard output – for example terminal or printer)

Virtual Machine – Environment that lets you to run programs independent on operating system and machine type, it is program that mediates between your program and operating system so you don't have to worry if you create program only for one operating system and it won’t work on others (in practice – you sill have to take care for it, but not as much as during creating application in language that runs without virtual machine).

 

Functional programming – Programming methodology that threads computations as evaluation of mathematical functions. Almost everything you write in Erlang is created from functions so code is easy to read and you don't have to know whole bunch of keywords – only understand what you want to do.

 

Distributed programming – Way of creating applications that run on many machines connected into network and send result to one or all of it. This is way of cooperation between parts of application that work even on second side of globe.

 

Sequential program – Program that would be executed line by line, in sequential programming you may use loops and tests, but this gives only limited freedom... Erlang of course can use sequential programming, but it isn't its main feature.

 

Concurrent program – Program that is created from modules, that runs separately on one or few machines and communicate with each others. Erlang is designed for concurrency so this is its power, it is designed for real-time system on which shut-down can be critical, so programs must run all the time (flight control, banks, telecommunication... there are many examples)... In Erlang this is used so good, so you can modify program (upgrade, recompile, anything...) while it is running and if it is designed well, it even wouldn't slow down while you would be doing that. High concurrency of Erlang allows you to create distributed application from stationary one almost without any modifications (only one line per module) so as you see this gives you large freedom.

 

I think that now you would know about what I'm talking about. If you know other programming language, you probably knew that already, but this tutorial is for all – those that doesn't have experience with programming in other languages and those that have it, so be patient if you read it and think that I write very basics. But let's install the beast...

 

Want to run it... - or Erlang Installation

 

First of all you have to download Erlang – If you are using Windows, you should download pre compiled binary, because you can have troubles getting source code to work – If you are using other platform – you should download source code and man pages – the HTML documentation is optional. There is address from where you can download it from [there]. If you downloaded pre compiled binary – you already have documentation and installation is very simple – you must run installation exe and everything would be done automatically... but you would get less performance... perhaps on Windows you even wouldn't get large speed up... So you know how to install Erlang on Windows... but what about other platforms? So now I would explain compilation of Erlang:

 

Unpack source code you downloaded (It is in .tar.gz file, so you can do for example (on Linux system):

 

tar -xzf file.tar.gz -C target_directory
Change directory to source files:

 

cd target_directory/otp_src_R10B-4
Set lang environment to C:

 

export LANG=C
Now you must think about options for compilation... first is: --enable-threads which is required on systems that use thread (line Linux and also most Unix systems...) the second is: --disable-hipe which disables HiPE (High Performance Erlang) since on some systems it may crash... try not disabling it, do test, if in test Erlang Segfaults (repeat test five times) return to this step and reconfigure it without HiPE...:

 

./configure --enable-threads –disable-hipe
Then you can compile it (if you’ve compiled it earlier, do make clean to remove old files):

 

make
and do some tests – you should get Erlang console:

 

./bin/erl
if everything went right, you may install Erlang:

 

make install
some more specific system infos are in Read Me file... Why I wrote this part of tutorial? Because they don't mention that on some Linux system when you have certain glibc and kernel, HiPE won't work and Erlang would segfault... while documentation says that it works on Linux and that it shouldn't be turned off... this probably would be fixed in some future releases...

 

So you installed Erlang... see if it works by typing in command line:

 

erl
i don't like text only things... - or ESDL installation

 

This will show you that Erlang is not only text-like environment for mathematical and old-school applications, we would install ESDL and run some test... Of course you must have Erlang running... and there is problem for Windows users – you would have troubles compiling ESDL – so its creators made for them pre compiled binary in one exe so like Erlang it can be installed very fast, but you wouldn't get any speed up that compilation normally gives. So for other operating system users – you should download source code and compile it yourself... you can download ESDL from [sourceForge]. There is info how to compile ESDL on Linux (Mac OS users should follow Read Me file, I cannot say anything about Mac, since I'm on Linux only):

 

Just like above, unpack downloaded sources somewhere onto hard disk and change current directory to it...

 

You should have SDL and OpenGL (also full GLU) to compile it... sometimes there are troubles locating GLU – some nVidia drivers for example keeps those libaries in non-standard place... you should locate your libGLU.so and libglut.so – they should be in /usr/lib (there is where all Erlang applications searches for it) but on my system they was in /usr/X11R6/lib and ESDL wasn't able to compile... if same or similar would happend to you, those symlinks fixed everything:

 

ln -s /usr/X11R6/lib/libGLU.so /usr/lib/ ln -s /usr/X11R6/lib/libglut.so /usr/lib/ ldconfig
now you can build ESDL – do:

 

make
and now there is time to do some tests :P, in shell type:

 

cd testerl -pa ../ebin
you should have Erlang shell opened and it should say something like:

 

Erlang (BEAM) emulator version 5.4.5 [source] [threads:0]

 

Eshell V5.4.5 (abort with ^G)

 

so time to run test... type one or all of following (note the dot at the end, after every line press enter...):

 

testsprite:go().testgl:go().  erldemo:go().testaudio:go(). testjoy:go().
If everything work – you may be happy and install everything:

 

make install
Now important thing about which many forgets... you should add path where ESDL is installed to your environment variables... In most cases ESDL would be installed in /usr/local/lib/erlang/lib/esdl-0.94.1025/ - if somewhere else, just modify method I would write there (remember that it is for Linux) – edit your /etc/profile file and add to it line:

 

export ESDL_PATH="/usr/local/lib/erlang/lib/esdl-0.94.1025/"
- After next restart you should have this added to your environment variables. If you don't want to restart now, add it manually, execute it in shell when you would need it...

 

So you have now ESDL installed, and you saw that it can display graphics rather easy... Graphics in 2D and 3D would be discussed later...

 

I want feel the speed but I'm on Windows... - or Some information

 

So – Linux and Mac OS users can compile Erlang and ESDL and they gain performance... what can Windows users do about it? Besides... pre compiled versions are also somewhat compiled... how? So let me tell you – you must have a lot of software and patience that you probably wouldn't have – but if yes, I don't know what you are doing on Windows, when you can at least dual-boot into Windows/Linux... any way – You should have:

 

CygWin (something like Linux emulator) with Make and other system tools

SDL (Libraries similar to DirectX)

MinGW (a C compiler)

Sources like for Linux...

... and a lot of free time.

 

When you install. Everything – your Erlang/ESDL should run faster than pre compiled versions, but you would for sure spot a lot of troubles compiling – if you want to have fast Erlang, get dual-boot with Linux...

 

Erlang example: Wings 3D... - or Should users install Erlang?

 

This is important question – if users should install Erlang and ESDL to watch/play/use product... if it would be so, Erlang would be forgotten very fast because users must have easier way of installing application so they won't get mad with it... great example is Wings 3D – pure Erlang/ESDL application that slowly reaches final version... I suggest to download it so you can see that Erlang is powerful and to feel the power under your fingers... of course – Wings runs from pre compiled binary and you don't need virtual machine installed, since it contains small version of it compiled and you don't even notice that someone have to install so many things to create it for you... :(

 

What can I do with this virtual machine? - or Basic usage

 

First of all you must start your Virtual Machine typing erl on *nix or running it from shortcut under Windows – You should see:

 

Erlang (BEAM) emulator version 5.4.5 [source] [threads:0]

 

Eshell V5.4.5 (abort with ^G)

1>

 

First thing – I am able to run it... how can I quit it? So... to quit Erlang you must press Ctrl+C, then you should get:

 

BREAK: (a)bort ©ontinue (p)roc info (i)nfo (l)oaded

(v)ersion (k)ill (D)b-tables (d)istribution

 

and when you press "a" and then enter and Erlang would quit. So how this all works? First off all take a look at those numbers – these are line numbers – they doesn't refer to source code line numbers, since you don't write this code in Virtual Machine, but in any other text editor. Let's make something, type:

 

3+4.
(note dot at end, after this press enter) You get 7... so this can be used as calculator... let's try something more complex:

 

(2*11)/(3+4).
As you see Erlang can do more... this is 22/7 which was used long time ago as PI number... but how can you get more exact PI? You have to type every time 3.14159? No - you can use math module – it has function pi which returns this value – try it:

 

math:pi().
Now try other function from module math:

 

math:log(7.38909).
You would get 2... if you know some elementary math, you would know why... As you see you can call any function from any module by typing:

 

module:function(arguments).

 

This is main Erlang calling structure which executes functions. So now we would define some functions – it would be most famous Erlang example – calculating factorial of natuarals... we would call our module that would contain this function – mymath – so create file mymath.erl (note that you must name your file same as module – this is important – without it Erlang would return error and you would search it some time...) - in this file type something like this:

 

-module(mymath).-export([factorial/1]).factorial(1) ->        1;factorial(N) ->        N * factorial(N-1).
We would extend this example later, but now it is enough – save it so you wouldn't forget and let me explain it... first of all you named your module, remember to keep your module name same as file name but without .erl at end. Second you told Erlang that in this module is one function named factorial, containing one argument and only this function can be called from within this module (I wouldn't explain function now, since this one is easy... note the “;” at end of first function instance – this tells Erlang “this is not end of this function”). Let's get back to Erlang virtual machine (would call this eshell) and compile your module, do:

 

c(mymath).
You should get:

 

{ok, mymath}
(Remember to place file mymath.erl in directory from which you started Erlang so it can locate your file). Now it is time to see what we created :D Type:

 

mymath:factorial(7).
And as you see it is working... now let me show you how powerful function you wrote:

 

mymath:factorial(10000).
How about that? If you have enough RAM, you get result very fast :D This is of course externally mathematical example, but at the beginning you would meet almost only mathematical examples so you would get used to functional philosophy of Erlang.

 

And I think this is enough for absolute beginning... with this series you would be able to learn Erlang step by step... soon you would be able to proof skills you gained there in project I'm going to start on AntiLost... You also would be able to take it as examples of how to make a game in Erlang since it would be highly documented...

 

And this is end for now... Hope you liked it and would wait for next part, when we start playing with sequential way of Erlang. See Ya all next time...

 

Giniu

 

--------------------------

Thanks for corection of this tutorial goes like usual to Nelle... BIG THANKS...

Share this post


Link to post
Share on other sites

Oh... I forgot about thing that don't suit into other parts... the comments in Erlang starts with "%", like in our example:

%this module is my own addiction to math module-module(mymath).-export([factorial/1]).factorial(1) ->       1;factorial(N) ->       N * factorial(N-1).
hope I won't forgot anything else... keep up - I'm finishing second part now :(

Share this post


Link to post
Share on other sites

Finishing part two - should be there in next two or three days... while this I realized that you might wan't to get some editor that is capable to highlight erlang stntax... if you are *nix user you probably already have it, it is:

vim
emacs

they are hardcore standards for programing... if you are using windows, mac or just like gui also in unix I can recomend you (with hand on my hearth):

jEdit - from [it's homepage] - it has highlighting for more than 130 languages and (with plugins) builded in anything you want - for example independent CVS interface... it is written in java so requires it to run...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.