Recent developments in the programming language world is indicating towards a wider acceptance of functional programming. It is not new, it has been actively used in the academia and in niche industries in form of languages like Lisp. But recently certain typical features of functional programming are available in the new-age general programming languages and are being added to the old ones.
What Is Functional Programming?
Functional programming is a programming paradigm where computing is done by evaluating expressions. It is based on the concept of Lambda Calculus in Mathetmatics, and the application of functions is a direct descendant of the calculus counterpart. It does not do anything other than computing. There is no change in state involved, which means that there is no storage and there is no sequential execution. In the functional programming world, these are in fact called side-effects of computing.
The functional programming languages rely on functions to enable evaluating of expressions. To be able to perform modular programming, there is a need for treating functions as first-class objects. This means, just like any object, a function can be created during execution of another function, or passed to other functions as parameters or even can be return values of functions – basically you can do anything with functions what you can do with normal objects.
John Hughes provides a comprehensive insight for modular programming using functional programming in his technical paper Why Functional Programming Matters (pdf). He explains that functional programming provides facilities like higher-order functions and lazy evaluation that work as glues between modules. They highly enhance modular programming and better reuse of modules. This results in smaller and readable programs that can benefit the software development world.
Functional programming carries certain features to enable better handling of functions. Functions that treat other functions are called higher-order functions. They can take other functions as input and even output a function. Functional programming languages also support closures, where the inner functions can access local variables of the outer functions. Also, to ease passing of functions as parameters a lot of functional programming languages support anonymous functions, where a block of code can be passed around. These anonymous functions are called Lambda functions in the mathematical and functional programming domain.
Recursion is actively used in functional programming for control flow. This especially visible in iterating logic, like processing elements in a list.
Another very typical feature of functional programming is Referential Transparency. It is a direct result of no side effects. An expressions evaluation is dependent only on itself, not on global state. This means that any function would always provide the same output if it was provided the same input. It not only improves isolates behavior of the funciton but also makes it easier to understand and use.
Functional Programming In Non-Functional Languages
While some of the features can be implemented in older languages, they are not very convenient. Here are some links which show how functional programming can be carried out in non-functional languages:
Recently there has been an advent of proposals to natively support some of the features for languges like C++ and Java. Dr Valentin Samko has put a proposal to include Lambda functions in standard C++. Gilad Bracha, Neal Gafter, James Gosling, and Peter von der AhÃ© have proposed adding Closures to Java (pdf). More interesting aspect is that the communities have not opposed them. Bill Venners speculates, but cites favorable incidents, about support for closures in Java. Considering that there are already efforts in C++ community these efforts will be well received.
The new-age languages, including scripting languages, like Python, Ruby, C#, Scala support a lot of features from the functional programming domain. There are common uses of functional programming languages like XSLT today. Recent developments like Linspire/Freespire adopting Haskell, a pure functional language, for its core have highlighted application of functional programming in today’s software engineering. Does this mean that functional programming is being slowly accepted in the mainstream?
If this happens, it means a huge paradigm shift for the designers and developers. Functional Programming is going to sound Greek to any developer who is experienced in imperative or object oriented programming. It is not very easy to understand the concept of evaluating expressions or lambda functions without a mathematical background. In the beginning it might feel a little repulsive, but my suggestion, dig a little deeper and you will see the beauty. The industry itself will decide whether functional programming hits the deck or not, but its knowledge will definitely help you write better code.