Daniel Staudigel

@dandisagrees

How Functional Programming Ruined Me

My first programming language, 20 years ago or so, was C. I got the Borland C compiler, and the book ‘C for Dummies’ when I was 10. My mom is a geophysicist, who worked with FORTRAN at the time (don’t worry, she’s using Python now). She figured that it would be a good place to start. There really weren’t a lot of well-known choices at the time — if you didn’t know what to look for or know someone who did, it didn’t exist. In any case, I struggled hard — C is a tough language for a 10 year old. I remember banging on it for years, cargo-culting my way through the most fundamental examples involving pointers. What I’m trying to say is… I didn’t start out as a functional programming wunderkind, I fought my way through problems in C, loving the for loop.

Enough reminiscing… we should probably clarify some things. Programming is instructing a computer to perform actions on things. For example, you might want to “capitalize every word in this list”. The way to instruct a computer to do this might look like:

set i = 1
set l = length(list) # call the 'length' function
loop:
list[i] = capitalize(list[i]) # list[i] means get an item
i = i + 1
goto(loop) if i < l

In this example, the things are:

  1. i : where we are in the list
  2. list: the list itself
  3. (kinda) loop: a place in the code, that we can go to

And the actions are:

  1. length: tells you how long a list is
  2. capitalize: capitalizes a word
  3. (also kinda)goto: move to a position

Let’s say we want to take every word in a list and make it lower case… we’d have to copy most of the code from above, and just replace capitalize with downcase.

So why do we have to do this? Why can’t we just make an “action” that just says “perform this some action to every element in a list”. We can! And that is functional programming. The fundamental leap (and required language feature) is to start thinking about “actions” as just another kind of “thing”. As soon as you can pass an action into another action you’re thinking functionally.

So, in a functional language, we get a new ‘action’, called map, which takes an action and a list:

set capitalized = map(capitalize,list)
set lowercased = map(downcase,list)

What Map does in this example, is execute capitalize or downcase on every item in list. The basic difference is that in a functional language, you can just read map, and understand that what it is doing is simple, just run every item in a list through another function. It’s simple and clear. In Javascript, a functional language, the two side-by-side look like this:

function upcase(string) {
return string.toUpperCase();
}
// Functional:
var allUppercase = list.map(upcase);
// Imperative:
var allUppercase = [];
for(var i = 0 ; i < list.length ; i++) {
allUppercase[i] = upcase(list[i]);
}

I’ll be honest — it’s not that much more to read through the for logic, I know that at some point you stop reading each word, and you just see the code for what it is (blonde, brunette redhead…). However, with more complicated programs, it’s a lot nicer to represent complex actions as combinations of simpler actions, and the only way to do that is if you treat actions as just another kind of thing. I’m so used to thinking this way, that not being allowed to is an instant turnoff.

After writing code this way for a few years, I miss it every time I have to work on codebases that aren’t built in this way. When I’m embroiled in unraveling the mysteries of a 2-deep for loop, part of me wishes I had never learned functional programming. A very small part… but it’s there.

More by Daniel Staudigel

Topics of interest

More Related Stories