The idea of this post is to understand functional programming concepts using Python.
1. Programming Paradigms There are three programming paradigms:
Functional Programming and
Logic Programming. Most of the programming languages support only imperative style. Imperative style is having direct relationship with machine language. The features of imperative styles like assignment operator, conditional execution and loops are directly derived from machine languages. The procedural and object oriented programming languages such as C, C++, Java are all imperative programming languages. Logic programming is completely a different style, it will not contain the solution to the problem instead written in terms of facts and rules. Prolog, ASP & DataLog are some of logic programming languages.
2. Functional programming Wikipedia definition says " In computer science,
functional programming is a
programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data". Following are the characteristics of the functional programmings :
Immutability : Functional languages must support immutable objects by default, mutable object must be declared explicitly and consciously. In contrast, imperative style languages supports mutable objects by default also immutable objects must be declared explicitly from a different library.
Function are first class citizens: Functions are first class citizens and it is handled like any other object. It means functions can be stored as an object, functions can be passed as an argument to other functions, function can return function.
Lexical Scoping: In functional programming, the scope of the function depends on the location of its definition. If a function is defined inside a function, then its scope is only within the outer function. It cannot be referred outside the outer function.
3. Type of Functions
3.1 Higher Order Functions: A function can take other function as a argument and may return a function.
if (shape == "Square"):
if (shape == "Circle"):
return (a * a)
return (22/7*r * r)
if (sides == 4):
if (sides == 0 ):
if __name__ == "__main__":
size = 5
area = FindFormula(findShape(sides=4))(size)
print("Area = ",area)
3.2 Anonymous Functions
A function without a name is called an anonymous function. Generally these functions are defined inside other functions and called immediately.
area = lambda x : 22/7*x*x
Anonymous functions are created with "lambda" keyword. We can use this syntax, where we need a function.
3.3 Nested Functions
Functions can be defined within the scope of another function. This type of functions defined inside a other function. The inner function is only in scope inside the outer function. It is useful when the inner function is being returned or when it is being passed into another function.
sum = oFunction(10,20)
In the above example, the function SqArea is strictly inside the scope of oFunction().
Simple definition by Cay S Hartsmann in his book "Scala for the impatient":
Currying is the process of turning a function that takes two arguments into a function that takes one argument. That functions returns a function that consumes the second argument. The generalized definition for currying: A function that takes multiple arguments and turning into a chain of functions each taking one argument and returning the next function, until the last returns the result.
A closure is a persistent scope which holds on to local variables even after the code execution has moved out of that block. The inner function that remembers the state of the outer function, even after the outer function has completed execution. Generally this behavior is not possible in imperative programming styles, because the function is executed in a separate stack frame. Once the function completes its execution the stack frame is freed up. But in Functional Programming, as long as the inner function remembers the state of the outer function, the stack frame is not freed up.
x = sumA(10)
y = x(20)
Benefits of Closures: It avoids the use of global variables and provides data hiding. In scenarios, where the developer does not want to go for object oriented program, closures can be handy to implement abstraction.
3.6 Tail Recursion A recursive function is called as Tail Recursive when the last statement in the function makes the recursive calls only. It is much more efficient than the recursive function because, since the recursive call is the last statement, there is nothing to be saved in the stack for the current function call. Hence the tail recursion is very light on the memory utilization. Regular Recursive Function for Factorial in Python:
if (n == 0):
return n * fact(n-1)
The same can converted to Tail Recursion as below, by having additional argument:
if (n == 0):
4 Benefits of Functional Programming
Clarity & Complexity
: Functional programming supports writing modular, structured and hierarchical code. The use of anonymous functions, local functions makes it easier to organize the code hierarchically. The currying and closures are reduces the complexity of the program.
Concurrency : Programming for multi-core architecture is a challenge, where single piece of code can be executed by many threads parallel. So, the issues of code re-entry problem and shared memory areas will arise. In imperative style programming we must use synchronization techniques to avoid these problems, which will lead to performance impact. The philosophies of functional programming style fits to suite the need of multi-core programming by enforcing immutability.
Memory Efficient: Functional programming style is memory efficient. The anonymous functions, nested functions and tail-recursion styles are very light on the memory. Because of the lexical scoping, once the function is out of scope they will be removed from memory and tail-recursion is very efficient by not holding the stack frames.
5 Summary We have briefly discussed the functional programming paradigm & its benefits. The functional programming style is most suitable for developing algorithms, analytic, data mining and machine learning algorithm. Most of the modern programming languages are supporting functional styles, also Java is trying to catch up with this style in the latest version (Java 8).