Tutorial 11
Tutorial 11
TUTORIAL 11
Erlang Intro
Erlang should be used to develop your application, if you have the following requirements −
● Run ` sudo apt update ` and ` sudo apt upgrade ` to update the list of package repositories
and installed packages.
● Run ‘ sudo apt install erlang ’ to download and install the latest version of Erlang on our
Ubuntu 20.04 desktop or server system using the APT package manager.
● Run ‘ erl ‘ To test Erlang programs, or to maintain, the Erlang shell is ideal. This is automatically installed
with Erlang and can be installed from the terminal by the command.
a. Although we don’t need to add the repository to get the packages for installing Erlang on Ubuntu 20.04,
however, the version will not be the latest. Therefore, to get the latest version of this programming language,
here we are adding Erlang’s official repository, manually :
echo "deb [signed-by=/usr/share/keyrings/erlang.gpg] https://packages.erlang-
solutions.com/ubuntu $(lsb_release -cs) contrib" | sudo tee
/etc/apt/sources.list.d/erlang.list
a. To rebuild the APT package index cache : ` sudo apt update `
b. After adding the repository, we can download and install the latest version of Erlang on our Ubuntu 20.04
desktop or server system using the APT package manager : ‘ sudo apt install erlang ‘
c. To test Erlang shell : ‘ erl ‘
a. Save the file using Ctrl+O, hit the Enter key, and Ctrl+X to exit the text editor
Note : with using ‘-s init stop’ Erlang environment will shutdown once the program is finished, as
shown if it is not used after finishing the program still Erlang environment is running and need to
quit from it
-export([FunctionName1/FunctionArity1,.,FunctionNameN/FunctionArityN])
Import :
● The import attribute is used to import functions from another module to use it as local.
-module(helloworld).
-author("XXX").
-version("1.0").
-import(io,[fwrite/1]).
-export([start/0]).
io:fwrite("~w~n",[Z]). function.
➔ One function can be called inside another. Here the
start() -> add function is calling from the start function.
add(5,6). ➔ Output: 11
functions can be defined with zero or more parameters. Function overloading is also
possible, wherein you can define a function with the same name multiple times, as long as
they have different number of parameters.
-module(helloworld).
-export([add/2,add/3,start/0]).
add(X,Y) ->
Z = X+Y,
io:fwrite("~w~n",[Z]).
add(X,Y,Z) ->
A = X+Y+Z, Output: 11
io:fwrite("~w~n",[A]). 17
start() ->
add(5,6),
add(5,6,6).
COMP 348 Principles of Programming Languages 11
Built-in Data Types
❏ Number : In Erlang, there are 2 types of numeric literals which are integers and floats.
-module(helloworld).
-export([start/0]).
-module(helloworld).
-export([start/0]).
start() -> Note: Boolean data types in Erlang are the two
io:fwrite(atom1), reserved atoms: true and false.
io:fwrite("~n"),
io:fwrite(atom_1), -module(helloworld).
io:fwrite("~n"), -export([start/0]).
io:fwrite('atom 1'),
start() ->
io:fwrite("~n"). io:fwrite(2 =< 3). Output: true
Output: atom1
atom_1
atom 1
COMP 348 Principles of Programming Languages 14
❏ String : A String literal is constructed in Erlang by enclosing the string text in
quotations. Strings in Erlang need to be constructed using the double quotation marks
such as “Hello World”.
-module(helloworld).
-module(helloworld). -import(string,[left/2]).
-export([start/0]). -export([start/0]).
-module(helloworld).
-import(lists,[append/2]).
-export([start/0]).
Output: [1,2,3,4,5]
start() ->
Lst1 = [1,2,3],
Lst2 = append(Lst1,[4,5]),
io:fwrite("~w~n",[Lst2]),
● delete(element,List1) : Deletes an element from the list and returns a new list.
● droplast(List1) :Drops the last element of a List. The list should be non-empty,
otherwise the function will crash with a function_clause.
● duplicate(N,Elem) :Returns a list which contains N copies of the term Elem.
(Elem − The element which needs to be duplicated in the list)
● last(lst1) :Returns the last element of the list.
● max(lst1) :Returns the element of the list which has the maximum value.
● min(lst1) :Returns the element of the list which has the minimum value.
● nth(N,List) :Returns the Nth element of List.
● reverse(lst) : Reverses a list of elements.
● sort(lst) : Sorts a list of elements.
-module(helloworld).
-export([start/0]). ● is_tuple(tuple):determines if the term
provided is indeed a tuple or not and
start() ->
P = {john,24,{june,25}} , return true or false.
io:fwrite("~w",[tuple_size(P)]). ● list_to_tuple(list):converts a list to a tuple.
● tuple_to_list(tuple):converts a tuple to a
list.
Output: 3
-module(helloworld).
-export([start/0]).
-record(person, {name = "", id}).
start() ->
P = #person{name = "John",id = 1},
P1 = P#person{name = "Dan"}, Output: 1
io:fwrite("~p~n",[P1#person.id]), “Dan”
io:fwrite("~p~n",[P1#person.name]).
https://www.erlang-solutions.com/downloads/
https://www.tutorialspoint.com/erlang/erlang_data_types.htm