Idea of this blog is to understand the functional programming concepts using python.
1. Programming Paradigms
There are three programming paradigms: Imperative Programming, 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 characterstics of the functional programmings :
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.
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.
def FindFormula(shape): if (shape == "Square"): return SquareArea if (shape == "Circle"): return CircleArea def SquareArea(a): return (a * a) def CircleArea(r): return (22/7*r * r) def findShape(sides): if (sides == 4): return "Square" if (sides == 0 ): return "Circle" 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.
def oFunction(x,y): def SqArea(x,y): return (x*y) return SqArea(x,y)
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.
def curried_pow(x): def h(y): return pow(x,y) return h
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.
def sumA(a): def sumB(b): return(a+b) return(sumB)
x = sumA(10) y = x(20) print (y)
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:
def fact(n): if (n == 0): return 1 else: return n * fact(n-1)
The same can converted to Tail Recursion as below, by having additional argument:
def fact(n): tailFact(n,1) def tailFact(n,a): if (n == 0): return(a) else: return tailFact(n-1,n*a)
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.
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).
 Haskell Wiki – https://wiki.haskell.org/Functional_programming
 Currying – https://mtomassoli.wordpress.com/2012/03/18/currying-in-python/