What Is Functional Programming?

The software consists of many modules, and each module has its function. These functions can be reused. Read this article till the end to know more about this.

Introduction

This article enriches its reader with the essence of functional programming. In functional programming, a program contains functions, and the input is given to the program using the main function. The main function process this input and produce the desired output.

The logic is divided into several functions until a function is received, which can not be further divided. Thus, in functional programming, functions resemble mathematical functions. Therefore, functional programming has its advantage and characteristics.

The main advantage of functional programming is that it has no assignment statements. The value variables persist throughout the program, and it is not changed. The value of expression does not change and can be evaluated whenever required.

The flow of program execution is not decided. Another advantage of functional programming is that all the programs are referentially transparent. Thus functional programming does not have an assignment statement, the value of variables does not change, and there is no predecided program execution flow.

Normally in conventional programming, a program contains almost 90% of assignment statements, but there is no assignment statement in the case of functional programming.

Functional Programming and Structured Programming 

Structured programming does not contain goto statements. Each block in a structured program contains only one entry and only one exit. All structured program contains modules. Modular programming improves the productivity of the program and leads to reusability. Each module can undergo an independent test and reduce the time of debugging.

Modular design improves the overall productivity of the program. Thus, languages such as Modula-II, ADA, and Machine Learning support modularity and successfully achieve their objectives.

To achieve modularity, each program is divided into sub-problems. Each sub-problem is solved independently, and the results of each sub-problem are combined to obtain the result of the overall problem.

The division of a problem into a sub-problem depends on the technique used to combine the result of each sub-problem. Functional programming provides techniques to combine sub-problems.

Concepts of Functional Programming

Functional programming consists of several programming concepts, and these concepts are specific to functional programming. For example, in functional programming, functions can be passed as an argument. Functional programming also supports Higher-order functions in which one or more than one function can be passed as an argument, and the called function returns the function as a value.

Higher-order functions are supported by functional programming in which the main function may accept one or more functions. Numerical differentiation is an example of a higher-order function. Both higher-order functions and first-class functions are essential components of functional programming. Higher-order and first-class functions accept a function as an argument and may or may not return a function as a value.

Functional programming supports pure functions. Pure functions contain expressions and do not suffer from problems related to memory leaks and input/output. For example, A pure function contains a pure expression. The advantage of a pure expression is that it can be removed from the program without influencing other expressions in the program. Two pure independent expressions can be evaluated independently.

Functional Programming Also Supports Recursion

To repeatedly execute a certain number of statements, recursion is used. Recursion is a programming construct that is used as an alternative to a loop. Each recursive function has a terminating condition. Each recursive function contains a terminating condition that needs to be satisfied before calling itself. Recursion is a memory consuming process due to which loops are often preferred over recursion.

Most of the functional programming language supports recursion. Most of the functional programming language has well-established recursion, and by placing constraints on this, functional programming becomes total functional programming.

Functional languages support strict and non-strict evaluation techniques, and this technique will evaluate the arguments of the functions. In strict evaluation, if any sub-term is evaluated to false, then the whole expression becomes false. On the other hand, in the case of lazy evaluation, sub-terms of an expression are not evaluated until required. The lazy evaluation technique is often used by programming languages such as Miranda, Clean, and Haskell.

Functional programs lack assignment statements. Therefore, the value once assigned to a variable in a functional program does not change throughout the program. Instead, the value of the variable can be changed as and when required. 

The most critical aspect of functional programming is that it avoids changing the value of the variables outside the variable’s scope.

Functional programming is used to maintain the state of the database, perform input-output operations etc. To do this, monads are used. Monads are used to maintain states. 

Functional programming languages memory consumption is more as it supports no assignment statements, and values cannot be changed once assigned to variables. In addition, functional programming languages efficiently use arrays. Lazy evaluation supported by functional programming also adds to the fast execution of the program.

Developers and Functional Programming

Most modern programming languages such as Java or Python use functional programming concepts and techniques. In addition, functional programming is used in data analysis and machine learning.  

In functional programming, functions are developed using immutable variables. Thus, in functional programming, everything is written in functions. Functions take input, process it and produce output. A function in python can be written as:

def square_1 (x_1);

Return x_1*x_1;          

This function takes the value of the variable that operates on it and return results. Functional programming is used to develop a framework on which software is developed. 

The functional programming is used to work with collections. Javascript use functional programming to use map( ) function. To declare list in javascript following functions are used:

let l_etters = [“a_1”, “b_2”, “c_3”];

console.info(l_etters.map((x_1) => x_1.toUpperCase())); // output [“A_1”, “B_2”, “C_3”]

Functional programming is used to develop more readable programs that are more resilient and more suitable to large-scale systems.

What is Functional Programming?

Functional programming is a technique of creating programs using pure functions. Function Programming is more about using expression and declaration statements and does not focus on the execution sequence of statements.

All functional programming techniques are concerned with the methods used to get results and do not focus on the process used to get the desired result. Functional programming is more concerned about what variables to compute and considers all sorts of data to be immutable. That data is not going to change throughout the program’s execution.

To write a solution to a problem in functional programming, the solution is broken down into subproblems, and each sub-problem is represented as a function. Functional programming does not support statements such as loops and if-else statements.

Functional programming has emerged from lambda calculus. This functional programming language has led to the development of LI\LSP.

Basic terminology used in functional programming are:

  • Immutable Data: Data structure in functional programming is of no use. Instead, data that already exists are modified.
  • Modularity: Functional programming supports modularity. Modules are developed to achieve the desired objective. 
  • Closure Using closure, variables declared in the parent functions are used.  

