Functional programming is a programming paradigm where the base of computation is the evaluation of expressions. Some characteristics are the use of higher-order functions, referential transparency and lazy evaluation. The advantages of the programming style include that programs are easy to read, are very reliable and can be broken into components. Disadvantages are that computations may be slow and the style and syntax are completely different from other common programming styles. The functional programming style is more often embraced by academics than by computer science professionals.
As the name suggests, functions are a fundamental part of this programming paradigm. Functions may be nested within other functions, called higher order functions, and each higher-order function can be broken down into building block functions that are easy to understand and debug. Examples of some higher order functions are Map and Nest. The function Map takes a function F and the list of variables, for instance (x, y, z) and gives the result in a list: Map [F, (x, y, z)] = (F(x), F(y), F(z)). Nest takes the function F, the variable x, and the number of iterations: Nest[ F, x, 3] = F(F(F(x))).
Pure functional programming takes an input and returns an output without ever changing a variable’s state. In other words, a function with the same input will always give the same results regardless of what has occurred previously in the program. This is called referential transparency. Since mathematical functions are referentially transparent, functional programming is intuitive to many mathematicians, engineers and scientists.
The referential transparency of functions means that the order of function evaluation is not important. Therefore functions need not be evaluated until their results are needed, which is called lazy evaluation. This is in complete contrast with imperative programming, where a program begins with the first command and runs through the list until the last command. Lazy evaluation skips over parts of the program that do not follow logically or are superfluous, which automatically optimizes the program and may reduce computing time.
Functional programming has many advantages over other programming paradigms. Functions with clear inputs and outputs are easy to read and understand. Once a function is thoroughly debugged, it can be reliably used in other applications. Multicore machines may be able to compute functions that are evaluated independently in parallel, drastically improving the performance of programs.
Unfortunately, not all programs lend themselves to parallel computing, and computing functional programs may be rather slow. Functional programs rely heavily on recursion, which is often less efficient that using traditional loops or iteration methods. In fact, functional programming can be quite clumsy and difficult to learn since it does not resemble other more common paradigms such as object oriented programming.
Academics tend to favor functional programming since it provides a clear and understandable way to program complex real world problems. Some pure languages are Haskell and Erlang. Mathematica is specialized in symbolic mathematics, R is specialized in statistics and J is specialized in financial analysis. Multiparadigm languages such as Scala and F# supports both functional programming and other programming styles.