English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In any programming language, you need to use multiple variables to store various types of information. A variable is just a reserved memory location used to store values. This means that when you create a variable, you will reserve some space in memory to store the value associated with that variable.
You may like to store information of various data types, such as strings, characters, wide characters, integers, floating-point numbers, booleans, etc. The operating system allocates memory based on the data type of the variable and determines what can be stored in the reserved memory.
Erlang provides a variety of built-in data types. The following is a list of data types defined in Erlang-
Number(Numbers) - In Erlang, there are two types of number literals: integers and floating-point numbers.
Atom(AtomAn atom is a letter, which is a named constant. If an atom does not start with a lowercase letter or contains alphanumeric characters, underscores (_), or other characters besides @, it should be enclosed in single quotes (').
Boolean(BooleanThe boolean data type in Erlang is two reserved atoms: true and false.
Bit String (Bit String)−Use bit strings to store untyped memory areas.
Tuple(Tuple)−Tuple is a compound data type with a fixed number of items. Each term in the tuple is called an element. The number of elements is called the size of the tuple.
Map(Mapping)−Map is a compound data type with a variable number of keys-Value associated compound data type. 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 called the size of the map.
List(List)−A list is a compound data type with a variable number of items. Each term in the list is called an element. The number of elements is called the length of the list.
Note−You will be surprised to find that the String type is not visible anywhere in the above list. This is because there is no special string data type defined in Erlang. However, in the next chapter, we will see how to use strings.
The following are examples of how to use each data type. Similarly, each data type will be discussed in detail in subsequent chapters. This is just to make you familiar with a brief introduction to the above data types.
The following program demonstrates an example of using the number data type. This program shows2to add two integers.
-module(helloworld). -export([start/0]). start(), -> io:fwrite("~w",[1+1]).
The output of the above program will be
2
An atom should start with a lowercase letter and can contain lowercase and uppercase letters, numbers, and underscores.(_)and the "at" symbol(@). We can also enclose atoms in single quotes.
The following program demonstrates an example of using the atom data type. In this program, we are creating an atom named atom1of the atom.
-module(helloworld). -export([start/0]). start(), -> io:fwrite(atom1).
The output of the above program will be
atom1
The following program demonstrates an example of using the boolean data type. This example compares2to compare between two integers, and output the boolean result to the console.
-module(helloworld). -export([start/0]). start(), -> io:fwrite(2 =< 3).
The output of the above program will be-
Output
true
The following program demonstrates an example of using the bit string data type. This program defines a bit string composed of2bit strings composed of bits.binary_to_listis a built-in function defined in Erlang, which can be used to convert a binary string to a list.
-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:
[10,20]
The following program demonstrates an example of using the Tuple data type.
Here, we define a tuple with3The tuple P. The function tuple_size is a built-in function defined in Erlang, which can be used to determine the size of a tuple.
-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-
3
The following program shows an example of how to use the Map data type.
Here, we define a map with2map of Map M1. The map_size is a built-in function defined in Erlang, which can be used to determine the size of a map.
-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:
2
The following program shows an example of how to use the List data type.
Here, we define aList L, it contains3project. The built-in length function defined in Erlang can be used to determine the size of a list.
-module(helloworld). -export([start/0]). start(), -> L = [10,20,30], io:fwrite("~w",[length(L)]).
The output of the above program will be:
3