Erlang – Records

Erlang – Records ”; Previous Next Erlang has the extra facility to create records. These records consist of fields. For example, you can define a personal record which has 2 fields, one is the id and the other is the name field. In Erlang, you can then create various instances of this record to define multiple people with various names and id’s. Let’s explore how we can work with records. Creating a Record A record is created using the Record Identifier. In this record identifier, you specify the various fields which constitute the record. The general syntax and example are given below. Syntax record(recordname , {Field1,Field2 ..Fieldn}) Parameters recordname − This is the name given to the record. Field1,Field2 ..Fieldn − These are the list of various fields which constitute the record. Return Value None For example -module(helloworld). -export([start/0]). -record(person, {name = “”, id}). start() -> P = #person{name=”John”,id = 1}. The above example shows the definition of a record with 2 fields, one is the id and the other is the name. Also, a record is constructed in the following way − Syntax #recordname {fieldName1 = value1, fieldName2 = value2 .. fieldNameN = valueN} Where in you assign values to the respective fields when an instance of the record is defined. Accessing a Value of the Record To access the fields and values of a particular record, the following syntax should be used. Syntax #recordname.Fieldname Parameters recordname − This is the name given to the record. Fieldname − This is the name of the field which needs to be accessed. Return Value The value assigned to the field. For example Live Demo -module(helloworld). -export([start/0]). -record(person, {name = “”, id}). start() -> P = #person{name = “John”,id = 1}, io:fwrite(“~p~n”,[P#person.id]), io:fwrite(“~p~n”,[P#person.name]). Output The output of the above program is as follows. 1 “John” Updating a Value of the Record The updation of a record value is done by changing the value to a particular field and then assigning the record to a new variable name. The general syntax and example is given below. Syntax #recordname.Fieldname = newvalue Parameters recordname − This is the name given to the record. Fieldname − This is the name of the field which needs to be accessed. newvalue − This is the new value which needs to be assigned to the field. Return Value The new record with the new values assigned to the fields. For example Live Demo -module(helloworld). -export([start/0]). -record(person, {name = “”, id}). start() -> P = #person{name = “John”,id = 1}, P1 = P#person{name = “Dan”}, io:fwrite(“~p~n”,[P1#person.id]), io:fwrite(“~p~n”,[P1#person.name]). Output The output of the above program is as follows − 1 “Dan” Nested Records Erlang also has the facility to have nested records. The following example shows how these nested records can be created. For example Live Demo -module(helloworld). -export([start/0]). -record(person, {name = “”, address}). -record(employee, {person, id}). start() -> P = #employee{person = #person{name = “John”,address = “A”},id = 1}, io:fwrite(“~p~n”,[P#employee.id]). In the above example the following things need to be noted − We are first creating a person’s record which has the field values of name and address. We then define an employee record which has the person as a field and an additional field called id. Output The output of the above program is as follows. 1 Print Page Previous Next Advertisements ”;

Erlang – Guards

Erlang – Guards ”; Previous Next Guards are constructs that we can use to increase the power of pattern matching. Using guards, we can perform simple tests and comparisons on the variables in a pattern. The general syntax of the guard statement is as follows − function(parameter) when condition -> Where, Function(parameter) − This is the function declaration that is used in the guard condition. Parameter − Generally the guard condition is based on the parameter. Condition − The condition which should be evaluated to see if the function should be executed or not. The when statement must be used when a guard condition is specified. Let’s look at a quick example of how guards can be used − Example Live Demo -module(helloworld). -export([display/1,start/0]). display(N) when N > 10 -> io:fwrite(“greater then 10”); display(N) when N < 10 -> io:fwrite(“Less than 10”). start() -> display(11). The following things need to be noted about the above example − The display function is defined along with a guard. The first display declaration has a guard of when the parameter N is greater than 10. So if the parameter is greater than 10, that function will be called. The display function is defined again, but this time with the guard of less than 10. In this way, you can define the same function multiple times, each with a separate guard condition. The output of the above program will be as follows − Output greater than 10 The guard conditions can also be used for if else and case statements. Let’s see how we can carry out the guard operations on these statements. Guards for ‘if’ Statements Guards can also be used for if statements so that the series of statements executed is based on the guard condition. Let’s see how we can achieve this. Example Live Demo -module(helloworld). -export([start/0]). start() -> N = 9, if N > 10 -> io:fwrite(“N is greater than 10”); true -> io:fwrite(“N is less than 10”) end. The following things need to be noted about the above example − The guard function is used along with the if statement. If the guard function evaluates to true, then the statement “N is greater than 10” is displayed. If the guard function evaluates to false, then the statement “N is less than 10” is displayed. The output of the above program will be as follows − Output N is less than 10 Guards for ‘case’ Statements Guards can also be used for case statements so that the series of statements executed is based on the guard condition. Let’s see how we can achieve this. Example Live Demo -module(helloworld). -export([start/0]). start() -> A = 9, case A of {A} when A>10 -> io:fwrite(“The value of A is greater than 10”); _ -> io:fwrite(“The value of A is less than 10”) end. The following things need to be noted about the above example − The guard function is used along with the case statement. If the guard function evaluates to true, then the statement “The value of A is greater than 10” is displayed. If the guard function evaluates to anything else, then the statement “The value of A is less than 10” is displayed. The output of the above program will be as follows − Output The value of A is less than 10 Multiple Guard Conditions Multiple guard conditions can also be specified for a function. The general syntax of the guard statement with multiple guard conditions is given below − function(parameter) when condition1 , condition1 , .. conditionN -> Where, Function(parameter) − This is the function declaration that used the guard condition. Parameter − Generally the guard condition is based on the parameter. condition1, condition1, .. conditionN − These are the multiple guard conditions which are applied to functions. The when statement must be used when a guard condition is specified. Let’s look at a quick example of how multiple guards can be used − Example Live Demo -module(helloworld). -export([display/1,start/0]). display(N) when N > 10 , is_integer(N) -> io:fwrite(“greater then 10”); display(N) when N < 10 -> io:fwrite(“Less than 10″). start() -> display(11). The following point needs to be noted about the above example − You will notice that for the first display function declaration, in addition to the condition for N>10, the condition for is_integer is also specified. So only if the value of N is an integer and greater than 10, this function will be executed. The output of the above program will be as follows − Output Greater than 10 Print Page Previous Next Advertisements ”;

