Friday 31 August 2012

Functional programming with python

Even we cant depend completely on functional programming, we should employ this approach whenever possible. In that sense, Python is a good platform for mixing up different programming approaches in a most convenient way

Python functional programming facilities

We shall discuss python functional programming capabilities based on following examples

With Python object oriented programming, we can notate an arithmetic progression as
>>> class term:
 def __init__(self,a,d):
  self.value=a
  self.d=d
 def next(self): 
  self.value+=self.d
  return self 

>>> x=term(1,2)
Here x is capable of generating terms in an arithmetic progression with a=0 and common difference d=2, as follows.
>>> x.next() 
>>> x.value
3
>>> x.next() 
>>> x.value
5
>>> x.next()
>>> x.value
7 
We can perform such an abstraction with functional programming facilities as follows.
>>> def term(a,d):
...     return(lambda f: f(a,d))
... 
>>> def next(s):
...     return s(lambda a,d: term(a+d,d))
... 
>>> def value(s):
...     return s(lambda a,b:a)
... 

>>> x=term(1,2)
>>> value(x)
1
>>> x=next(x) 
>>> value(x)
3
>>> value(next(next(next(next(term(1,2))))))
9
Inside term(a,b) a nameless function is defined using lambda expression. The definition of this lambda function contain use of local variables of function term(). The closure property of functional languages facilitate this kind of definition of functions inside a function. Again, this lambda function accepts a function f of the form
f(a,b):
     return expression_on(a,b)

as its argument and apply f on local variables a and b of function term(a,b) then return the result. So this lambda function is a higher order function, that is a function accepts functions as arguments. What is returned by term function is a higher order function which has access on local variables of term(). This ability to use function as return value is called currying.

What's the benefit?

Functional programs contain no side-effects. that is, A function call can have no effect other than to compute its result.


Function term(a,b) returns a higher order function x with a,b as tis local variables. With special functions value(s) and next(s) we can have read access to the local variables. In the second case x is a function object with constant state. In other words state is strictly conserved. in first case, in this style of programming, state change for x occurs  as a side effect for each call to member function next().

Advantage of Eliminating side effects is "Referential transparency"

In first case
>>>x.next().value==x.next().value
False 
in second case
>>>value(next(x))==value(next(x))
True 

Here value(next(x)) returns same value at every time applied on x. That means we can replace this expression with a value whenever it occurs. This property is called referential transparency. No one feel any confusion because it is "Mathematically provable". In first case we can maintain referential transparency by adding some extra object replications instead of just returning a self. But in functional programming approach, it is an inherent characteristic.

No comments:

Post a Comment