Using functional programming, confusing problems can be solved easily, and existing errors in the programs are removed. In addition, functional programming supports concurrency control and is useful in parallel processing. Finally, functional programming is fault-tolerant. 

Haskell: Functional Programming Language

The functional programming language provides a high-level view of functional language to be used in programming. Using functional programming, function libraries can be developed. The main idea of functional programming is a function that, in addition,  expects input, manipulates input, and delivers output.

AN example of functional programming construct is map( ) function. This map( ) function converts objects in a list in the desired form. A simple programming example is to add two integer values and then double them.

Add_double x y = 2 * ( x + y)

Here, x and y are inputs and result if 2*(x+y).

The inherent model of functional programming is to evaluate an expression like this, 

3+add_Double 4 5

⇒ 2…

Haskell

Haskell is a programming language having its compilers and interpreters. Haskell introduced the concept of lazy functional programming. A functional programming language provides you with a central model of programming. This central model consists of one input value. This input value is manipulated to obtain the desired result. Haskell introduced the concept of tree data type and was used in programming. 

Developing program in Haskell 

To develop a program in Haskell, it is necessary to understand what needs to be developed. In addition, the programmer must know what the available resources that need to use are and how to use these resources to develop the function. Therefore, the function programming advocates breaking the main logic into subparts.

Recursion is one of the most critical components of functional programming. Therefore, recursion is used in the development of functional programming language programs. 

Functional programming supports Int, Float, and Bool as basic data types. In addition, Haskell language supports two compound data – tuple and list. Haskell also introduced the concept of polymorphism to work on more than one type. 

Tuples, list and strings in functional programming

Tuples and lists are developed by combining the different data items into a single object, but each has its property.

The tuple is a combination of different values having different data types but put into a single object.

A list consists of different values, but all the same type and put into one object.

An example of this is to develop a market model, which keeps track of shopping baskets. Each item in the basket has a value in terms of rupees and an associated weight/quantity. To do this tuple is more suitable. A tuple is used like this,

Tuple 1 –  (“Salt_sugar: 1kg”, 130)

Tuple -2   (“Plain”, 24)

Each tuple Tuple-1, Tuple-2 has a string value and an associated value of double to it. Thus, the generalized form of this tuple is (String, Int).

To combine these tuples list is used. For example, three tuples maybe like this,

(“Salt_Sugar: 1Kg”, 123), (“Plain_Chips”, @3), (“GINI: 2lt”, 1789) 

These three tuples can be combined to build a list. This list will look like this:

[(“Salt_Sugar: 1Kg”, 123), (“Plain_Chips”, @3), (“GINI: 2lt”, 1789) ]

This list contains three tuples. Haskell functional programming language is designed so that the list contains all elements of the same type, but the internal composition of the list may contain elements of different types.

In Haskell, types can be defined. For example, two types are:

  type Shop_Item = (String, Int)

   type Basket = [ShopItem]

Here type, is the keyword. The above statements define a type. For example, the Shop_Item and Basket are types. 

In Haskell, strings consist of a particular list type. 

A tuple is designed to develop components of the same type. For example, tuple t1 can have value v1, type t2 can have value v2, and tuple t3 can have value v3.

In other programming languages, tuples are also called records and structures. 

A Haskell program to return a minimum and maximum of two Integevaluesue includes:

min_And_Max :: Int -> Int -> (Int, Int)

min_And_Max x y

x >= y = (y, x)

otherwise =  (x,y)

Pattern matching is also possible in Haskell. Functions can also be defined over tuples. These functions are defined over pattern. Thus to write,

Type (Int, Int) pattern (x,y) is used.

  Thus, add_Pair :: (Int, Int) -> Int can be represented in pattern as add_Pair (x,y) = x+y

Thus, add_Pair(5,8) will be interpreted as x to be matched to 5 and y to 8.

The pattern can also be nested, example of this includes,

add_Pair (0,y_1) = y_1

add_Pair(x_1,y_1) = x_1+y_1

shift :: ((Int, Int), Int) -> (Int, (Int, Int))

Shift ((x_1,y_1),z_1) = (x_1,(y_1,z_1))

Pattern matching is used to filter a particular part of the tuple, for example, Shop_Item type, the definition is,

name :: Shop_Item -> String

price  :: Shop_Item -> Int

Haskell define three selector functions these are,

fst (x,y) =  x

snd (x,y) = y

List in Haskelll

A kist contains items of a particular type. For each type t, there exists a Haskell type [t].

[1,2,3,4,1,4] :: [Int]

[Ture]          :: [Bool]

[1,2,3,4,1,4] :: [Int] is a list of integer and [Ture] :: [Bool] is a list of boolean. 

The order of items in a list is significant. The number of items in the list defines the length of the list. 

Other ways list cab be written include:

[nn .. mm] this is a list that contains [nn, nn+1, …, mm]. If the value nn exceeds mm the list is empty. 

[2 .. 7]  = [2,3,4,5,6,7]

[3.1 .. 7] = [3.1, 4.1, 5.1, 6.1]

[‘a’ .. ‘m’] =”abcdefghijklm”

[n,p .. m] defines a list in which the first two elements are n, and p and the last element is m.

The list [7,6 ..3] will be interpreted as [7,6,5,4,3]. The list [0.0, 0.3 .. 1.0] will be interpreted as [0.0, 0.3, 0.6, 0.9]. The list [‘a’, ‘c’, .. ‘n’] will be interpreted as “acegikm”. Thus list can be defined as having a step size.  If the last item in the list is not reached using step size,e, the last element will be the biggest value in the sequence or the smallest value in the sequence.