Erlang – Discussion

Discuss Erlang ”; Previous Next Erlang is a general purpose or you might say a functional programming language and runtime environment. It was built in such a way that it had inherent support for concurrency, distribution and fault tolerance. Erlang was originally developed to be used in several large telecommunication systems. But it has now slowly made its foray into diverse sectors like ecommerce, computer telephony and banking sectors as well. Print Page Previous Next Advertisements ”;

Erlang – Quick Guide

Erlang – Quick Guide ”; Previous Next Erlang – Overview Erlang is a functional programming language which also has a runtime environment. It was built in such a way that it had integrated support for concurrency, distribution and fault tolerance. Erlang was originally developed to be used in several large telecommunication systems from Ericsson. The first version of Erlang was developed by Joe Armstrong, Robert Virding and Mike Williams in 1986. It was originally a proprietary language within Ericsson. It was later released as an open source language in year 1998. Erlang, along with OTP, a collection of middleware and libraries in Erlang, are now supported and maintained by the OTP product unit at Ericsson and widely referred to as Erlang/OTP. Why Erlang? Erlang should be used to develop your application, if you have the following requirements − The application needs to handle a large number of concurrent activities. It should be easily distributable over a network of computers. There should be a facility to make the application fault-tolerant to both software and hardware errors. The application should be scalable. This means that it should have the ability to span across multiple servers with little or no change. It should be easily upgradable and reconfigurable without having to stop and restart the application itself. The application should be responsive to users within certain strict timeframes. The official website for Erlang is https://www.erlang.org/. Erlang – Environment Now before you can start working on Erlang, you need to ensure that you have a fully functional version of Erlang running on your system. This section will look into the installation of Erlang and its subsequent configuration on a windows machine to get started with Erlang. Ensure that the following system requirements are met before proceeding with the installation. System Requirements Memory 2 GB RAM (recommended) Disk Space No minimum requirement. Preferably to have enough storage to store the applications which will be created using Erlang. Operating System Version Erlang can be installed on Windows, Ubuntu/Debian, Mac OS X. Downloading Erlang To download Erlang, one must go to the following url − www.erlang.org/downloads. This page has a variety of downloads and also the steps required to download and install the language on Linux and Mac platforms. Click on the ‘OTP 18.3 Windows 32-bit Binary File’ to begin the download of the Erlang Windows Installation file. Erlang Installation The following steps detail how Erlang can be installed on Windows − Step 1 − Launch the Installer downloaded in the earlier section. After the installer starts, click Run. Step 2 − Click Next on the following screen to accept the default components, which will be installed. Step 3 − Accept the default installation path and click Next. Step 4 − Accept the default Start Menu item, which will be created and click Next. Step 5 − After the installation is complete, click Close to complete the installation. Erlang Configuration After the installation is complete the following configuration needs to be carried out to ensure that Erlang starts working on the system. OS Output Windows Append the String; C:Program Files(x86)erl7.2.1bin OR C:Program Fileserl7.2.1bin to the end of the system variable PATH. If you now open the command prompt and type erl, you should be able to get the erl command prompt. Congratulations, you now have erl successfully configured on your laptop. Installation of Plugin-ins on Popular IDE’s Erlang as a programming language is also available in popular IDE’s such as Eclipse and IntelliJ. Let’s look at how we can get the required plugin’s in these IDE’s so that you have more choices in working with Erlang. Installation in Eclipse Step 1 − Open Eclipse and click the Menu item, Help → Install New Software. Step 2 − Enter the Work with link as https://download.erlide.org/update Then click Add. Step 3 − You will then be prompted to enter a Name for the plugin, enter the name as Erlide. Click Ok. Step 4 − Eclipse will then scan the link provided and get the required plugins. Check the plugins and click Next. Step 5 − In the next dialog box, Eclipse will show all the components which will be installed. Click Next. Step 6 − In the next dialog box, Eclipse will just ask to review the components being installed. Click Next. Step 7 − In the next dialog box, you just need to accept the license agreement. Finally, click the Finish button. The installation will then begin, and once completed, it will prompt you to restart Eclipse. Once Eclipse is restarted, when you create a project, you will be able to see Erlang as an option as well. Installation in IntelliJ Please follow the subsequent steps to install IntelliJ in your computer. Step 1 − Open IntelliJ and click Configure → Plugins. Step 2 − Type Erlang in the search box. You will get Erlang plugin on the right hand side of the screen. Click the Install button. Step 3 − After the Erlang plugin is installed, you will be prompted to restart the IDE. When you restart the IDE and try to create a new project, you will see the option to create an Erlang project. Erlang – Basic Syntax In order to understand the basic syntax of Erlang, let’s first look at a simple Hello World program. Example Live Demo % hello world program -module(helloworld). -export([start/0]). start() -> io:fwrite(“Hello, world!n”). The following things need to be noted about the above program − The % sign is used to add comments to the program. The module statement is like adding a namespace as in any programming language. So over here, we are mentioning that this code will part of a module called helloworld. The export function is used so that any function defined within the program can be used. We are defining a function called start and in order to use the start function, we have to use the export statement. The /0 means that our function ‘start’ accepts 0 parameters. We finally

Erlang – Preprocessors

Erlang – Preprocessors ”; Previous Next Before an Erlang module is compiled, it is automatically processed by the Erlang Preprocessor. The preprocessor expands any macros that might be in the source file and inserts any necessary include files. Ordinarily, you won’t need to look at the output of the preprocessor, but in exceptional circumstances (for example, when debugging a faulty macro), you might want to save the output of the preprocessor. To see the result of preprocessing the module some_module.erl give the OS shell command. erlc -P some_module.erl For example, suppose if we had the following code file − Example -module(helloworld). -export([start/0]). -include(“user.hrl”). start() -> io:fwrite(“~w”,[?macro1(1,2)]). And if we executed the following command from the command line − erlc –P helloworld.erl A file called helloworld.P would be generated. If you open this file, you would find the following contents which is what the preprocessor would compile. -file(“helloworld.erl”, 1). -module(helloworld). -export([start/0]). -file(“user.hrl”, 1). -file(“helloworld.erl”, 3). start() -> io:fwrite(“~w”, [{1 + 2}]). Print Page Previous Next Advertisements ”;

Erlang – Distributed Programming

Erlang – Distributed Programming ”; Previous Next Distributed Programs are those programs that are designed to run on networks of computers and that can coordinate their activities only by message passing. There are a number of reasons why we might want to write distributed applications. Here are some of them. Performance − We can make our programs go faster by arranging that different parts of the program are run parallel on different machines. Reliability − We can make fault-tolerant systems by structuring the system to run on several machines. If one machine fails, we can continue on another machine. Scalability − As we scale up an application, sooner or later we will exhaust the capabilities of even the most powerful machine. At this stage we have to add more machines to add capacity. Adding a new machine should be a simple operation that does not require large changes to the application architecture. The central concept in distributed Erlang is the node. A node is a self-contained. The Erlang system contains a complete virtual machine with its own address space and own set of processes. Let’s look at the different methods which are used for Distributed Programming. Sr.No. Methods & Description 1 spawn This is used to create a new process and initialize it. 2 node This is used to determine the value of the node on which the process needs to run. 3 spawn on Node This is used to create a new process on a node. 4 is_alive This returns true if the local node is alive and can be part of a distributed system. 5 spawnlink This is used to create a new process link on a node. Print Page Previous Next Advertisements ”;

Erlang – Processes

Erlang – Processes ”; Previous Next The granularity of concurrency in Erlang is a process. A process is an activity/task that runs concurrently with and is independent from the other processes. These processes in Erlang are different than the processes and threads most people are familiar with. Erlang processes are lightweight, operate in (memory) isolation from other processes, and are scheduled by Erlang’s Virtual Machine (VM). The creation time of process is very low, the memory footprint of a just spawned process is very small, and a single Erlang VM can have millions of processes running. A process is created with the help of the spawn method. The general syntax of the method is given below. Syntax spawn(Module, Name, Args) Parameters Module − This is a predefined atom value which must be ?MODULE. Name − This is the name of the function to be called when the process is defined. Args − These are the arguments which need to be sent to the function. Return Value Returns the process id of the new process created. For example An example of the spawn method is shown in the following program. Live Demo -module(helloworld). -export([start/0, call/2]). call(Arg1, Arg2) -> io:format(“~p ~p~n”, [Arg1, Arg2]). start() -> Pid = spawn(?MODULE, call, [“hello”, “process”]), io:fwrite(“~p”,[Pid]). The following things need to be noted about the above program. A function called call is defined and will be used to create the process. The spawn method calls the call function with the parameters hello and process. Output When we run the above program we will get the following result. <0.29.0>”hello” “process” Now let’s look at the other functions which are available with processes. Sr.No. Methods & Description 1 is_pid This method is used to determine if a process id exists. 2 is_process_alive This is called as is_process_alive(Pid). A Pid must refer to a process at the local node. 3 pid_to_list It converts a process id to a list. 4 registered Returns a list with the names of all registered processes. 5 self One of the most commonly used BIF, returns the pid of the calling processes. 6 register This is used to register a process in the system. 7 whereis It is called as whereis(Name). Returns the pid of the process that is registered with the name. 8 unregister This is used to unregister a process in the system. Print Page Previous Next Advertisements ”;

Erlang – Maps

Erlang – Maps ”; Previous Next 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. 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 Live Demo -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 as follows. Output 2 Some of the other methods available for maps are as follows. Sr.No. Methods & Description 1 from_list This method is used to generate a map from a list. 2 find This method is used to find if a particular key exists in the map. 3 get This method is used to get the value of a particular key in the map. 4 is_key This method is used to determine if a particular key is defined as a key in the map. 5 keys This method is used to return all the keys from a map. 6 merge This method is used to merge 2 maps. 7 put This method is used to add a key value pair to the map. 8 values This method is used to return all the values from a map. 9 remove This method is used to remove a key value from the map. Print Page Previous Next Advertisements ”;

Erlang – Useful Resources

Erlang – Useful Resources ”; Previous Next The following resources contain additional information on Erlang. Please use them to get more in-depth knowledge on this. Useful Video Courses Python Flask and SQLAlchemy ORM 22 Lectures 1.5 hours Jack Chan More Detail Python and Elixir Programming Bundle Course 81 Lectures 9.5 hours Pranjal Srivastava More Detail TKinter Course – Build Python GUI Apps 49 Lectures 4 hours John Elder More Detail A Beginner”s Guide to Python and Data Science 81 Lectures 8.5 hours Datai Team Academy More Detail Deploy Face Recognition Project With Python, Django, And Machine Learning Best Seller 93 Lectures 6.5 hours Srikanth Guskra More Detail Professional Python Web Development with Flask 80 Lectures 12 hours Stone River ELearning More Detail Print Page Previous Next Advertisements ”;

Erlang – BIFS

Erlang – BIFS ”; Previous Next BIFs are functions that are built into Erlang. They usually do tasks that are impossible to program in Erlang. For example, it’s impossible to turn a list into a tuple or to find the current time and date. To perform such an operation, we call a BIF. Let’s take an example of how BIF’s are used − Example Live Demo -module(helloworld). -export([start/0]). start() -> io:fwrite(“~p~n”,[tuple_to_list({1,2,3})]), io:fwrite(“~p~n”,[time()]). The following things need to be noted about the above example − In the first example, we are using the BIF called tuple_to_list to convert a tuple to a list. In the second BIF function, we are using the time function to output the system time. The output of the above program will be as follows − Output [1,2,3] {10,54,56} Let’s look at some of the more BIF functions available in Erlang. Sr.No. BIF Functions & Description 1 date This method returns the current system date. 2 byte_size This method returns the number of bytes contained in a Bitstring. 3 element The method returns the Nth element in the tuple. 4 float This method returns the float value of a particular number. 5 get The method returns the process dictionary as a list. 6 put This method is used to put a key,value pair in the process dictionary. 7 localtime The method is used to give the local date and time in the system. 8 memory Returns a list containing information about memory dynamically allocated by the Erlang emulator. 9 now This method returns the tuple {MegaSecs, Secs, MicroSecs} which is the elapsed time since 00:00 GMT, January 1, 1970. 10 ports Returns a list of all ports on the local node 11 processes Returns a list of process identifiers corresponding to all the processes currently existing on the local node. 12 universaltime Returns the current date and time according to Universal Time Coordinated (UTC). Print Page Previous Next Advertisements ”;