articles

Home / DeveloperSection / Articles / Erlang Programming – Data Types Short Description

Erlang Programming – Data Types Short Description

Tarun Kumar2514 19-Feb-2016

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)
1>

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.

8

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.

55.0

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
a

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:

-module(prog1).
-export([start/1]).

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).
22

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.

Erlang DATA TYPES:

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

Integer syntax: base#number

Examples:

1>  2#11001.
25
2>  8#763.
499
3>  16#FBA.
4026

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. 

Example:

-module(prog3).
-export([myfunction/2]).

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

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

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.


        Examples:
        1>    X = 10, Y = 4.
        4
        2>    Point = {X, Y}.
        {10, 4}
        3>    Point = {4, 5}.
        {4, 5}
        4>    {X, Y} = Point.
        {4, 5}
        5>    X.
        4
        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.

   Example:
       1>   [FirstValue | RestValues] = [A, B, C, D, E].
       [A, B, C, D, E]
       2>   FirstValue.
       A
       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.

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

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.

Updated 15-Dec-2017

Leave Comment

Comments

Liked By