Last updated: February 12, 2019
Learn Functional Programming: This Style of Coding Will Blow Your Mind
While most programming languages consist of inputs, outputs and external variables that can be set or used from inside its functions, functional programming avoids this.
The idea behind functional programming is that every time a function is called with the same parameters, it should return the same value.
What Makes Functional Programming “Functional”?
Consider a function that returns the current temperature. It is passed only one variable that indicates whether the temperature should be returned in degrees Celsius or Fahrenheit.
The function then uses internal logic to return the current temperature — perhaps by reading a temperature sensor. This is not considered to be functional programming because each time the function is used, it has the potential to return a different value, even when the function inputs are the same.
Functional programming languages have first-class functions. This means the function can be used as if it were the value that it is returning.
For example, consider the function,
double(x), which returns twice the value of its input parameter. So
double(2) would return 4. Given that it is a first class function, the code,
double(double(2)), would be the same as the code,
As with the example, this allows nesting one function as a parameter of another and so on.
Functional programming languages also allow functions to take functions as passed variables. Note that this involves the function itself being passed, rather than just the results of the function.
Functional programming excels at implementing complex mathematical modeling; for this reason, one of the main uses of functional languages has traditionally been academic.
Many functional languages also excel when implementing parallel processing. This is due to their ability to take advantage of pure functions always returning the same value regardless of the order they are run.
Other languages need to worry about race conditions — where one function is run before the variable it uses is set to the expected value.
Many programming languages that are not functional can be used with functional programming methodology.
This developmental approach can give most of the benefits of imperative programming and functional programming. Some of the benefits that you tend to lose are the built-in abilities to help maintain purity leaving the developer with that responsibility.
Some languages are actually designed with a hybrid of methodologies along with functional programming removing some or all of these drawbacks.
Lambda Calculus is arguably the first computer language, and it is a functional language. It was first developed by Alonzo Church.
Interestingly, this language was first created in the 1930s — well before programmable computers existed. In 1958 John McCarthy, a professor at the Massachusetts Institute of Technology’s (MIT), created LISP, which was modeled after lambda calculus and is one of the most important functional languages.
There are still many versions of LISP being used today — some more functional than others.
Languages That Support Functional Programming
D was designed after C++ to get all the benefits while removing the perceived weaknesses of being backward compatible with C.
Elixir was derived and sits on top of Erlang using its ability to create h
concurrent applications while making the language easier to write and maintain.
Erlang is highly-scalable and concurrent making it ideal for telecommunication and other applications that receive massive amounts of data in an unpredictable order.
F# is an open source multi-paradigm language that is often used within the
framework. It is commonly used for rapid development of analytical software.
Haskell is a pure functional language which uses Lambda Calculus.
ML is used in mathematical, scientific, financial, analytical and other types of applications. One of its strengths is for making software for manipulating other programs.
OCaml or Objective Caml, is a free, open sourced language which was based on Caml. It tends to create very light weight programs which helps them load and run more quickly than those created in other languages.
Scala was designed to be easily integrated into Java and other object oriented languages. Scala takes advantage of functional programming, allowing it to be used in distributed and concurrent applications.
Scheme was based on the syntax of LISP and the structure of ALGOL. Due to its simplicity, Scheme is used as an introduction to program design in many computer science courses to show some of computer programming fundamentals.
Swift was designed with the goal of being fault tolerant, quick to develop in, and expressive.
Functional programming resources are usually focused on a particular language, like Scala. But even when they are, they provide valuable information. So don’t be shy just because an article or book uses a language that you don’t use.
A Practical Introduction to Functional Programming: Mary Rose Cook’s discussion of using functional programming techniques with imperative languages.
Functional Programming Basics: a good theoretical discussion of functional programming.
Don’t Be Scared Of Functional Programming: is functional programming the “mustachioed hipster of programming paradigms”? Find out here.
Introduction to Functional Programming in Swift: a detailed introduction to functional programming using Swift.
Why Are All the Functional Programming Tutorials So Mathy? — a discussion on Stack Exchange that is worth reading.
An Introduction to Functional Programming Through Lambda Calculus (2011) by Greg Michaelson: an introduction that starts at the very beginning.
Functional Thinking: Paradigm Over Syntax (2014) by Neal Ford: an advanced book on functional programming fundamentals.
The Magical World of Functional Programming: Part I: Thinking Functional (2014) by K Anand Kumar: a short ebook about the fundamentals of functional programming.
Becoming Functional (2014) by Joshua Backfield: an introduction to functional programming for people familiar with imperative and object-oriented programming.
Although functional programming is very good for some applications, many programmers find the ease of imperative languages, much easier to wrap their heads around.
But for mathematical and more formal coding, functional programming is a good choice. And understanding the principles of functional programming can be of great use to all programmers.
Further Reading and Resources
We have more guides, tutorials, and infographics related to coding and development:
F# Programming: find out about functional programming in the .NET framework.
Erlang Programming Introduction and Resources: an early functional language used for creating parallel code.
Swift Introduction and Resources: this is one of the newest C-like languages. The future of programming? Find out here!
What Code Should You Learn?
Confused about what programming language you should learn to code in? Check out our infographic, What Code Should You Learn?
It not only discusses different aspects of the languages, it answers important questions such as, “How much money will I make programming Java for a living?”