Why Functional Programming is The Awesomest Sauce

HappyLearnTutes
7 min readMay 13, 2016

--

“I am more convinced than ever, that it really is a big win”
— John Carmack, Creator of Quake talking at QuakeCon 2013 about Functional Programming, in Haskell

Functional Programming is about describing what the answer should be instead of describing how to get an answer. That is, it’s less about commands and more about expressions.

This article describes some of the differences benefits of choosing functional programming languages over imperative languages, and why you should begin to learn some functional programming in Haskell today!

What are commands?

In most popular programming languages, you’ll find commands. Programming languages like Java, C, JavaScript, Python and Ruby use them.

They’re usually called statements today, but spin it how you will, they’re all ways of telling the computer what to do in sequence. Like this JavaScript statement that pops up an alert window on the screen:

Statements (commands) are the basis of imperative programming

The idea of commands hearken back to the dusty old days of computer programming when we would “twiddle bits” by flipping switches, or reading punch cards in, or if you were programming in the 80’s, use the BASIC commands “peek” and “poke” to do things like build graphics or get and put other values into the computer’s memory.

Flippin’ Switches

See, there was this awesome computer (at the time) called the Altair 8800. It let ordinary people write computer programs! At the time this was amazing, because most programmers usually had to be mathematicians or computer scientists. It had two “modes” and lots of switches for input and lots of lights for output.

What you’d do is put it into programming mode and flip some switches to put some numbers into the memory locations, in a big list one after another.

Each number referred to a built-in instruction, or some numberical info. These instructions were commands: things it could do like “add this to the answer” or “subtract this from the answer”, or “put the answer in that memory location”, and also what data to work with.

All these commands went into the computer’s sequential memory locations by flipping switches. Then, you’d turn on run mode which would make the computer start at the beginning of the list of instructions, and step through doing them one by one!

Facebook uses Haskell to write their anti-SPAM software:

Usual Programming Today

That’s nice and all, but so what? Well, these commands are very similar to how we still program today, only instead of flipping switches, we write words. We can make our own commands, but these words are still all commands.

Running a command which is made of other calls to execute still more commands

Instead of having a sequence of memory locations, we have lines of code, and instead of memory locations for holding data, we have “variables” which are like little buckets for our data. When we want to “store” something, we run a command to store that data in a location.

Storing something in a variable (imperative style)

And later, when we want to retrieve the data from the location, we have a command to grab the data out. Maybe some other program has changed the value. We don’t know.

Retrieving something from a variable (imperative style)

At least, that’s how the common programming languages work.

These common programming languages grouped together are called imperative languages. There are another kind of languages called functional languages. These don’t use commands and memory locations directly, but rather talk about functions, named definitions and values.

What are expressions?

“I settled on Haskell as my functional programming language of choice to follow up on”
—John Carmack, Lead Developer for Commander Keen, Wolfenstein 3D, Doom, Quake, Rage and their sequels

Expressions are when we take values and apply functions to them. This makes definitions which we can then use in further expressions, building up functionality like building blocks.

What about Functions?

Functions relate one type of data to another

Functions in functional programming languages are simply an expression that relates some values together, where some of those values are unknown.

So if we say “twice something is a function that means two times something” then I have a function called twice. We don’t know what “something” is until we apply the function by putting it with a value, then it can be evaluated to a result. Say twice 5, and it will evaluate to 10.

When we write programs in this way, the functional programming language handles all the memory location management and commands for us, and we just declare our intended expressions of meaning.

How do expressions with functions compare to commands?

Let’s take a very simple example: Adding a shopping list up. For imperative languages, we first write commands to:

  1. Set aside a memory location we’ll call total;
  2. Do a loop, which goes over each of the items in the list:
  3. Take the “total” memory location, and add the next item’s price to it by calling the + procedure
  4. Write the result back into the “total” memory location
  5. Go back to step 3 unless there are none left in the list
  6. Return the final answer: the contents of total

For functional languages, we write some expressions that read like this:

  1. the final answer is “shopping list sum”, which is the “sum” of the “prices”
  2. “prices” is the mapping of each item on the “shopping list” to its “price”
  3. “sum” is “+” applied to each item of “a list”
  4. “price” is a number that we find for “some item”

This functional way of writing computer programs is much simpler for people to think about, because when we write functions, we simply explain what things are.

Statements or commands are the basis of imperative programming

We also explain which parts we do know and which parts we don’t know to the computer. The functional programming language won’t let the computer change the parts we do know, and this includes what types things are. For example, “prices” won’t work on anything but a list of things that can have a “price” type of information on them.

Problems with Commands

Think about the difference between working this way and writing and reading memory locations with commands. What if our command based program’s “total” memory location has another program change its value when the computer is part way through doing its calculation?

Switches! The basis of computer programming.

This might sound like a ridiculous thing so suggest, but imperative programmers have this problem all the time. It’s called a race condition because each of the two or more programs are trying to race to get to be the one to store their value in the location first, and they lock up the computer in the process.

Not only that, but when you program in an imperative language, because commands can do all kinds of things, when you write your own software, if you use a library of already written commands, you have no way of knowing that the commands you use will only do what you need them to do, especially if they’re complex. This can make writing and changing programs really, really hard.

Why is Functional Programming worth learning?

Functions usually make one small transformation to data

Functional programming simplifies this a lot because the pieces are smaller, simpler and easier to understand on their own. The parts of your code that work with unknown things are smaller and more isolated. When you use a powerful functional programming language like Haskell, you can be more sure that your programs are correct and will be less buggy, because less of your code deals with things that can change out from under you.

Aside from this, even if you don’t decide to use a functioal programming language when you write programs, you should learn one because it will make all your programming better. Maybe you can’t choose Haskell because someone else is making the decisions for you, but you can still learn it.

How does it make you a better programmer? By encouraging you to separate out your functionality into small clear pieces, and making sure you’re aware of the complexity in what you do as you do it when you write programs.

Excitement!

We’re sure you’re now super-excited to learn a functional programming language, so we’d love to suggest… drum roll… Haskell… with our free-to-read-but-please-pay-us-money-because-you-love-it functional programming introduction Happy Learn Haskell Tutorial:

Put some fun into learning Functional Programming. Yes, we said it (groan, sorry!).

We also have some videos you can start watching right now:

Read our book, let us know what you think of it (and this article in the comments below if you like), and follow us on twitter here.

--

--