Mapping Out Lambda Land: An Introduction to Functional Programming

lambda jewellery and keychains

Anyone who has met me will probably know that I am wildly enthusiastic about functional programming (FP). I co-founded a group for women in FP, have presented a series of talks and workshops about functional concepts, and have even been known to create lambda-branded clothing and jewellery. In this blog post, I will try to give some insight into what the fuss is about. I will briefly explain what functional programming is, why you should care, and how you can use OpenShift to learn more about FP.

Functional Programming Defined

What is functional programming? The most succinct answer is…programming with functions! However, using your favorite programming language’s ‘function’ keyword does not mean you are doing functional programming. The functions I am talking about are functions in the mathematical sense, that is, functions that simply map inputs to outputs.

Let’s take addition (+) as an example. You might just think of it as a symbol from high school math, but it also happens to be a function that takes two arguments and returns their sum. What is great about addition, and other mathematical functions like it, is that we can count on it to do only that and to do it consistently.

The addition function will only use the inputs you give it to calculate its result. The function’s sole mission is to calculate that return value; it will never mess with program state or do anything else observable — that is, it will not have any side effects. If you give it the same two arguments, it will always return the same result. We call functions like this pure functions.

An expression made up of calls to pure functions — such as 1 + 2 or add(1, 2) — can be said to be referentially transparent. This just means we can replace this expression in our code with its value, which in this case is the result of the add function call, without affecting the overall behavior of the program in any way. Using pure functions and referentially transparent expressions means that we can reason about our code the same way we reason about equations in math, which is powerful.

Functional programming is a paradigm or style of programming that treats computation as the evaluation of functions like the ones I have just described. Functional code also typically favors immutable data, using modified copies of values rather than altering the original ones. FP is not limited to particular programming languages, although some languages facilitate this style of coding better than others. Programs are created by gluing many small, focused functions together to transform data and produce the behavior desired.

Although functional programming has been growing in popularity in recent years, the core ideas are far from new. FP has its roots in lambda calculus, which was developed by Alonzo Church in the 1930s. Lambda calculus and the Turing machine have been shown to be equally expressive for modelling computations.

While the FP definition I’ve given may be simple, it has huge ramifications. Perhaps you have already had some questions pop into your head about how this could possibly work in practice. There are many implications that flow from this definition, which I don’t have the space to delve into here. I have included some links at the end of this post to help you get started on learning more.

Why Functional Programming Matters

So, functional programming is coding with pure functions. Makes sense, but why would anyone want to do that?

There are a whole host of reasons, but I think a core one is the ability to reason about the correctness of your code.

Coding with functions that simply map inputs to outputs makes code much easier to test. Using pure functions and embracing immutable data also helps when you want to run code in parallel, which is becoming all the more relevant in today’s increasingly distributed world of computing. In the words of Pragmatic Programmer Dave Thomas:

“[W]e’re going to be living in a multicore, distributed, concurrent — all the buzz words — world. The conventional models we’ve been doing, the OO stuff…is not going to survive in that kind of environment. We’re already seeing it beginning to fall apart.”

The practice of using small units of functionality as building blocks also helps to facilitate modularity. A modular approach aids code reuse and makes programs easier to maintain and extend — as most developers have drummed into them not long after Hello World. In his seminal paper Why Functional Programming Matters, John Hughes argues that modularity is the key to successful programming and that functional programming features contribute significantly to that.

Functional programming may have been around since the 1930s, but it is growing in popularity today. Functional features are being added to traditional mainstream languages and many new languages are being built with FP front of mind, such as Elixir, Idris, and Rust.

Many believe functional programming has a key part to play in the future of computing, for the reasons expressed by Dave Thomas in the quote above. If you are not familiar with functional concepts yet, now is a great time to get on board the lambda express.

Using OpenShift on Your FP Learning Journey

cloud lambdabot

Red Hat’s open source Platform as a Service, OpenShift, is a polyglot playground. It supports many programming languages that support multiple programming paradigms, including the functional paradigm.

One big advantage of using a Platform as a Service (PaaS) is that it abstracts away configuration headaches, which helps developers to get up and running with unfamiliar technology stacks with ease.

Although you can do functional programming in pretty much any programming language, there are some languages that make it easier than others. I think it is helpful to use one of these functionally oriented languages when you are trying to learn the core principles.

When you are starting out, you will probably want to begin with some theory and by learning the syntax of whichever language you choose. However, it won’t be long before you’ll want to test out your new skills by putting them into practice. I think doing this using what for many is a familiar domain, web development, is a great way to progress on your FP journey, and that is where OpenShift can come into play.

Although none of the Red Hat-supported programming languages for OpenShift (Java, PHP, Ruby, Python, Perl, and JavaScript (Node.js)) are typically characterized as functional, the open source nature of the platform and its cartridge system and the rich community-driven ecosystem surrounding it make it easy to run many other languages.

The best language to use may well depend on you and your preferences. I learnt functional programming with Haskell, which can be used to express FP concepts in a very succinct manner. I have written a blog post explaining how to get started with Haskell on OpenShift using the community-created cartridges.

JVM languages, most notably Scala and Clojure, are also popular choices for learning more about FP, that you can try out on OpenShift.

Elixir, a new functional language that runs on the Erlang VM, has also been pitched as a good option for FP beginners. Erlang can run on OpenShift, although the community-created cartridges for it are not as mature as other offerings, so there is more work to be done on that front.

Aside from the functionally oriented languages, of course you can also try writing web apps using a functional style and supported language or platform such as Node.js. Nodeschool has a functional programming module that teaches some FP concepts in JavaScript.

Whichever language you choose, spinning up an application on OpenShift with a particular language or framework cartridge is still as simple as a one-line terminal command or a few clicks in the web console.

Summary

Functional programming is a style of coding that treats computation as the evaluation of pure functions. This approach has big benefits when it comes to evaluating code correctness, modularity, maintainability, and the ease of testing and debugging. The functional style can also help with parallel programming. These perks and the changing computing landscape are fueling a boost in interest in functional programming. The open source OpenShift platform can help you experiment with alternative programming languages that embrace the functional paradigm, such as Haskell, Scala, Clojure, Erlang, and Elixir.

Reading and Resources

Next Steps

Categories
Educators, OpenShift Container Platform, OpenShift Online
Tags
, , , , , , ,
  • Gage Peterson

    Thanks! This is an awesome post! I’m really having fun learning Elixir, but have found Haskell quite interesting as well.

  • Gage Peterson

    I would really like to see better support for Elixir in the near future.