Get Started with the Erlang Programming Language

How to Evaluate Simple Statements in an Ericsson Erlang Shell

© Mark Alexander Bain

Aug 13, 2009
Get Started with the Erlang Programming Language, Mark Alexander Bain
The Ericsson Erlang programming language was developed for their mobile telephony switches and is now open source. It can handle thousands of parallel, distributed events

Most people will know that Ericsson (in partnership with Sony) are famous for producing a wide range of mobile (or cell) phones, and there are many people who will also know that much of the equipment used to build the mobile telephone networks (built by companies such as Vodafone) is also supplied by Ericsson. However, what most people don't know is that Ericsson have created their own programming language known as Erlang, and not only that: they've even made it open source and freely available for anyone to use.

Ericsson created Erlang for use with their telecommunication equipment (such as their mobile telephony switches) because they needed a language that is:

  • able to handle thousands of events simultaneously
  • able to handle distribution over many computers in different locations
  • able to handle parallel operations
  • above all, to be robust

They were unable to find any language that could do all of that to their satisfaction, and so Erlang was created in the Ericsson Computer Science Laboratory.

Obtaining and Installing Erlang

The Erlang programming language installation application can be downloaded from http://www.erlang.org. Once that's been done then the Erlang emulator can be accessed from the start menu or, if the path is set correctly, then the user just needs to type "erl" on the command prompt. They can then start entering Erlang Code.

Using the Erlang Shell

The Erlang shell (shown in figure 1 at the bottom of this article) is just like any Linux shell or the Windows command prompt:

  • the user types in statements on the command line
  • the interpreter evaluates the statements and displays a result on the screen

So, for example, the user can type in a string such as:

"Hello World".

And the text will be displayed on the screen, or the user could type in:

PI = 3.14159265358979.
R = 5.
C = 2 * PI * R.

This will display 31.4159265358979 on the screen (as can be seen in figure 2), and there a few things that should be noted from this:

  • every line must be terminated with a full stop (or period)
  • each line is evaluated immediately and the result displayed
  • previous lines can be accessed by using the drop down list box at the top left of the Erlang console

The next stage is to format the output.

Displaying Outputs in an Erlang Shell

Erlang will immediately evaluate any inputs and display the results of that evaluation. However, in a complex calculation these outputs may be all very confusing. Therefore, the programmer can produce a formatted output by using the io:format method, for example:

io:format ("The circumference of a circle of radius ~w is ~w~n", [R, C]).

The io:format takes two inputs:

  • a formatted string where:
    • ~w is a place holder for a variable
    • ~n is a new line
  • the variables to be used in the place holders contained in the formatted string

The output (as shown in figure 3) will be something like:

The circumference of a circle of radius 5 is 31.4159265358979

And so, in this way, the programmer can use the Erlang programming language to quickly analyse information and to display it in a useful format. However, all of the code looked at so far can only be used whilst the current Erlang shell is running. It is all lost as soon as the shell is closed. The next step, therefore, is to start creating code that can be reused, and that's discussed in Create and Use Erlang Modules and Functions.


The copyright of the article Get Started with the Erlang Programming Language in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish Get Started with the Erlang Programming Language in print or online must be granted by the author in writing.


Get Started with the Erlang Programming Language, Mark Alexander Bain
Figure 1: The Ericsson Erlang Shell, Mark Alexander Bain
Figure 2: Simple Erlang Evaluation, Mark Alexander Bain
Figure 3: Formatted Erlang Output, Mark Alexander Bain
 


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo