Functional Programming - Records



A record is a data structure for storing a fixed number of elements. It is similar to a structure in C language. At the time of compilation, its expressions are translated to tuple expressions.

How to create a record?

The keyword ‘record’ is used to create records specified with record name and its fields. Its syntax is as follows −

record(recodname, {field1, field2, . . fieldn})

The syntax to insert values into the record is −

#recordname {fieldName1 = value1, fieldName2 = value2 .. fieldNamen = valuen}

Program to create records using Erlang

In the following example, we have created a record of name student having two fields, i.e., sname and sid.

-module(helloworld).  
-export([start/0]).  
-record(student, {sname = "", sid}).   

start() ->  
   S = #student{sname = "Sachin",sid = 5}. 

Program to create records using C++

The following example shows how to create records using C++, which is an object-oriented programming language −

#include<iostream> 
#include<string>
using namespace std; 

class student {
   public: 
   string sname; 
   int sid; 
   15 
}; 

int main() {    
   student S;  
   S.sname = "Sachin"; 
   S.sid = 5; 
   return 0;  
} 

Program to access record values using Erlang

The following program shows how access record values using Erlang, which is a functional programming language −

-module(helloworld).  
-export([start/0]).  
-record(student, {sname = "", sid}).   

start() ->  
   S = #student{sname = "Sachin",sid = 5},  
   io:fwrite("~p~n",[S#student.sid]),  
   io:fwrite("~p~n",[S#student.sname]). 

It will produce the following output −

5 
"Sachin"

Program to access record values using C++

The following program shows how to access record values using C++ −

#include<iostream> 
#include<string> 
using namespace std; 

class student {     
   public: 
   string sname; 
   int sid; 
}; 

int main() {     
   student S;  
   S.sname = "Sachin"; 
   S.sid = 5; 
   cout<<S.sid<<"\n"<<S.sname;  
   return 0; 
} 

It will produce the following output −

5 
Sachin 

The record values can be updated by changing the value to a particular field and then assigning that record to a new variable name. Take a look at the following two examples to understand how it is done using object-oriented and functional programming languages.

Program to update record values using Erlang

The following program shows how to update record values using Erlang −

-module(helloworld).  
-export([start/0]).  
-record(student, {sname = "", sid}).   

start() ->  
   S = #student{sname = "Sachin",sid = 5},  
   S1 = S#student{sname = "Jonny"},  
   io:fwrite("~p~n",[S1#student.sid]),  
   io:fwrite("~p~n",[S1#student.sname]). 

It will produce the following output −

5
"Jonny" 

Program to update record values using C++

The following program shows how to update record values using C++ −

#include<iostream> 
#include<string> 
using namespace std;  

class student {    
   public: 
   string sname; 
   int sid; 
};  

int main() {     
   student S;  
   S.sname = "Jonny"; 
   S.sid = 5; 
   cout<<S.sname<<"\n"<<S.sid; 
   cout<<"\n"<< "value after updating"<<"\n"; 
   S.sid = 10; 
   cout<<S.sname<<"\n"<<S.sid; 
   return 0; 
}

It will produce the following output −

Jonny 
5 
value after updating 
Jonny 
10 
Advertisements