Functional Programming

Anshul vyas
4 min readOct 11, 2018

--

Functional programming is a programming paradigm which models the computations as the evaluation of mathematical functions. It is a declarative style of programming where the major focus is on “what to solve” rather than “how to solve”, as in the case of imperative style. It uses expressions in place of statements, where the expression is evaluated to produce a value rather than being executed to assign a value.

In functional code, the output value of a function depends only on the arguments that are passed to the function, so calling a function f twice with the same value for an argument x produces the same result f(x) each time, in contrast to procedures where depending on a local or global state, different results may be produced at different times when called with the same arguments but a different program state.

Popular Programming Languages that support functional programming include Haskell, JavaScript, Scala, Erlang, Lisp, ML, Clojure, OCaml, Common Lisp, Racket.

Lambda Calculus

Lambda calculus is a framework developed by Alonzo Church to study computations with functions. It can be called the smallest programming language of the world. It gives the definition of a computable function. It provides a theoretical framework for describing functions and their evaluation. It forms the basis of almost all current functional programming languages. It is equivalent to Turing machine in its ability to compute.

Concepts

A number of concepts and paradigms are specific to functional programming which includes the following:

Pure Functions

These functions have two main properties. First, they always produce the same output for the same arguments irrespective of anything else.
Secondly, they have no side-effects i.e. they do modify any argument or global variables or output something. Later property is called immutability.

The pure functions only result is the value it returns. They are deterministic. The properties are depicted in the snippet below.

sum(x, y)           // sum is function taking x and y as arguments
return x + y // sum is returning sum of x and y without changing them

Pure functions adhering to immutability provides a lot of advantages. It facilitates easier debug process, also allows writing parallel/concurrent applications, and enables the compiler to do mart optimations.

Recursion

There are no “for” or “while” loop in functional languages. Iteration in functional languages is implemented through recursion. Recursive functions repeatedly call themselves, until it reaches the base case. This is demonstrated in the snippet below.

fib(n)
if (n <= 1)
return 1;
else
return fib(n - 1) + fib(n - 2);

Functions are First-Class and can be Higher-Order

First-class functions are treated as the first-class variable. The first class variables can be passed to functions as a parameter, can be returned from functions or stored in data structures. Higher order functions are the functions that take other functions as arguments and they can also return functions.

The higher-order function describes a mathematical concept of functions that operate on other functions, while the First-Class function is a computer science term that describes programming language entities that have no restriction on their use.

Referential transparency

Functional programs do not have assignment statements, i.e. the value of a variable in a functional program never changes once defined. This eliminates any chances of side effects because any variable can be replaced with its actual value at any point of execution.

int plusone(int x) {return x+1;} // it does not implicitly change the input x and thus has no such side effects.
// So the expression is referentially transparent.

Functional programs do not have assignment statements. If functions have to store some value, they define new variables instead. So, functional programs are referentially transparent. State of any variable is constant at any instant.

Advantages of Functional Programming

  1. Pure functions are easier to understand because they don’t change any states and depend only on the input given to them. Their function signature gives all the information about them i.e. their return type and their arguments.
  2. The ability of functional programming languages to treat functions as values and pass them to functions as parameters make the code more readable and easily understandable.
  3. Testing and debugging is easier. Since pure functions take only arguments and produce output, they don’t produce any changes don’t take input or produce some hidden output. They use immutable values, so it becomes easier to check some problems in programs written uses pure functions.
  4. It is used to implement concurrency/parallelism because pure functions don’t change variables or any other data outside of it.
  5. It adopts lazy evaluation which avoids repeated evaluation because the value is evaluated and stored only when it is needed.

--

--

Anshul vyas

Product Engineer @ GO-JEK Tech | History and Literature Enthusiast |IIITIAN | Nerd | Music Lover |