As we know that every operating system have a command interpreter or shell, (for ex: Linux or UNIX O/S have shells and Windows has command prompt), same as Erlang uses its own shell to compile Erlang code. Here we will see some basic concepts of Erlang like how to start the Erlang shell and execute some Erlang code. At first we will install Erlang program- for that open terminal and type this code:

sudo apt-get install erlang erlang-doc

After completing the installation process our Erlang program is now installed. Now, to start Erlang shell type erl in command prompt, after that Erlang shell will be started look like this:

% erl
Erlang 18 (erts-5.9.1) [source] [64-bit] [smp:8:8] [async-threads:10] [kernel-poll:false]

Eshell V7.2  (abort with ^G)

In Erlang shell 1> represents the line number, now type “5 + 3” in the Erlang shell and after finishing your code enter dot(.) which will indicate that the statement is complete and press enter, here notice one thing after pressing enter it will return the result 8 of 5 + 3. Like this:

1>  5 + 3.


Erlang does not care if we enter integer or floating point numbers, means if we are dealing with arithmetic calculation then both type are supported. Example to calculate complex calculations like this:

2>  (23 + 43) * 5 / 6.


To stop the Erlang shell Press Ctrl with C key on keyboard, Erlang shell will display the following output:

BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution

Type a and press enter to abort the Erlang shell. We can also stop the Erlang shell/system by entering halt(): command in the Erlang shell.

Now, if you want to create Erlang program then we need to create a file with .erl extension (ex: prog1.erl) using suitable text editor, and always remember one thing that save your Erlang program file in the location where you start the Erlang program. write this code in your erlang file:


start(N) -> N + 10.

After saving the file, file name will be same as the module name. So in this case our file name will be prog1.erl. and if you are using functions and want to access those functions to outside the program or module then we pass it in the export() function. The variables we use in the erlang program will always be in Capital letter (for ex: N, Number, Name, Age). To compile the Erlang program, enter code like this in erlang shell:

2> c(prog1).
{ok, prog1}

The {ok, prog1} means that the compilation is OK.

To run the program we use module name and call the function with the argument,

like this:

3> prog1:start(12).

Like other programming languages Erlang also uses data-types like Numbers, Strings, Atoms, Tuples, Lists, etc. In Erlang programming there is no need to define the data type when we create a variable.


NUMBERSErlang defines numbers in integers and floats. We can write Integers in any base from 2 to 36.

Integer syntax: base#number


1>  2#11001.
2>  8#763.
3>  16#FBA.

And we can write ASCII value of any characters like $c, for example, $A is 65, and $\n is 10.

ATOMS: Atoms are simply names, nothing else, and starts with a small letter, digits and underscores and any string enclosed in single quotes (for ex: sum, ‘sub’). Atoms are not like the other variables who have a value. 



myfunction(N, add) -> N + 10;
myfunction(N, sub) -> N – 10;

     To compile:
     1>   c(prog3)
     {ok, prog3}
     To Execute:
     2>   prog3:myfunction(25, add).
     3>   prog3:myfunction(25, sub).

TUPLES: Tuples are the comma-separated sequence of values that are enclosed in

braces, for ex: {23, xyz, “Hello”}.  The number of elements are the size of the tuple.

Tuples are used to organize data, it provides the facility to group many terms

together when we are clear how many there are.

        1>    X = 10, Y = 4.
        2>    Point = {X, Y}.
        {10, 4}
        3>    Point = {4, 5}.
        {4, 5}
        4>    {X, Y} = Point.
        {4, 5}
        5>    X.
        6>   {X, _} = Point.
        {4, 5}  

LISTS: Lists are also comma-separated sequence of values that are enclosed in

brackets, for ex: [23, xyz, “Hello”]. The number of elements are the length of list. A list

is either the empty list [] or consisting a head and a tail. Tail can also be a list.

       1>   [FirstValue | RestValues] = [A, B, C, D, E].
       [A, B, C, D, E]
       2>   FirstValue.
       3>   RestValues.
       [B, C, D, E]

In the above code we are using separator “|”, it will separate the first element from the list. 

Boolean algebra and Comparison Operators: like other programming languages

Erlang also uses true/false operators for comparisons. Erlang has ways to compare

items, using Boolean algebra: the Boolean operators like and, or, xor, etc will always

evaluate arguments on both sides of the operator.

       1>    true and false.
       2>    true or false.
       3>    true xor false.
       4>    not false.
       5>    not true.
       6>    not (true and true).

Erlang has other special operators for comparisons, usually all languages uses == (is

equal), != (not equal) operator to test equality. Erlang also uses =:= (is equal), =/= (not

equal), because Erlang don’t care about floats and integer values in arithmetic. For

example: if we are comparing integer and float values then ==, =/= operators will not

return correct result so that, Erlang provides =:= (is equals), =/= (not equal) operators.

Notes: Erlang doesn’t treat true or false as Boolean values because the term true

and false are the atoms.

[In Erlang this is the priority order in comparison: number < atom < reference <

fun < port < pid < tuple < list < bit string]

Reference : it is a term that is unique in an Erlang runtime system, and it can be

created by using make_ref/0.

Fun : it is a functional object which is used as anonymous function.

Port : it is an identifier which is used to identify the Erlang port.

Pid : it is a process identifier means it identifies a process.

Tuple : it is a compound data type which has fixed number of elements.

List : it is also a compound data type with a number of elements.

Bit string : it is used to store an area of untyped memory. It consist of a number

of bits that are evenly divisible by eight, which is known as binaries.
  Modified On Dec-15-2017 02:42:56 AM

Leave Comment