Erlang – Data Types

Erlang data types

Here we will discuss Data Types in Erlang. In any programming language, you need to use several variables to store various types of information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory to store the value associated with that variable.

You may like to store information of various data types like string, character, wide character, integer, floating point, Boolean, etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.

Built-in Data Types

Erlang offers a wide variety of built-in data types. Following is a list of data types which are defined in Erlang βˆ’

  • Number βˆ’ In Erlang, there are 2 types of numeric literals which are integers and floats.
  • Atom βˆ’ An atom is a literal, a constant with name. An atom is to be enclosed in single quotes (‘) if it does not begin with a lower-case letter or if it contains other characters than alphanumeric characters, underscore (_), or @.
  • Boolean βˆ’ Boolean data types in Erlang are the two reserved atoms: true and false.
  • Bit String βˆ’ A bit string is used to store an area of un-typed memory.
  • Tuple βˆ’ A tuple is a compound data type with a fixed number of terms. Each Term in the tuple is called as an element. The number of elements is said to be the size of the tuple.
  • Map βˆ’ A map is a compound data type with a variable number of key-value associations. Each key-value association in the map is called an association pair. The key and value parts of the pair are called elements. The number of association pairs is said to be the size of the map.
  • List βˆ’ A list is a compound data type with a variable number of terms. Each term in the list is called an element. The number of elements is said to be the length of the list.

Note βˆ’ You will be surprised to see that you cannot see the String type anywhere in the list above. That’s because there is no string data type exclusively defined in Erlang. But we will see how we can work with strings in a subsequent chapter.

Following are the examples of how each data type can be used. Again each data type will be discussed in detail in the ensuing chapters. This is just to get you acquainted with a brief description of the above-mentioned data types.

Number

An example of how the number data type can be used is shown in the following program. This program shows the addition of 2 Integers.

Example

-module(helloworld).
-export([start/0]).

start() ->
   io:fwrite("~w",[1+1]).

The output of the above program will be βˆ’

Output

2

Atom

Atoms should begin with a lowercase letter and can contain lowercase and uppercase characters, digits, underscore (_) and the β€œat” sign (@). We can also enclose an atom in single quotes.

An example of how the atom data type can be used is shown in the following program. In this program, we are creating an atom which is called atom1.

Example

-module(helloworld).
-export([start/0]).

start() ->
   io:fwrite(atom1).

The output of the above program will be βˆ’

Output

atom1

Boolean

An example of how the Boolean data type can be used is shown in the following program. This example does a comparison between 2 integers and prints the resultant Boolean to the console.

Example

-module(helloworld).
-export([start/0]).

start() ->
   io:fwrite(2 =< 3).

The output of the above program will be βˆ’

Output

true

Bit String

An example of how the Bit String data type can be used is shown in the following program. This program defines a Bit String consisting of 2 bits. The binary_to_list is an inbuilt function defined in Erlang which can be used to convert a Bit String to a list.

Example

-module(helloworld).
-export([start/0]).

start() ->
   Bin1 = <<10,20>>,
   X = binary_to_list(Bin1),
   io:fwrite("~w",[X]).

The output of the above program will be βˆ’

Output

[10,20]

Tuple

An example of how the Tuple data type can be used is shown in the following program.

Here we are defining a Tuple P which has 3 terms. The tuple_size is an inbuilt function defined in Erlang, which can be used to determine the size of the tuple.

Example

-module(helloworld). 
-export([start/0]). 

start() -> 
   P = {john,24,{june,25}} , 
   io:fwrite("~w",[tuple_size(P)]).

The output of the above program will be βˆ’

Output

3

Map

An example of how the Map data type can be used is shown in the following program.

Here we are defining a Map M1 which has 2 mappings. The map_size is an inbuilt function defined in Erlang, which can be used to determine the size of the map.

Example

-module(helloworld). 
-export([start/0]). 

start() -> 
   M1 = #{name=>john,age=>25}, 
   io:fwrite("~w",[map_size(M1)]).

The output of the above program will be βˆ’

Output

2

List

An example of how the List data type can be used is shown in the following program.

Here we are defining a List L which has 3 items. The length is an inbuilt function defined in Erlang, which can be used to determine the size of the list.

Example

-module(helloworld). 
-export([start/0]). 

start() -> 
   L = [10,20,30] , 
   io:fwrite("~w",[length(L)]).

The output of the above program will be βˆ’

Output

3

Next Topic : Click Here

This Post Has One Comment

Leave a Reply