Functional Programming Primer

Functional Programming is the hot new keyword in the coding world. Look! We’re Functional! However, I’m pretty sure most people jumping on the Functional Programming bandwagon have no idea where Functional Programming came from, why functional programming is important, or why the lowercase greek letter lambda is splashed all over the covers of Functional Programming books.

Never fear! This is  your quick Functional Programming Primer. I’ll give you the 10,000 foot overview of the benefits of Functional Programming, along with its benefits, and even explain the origin of the lambda.

Note: Check out my other Functional Programming tutorials, too.

Note: I have listed out the most common functional programming languages here.

How Functional Programming Got Its Lambda

While not extremely important for creating Functional programs, it is interesting to know where the lambda that graces Functional programs came from.

Functional Programming is based on the theories of Lambda Calculus. Lambda Calculus, created by Alonzo Church, is a notation used for representing mathematical and logical problems. Alan Turing, one of Alonzo Church’s students, used a mathematically equivalent notation for programming his computational engines. Modern programming can be traced  back to Lambda Calculus.

Lambda Calculus used the concept of bound variables when defining functions. Essentially, Church’s Lambda Calculus functions had arguments with pre-defined values, like modern computer programming functions and methods do. Before Lambda Calculus, bound variables were represented by variables with circumflexes, such as ŷ. Church wanted to distinguish his bound variables from the traditional bound variables by moving the circumflex to the front of the variable.

Church then went one step further, realizing that a circumflex looks like a capital lambda, he changed it to a lowercase lambda, something that Greek-literate mathematicians of the time would have instantly recognized. So bound variables, an extremely integral part of Lambda Calculus, look like this:


And that’s house Functional Programming got it’s lambda!

Benefits of Functional Programming

Before 2005, increased clock rate (megahertz and gigahertz) was the key draw of new computers. The next generation of new computers had better clock rates, so was assumed to be faster. Since 2005, the primary improvements have been in the number of cores available to programs. Yes, cache, bus size, and and other features still improve, but more cores is where the most notable improvements are coming from.

Until multi-core systems proved to be the wave of the future, concurrency / multi-threading was mostly ignored when writing programs. Concurrency mattered when dealing with external events such as UIs or data retrieval from remote devices.

Now that multi-core systems are definitely not going away, modern programs need to look to concurrency for significant speed bumps. The problem with concurrency is it is difficult. Toy examples of locking one method or piece of data at a time don’t seem bad, but in an intricate system you will have multiple methods and chunks of data that need manipulating at once, and several threads seeking access to the same data and methods. Any complex concurrent system that is poorly written risks mutex bottlenecks, or worse yet, deadlocks.

The benefit of Functional Programming is that it prevents the need for locking data and synchronizing methods. Functional Programming uses pure functions that behave the same way no matter how many threads are hitting them at the same time. Also, Functional Programming shows how to use immutable data that doesn’t have the risk of changing during access by multiple threads.

All this allows Functional Programmers to take advantage of the multi-core revolution without the stress of worrying about bad side affects from use of multiple threads. The best kind of mutex is an unused mutex! Functional Programming allows for fully concurrent programs with a lot less stress and complexity found in OOP programs using concurrency.

Functional Programming Languages

A self disciplined coder can use functional techniques in any language, though some languages don’t lend themselves to functional programming styles. Most common languages allow for functional programming these days. From a functional programming perspective, I still split languages into three categories:

  1. Languages that are not intended for functional programming
    C and Perl are two languages that fall soundly into this category. You can pull off functional paradigms with both languages, just as you can do object oriented design on both languages, but any self-respecting software architect will throw-up a little in their mouth at the thought of doing so.
  2. Languages that have functional programming capabilities added as an after thought
    C# and Java both had functional programming features added years after their initial designs. That is nice, if you want functional programming, and want to leverage languages that already have large programmer bases to choose candidates from. However, the ease of falling into old object oriented programming habits makes these challenging platforms to get good functional programs from. You’ll need a good software architect and lots of detailed code reviews when making the switch to functional programming in C# and Java environments.
  3. Languages designed from the ground up for functional programming
    Clojure and Haskell are languages designed from the ground up to be Functional. It is nearly impossible to write OOP programs in either. Since Functional and OOP designs really don’t play nice together, going with a language designed from the beginning to be functional is popular with companies determined to take advantage of Functional design.