Oct 18, 2008

Functional Programming

Oct 18, 2008

You must have seen the amount of interest that is being generated for LINQ, Lamba Expressions with the release of C# 3.0.The entire Data Access Architecture has been focused on to LINQ to SQL, LINQ to XML and LINQ to Objects and more. So what exactly is Functional programming ?

FP is a programming model that treates computation as the evaluation of mathematical function.

For e.g, if we define two functions like given below
f(x) = x^2 + x + 1
g(x,y) = x * y

A problem f (g (2, 2) ) will be evaluated by compiler as
g(2*2)^2 + g(2*2) + 1
(4)^2 + (4) + 1
16+ 4 + 1
21

It is a declarative way of programming, where we leave the compiler to do the evaluation as late as possible. The user is only bothered about the result and not on how it is being evaluated.The focus is never on in the state transition of the variables, so one need not bother about the side effects.

Advantages:
 As the functional units do not have side effects, so their orders could be reversed.
 They can be performed in parallel. (suited for parallel computing)
 They are thread safe as one does not interfere other. (Thread safety)
 Function can be evaluated as late as possible, providing composability. (late evalutation)
This gives the compiler as edge to reorder or combine the evaluation of expressions in a program.

I believe it is a very strong feature for a programming language to possess.

0 comments:

Post a Comment

Top Ad 728x90