xkcd: Functional

XKCD comic on functional programming. Overlay money quote:

Functional programming combines the flexibility and power of abstract mathematics with the intuitive clarity of abstract mathematics.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

So true. What saves it for

So true. What saves it for me is the appeal of tail recursion...

Now that was weird.

Now that was weird. I finished reading the final comment in this thread, and suddenly found myself back at the end of the original post.

lose cavalier power, gain predictive power

Programmers like to ask what these functional languages, generally what these restrictive languages, gain you. They are quick and right to point out what you lose up front: freedom to dynamically dispatch on types, freedom to insert effects everywhere, freedom to modify memory all ways, freedom to perform list operations on booleans, freedom to modify the program itself on the fly...

For those restrictions, I gain ease of reasoning, in fact some reasoning becomes trivial. With type-case gone, I gain parametricity. With effects and mutations restricted, I gain more repeatable, predictable results. With type-cavalierism (to perform list operations on booleans, opposite of type-safety) gone, I gain integrity of memory content at the low level and a tidy mental model at the high level. With self-modifying code gone—who misses it anyway?—I gain sanity.

Don't take the fun out of

Don't take the fun out of functional.

The best of both worlds

I write my code in a mostly-functional style in whatever language. I feel free to mutate local variables in first-order languages, because that can't do any serious harm. Where I need to insert a more global mutation, I do so. I type-case routinely in order to avoid the straitjacket of making sure everything is an explicit union type (in essence, everything can be silently cast to type ⊤.

This is why I prefer...

... dysfunctional programming.

Run with that. It could be a

Run with that. It could be a cool case of critical programming language design :)

Recursion? In 2013? Really?

He however did hit a pet peeve of mine, which is the common shibboleth that functional programming favors recursion (particularly tail recursion) over looping. As an industry programmer using functional languages daily, I probably write a recursive function definition approximately once per year. Maybe 20% of those uses would be tail-recursive. FWIW, that's also about as often as I use explicit looping (solely for performance reasons). I consider all of those to be code smells, signs that my design is lacking in some regard. What I use instead of looping and recursion is, of course, higher order combinators over my data structures: map, flatmap/bind, filter, fold, exists, groupBy, etc. Those may be recursively implemented (but probably aren't, for performance reasons), but why should I care? Programming via higher-order combinators like that allows me to put out code that is almost branch-less, and thus ludicrously easy to test and debug. Recursion? It's notably unusual for me to even use "if" statements anymore!

map and fold are just memes

map and fold are memes that pass as very good abstractions also, but as soon as you really start worrying about performance, can you really ignore (tail) recursion? Foldr vs. foldl vs. foldl' comes to mind...

one liner

The "functional programming combines the flexibility and power of abstract mathematics with the intuitive clarity of abstract mathematics" was funny. Does anyone have a similar one liner about OO or Structured paradigms?

"If it was one line, it

"If it were one line, it wouldn't be structured programming"

If it were one line, it

If it were one line, it would probably be APL.

APL: Multi-dimensional

APL: Multi-dimensional arrays, one-dimensional programs.

How's about you just switch it?

Object-oriented programming combines the intuitive clarity of objects with the flexibility and power of objects.