Principles of functional programming


A lot of concepts which are specific to the functional paradigm are pretty new and unheard of in the object oriented paradigm.

Lets take a look at the main concepts and principles of functional programming.

1. Referential transparency:
On a broader level, an expression is said to referentially transparent if it can be replaced by its value without changing your program. Essentially, the value of that function only depends on the arguments that are supplied to the function. So a referentially transparent function will never use a global variable or a I/O fetched value for computation. One of the most important advantage of writing referentially transparent programs is to optimize code using parallelization.
As two such functions are dependent on only the arguments, performing them in parallel/concurrently is possible. With the new multi core processors coming into picture, concurrent computing has become important. This is also one of the reasons why Erlang and Scala have gained popularity.

2. Higher order functions:
Functional languages treat functions as "first-class" objects. There is a subtle difference in "first-class" functions and higher order functions. Higher-order functions are functions which allow functions as arguments and also as a result of other functions. Higher order function is a mathematical concept of functions that operate on other functions.

3. Pure functions:
Functions which have no memory or I/O side effects are called pure functions. A side effect a state change which has occurred while computing the the value of that function.
Lets say for example, you increment a global counter every time the function is called, the incremented global counter is a side effect.

4. Recursion:
Looping in functional languages is normally done using recursion.

5. Evaluation strategy:
This refers to how function arguments are evaluated when an expression is processed.
Strict evaluation evaluates the arguments even before the function call is made. In non-strict evaluation the arguments are not evaluated until the values are required.

Phew!! That was a long one. Well, that's what she said? ;)

Comments

Popular posts from this blog

Introduction to functional programming

Identifying IE browser usage correctly

Comparison operators in JavaScript