This is a simple, friendly guide for people who don’t know anything about computer science, or people who know computers but want to learn less. If you don’t know a kilobyte from a mitochondria, don’t worry! Just 3 years ago I didn’t know anything about computer science. Fast forward to today, I still don’t. So we’re in the same boat!
For the first lesson in series, we’re going to talk about an intimidating topic: monads. I want this series to be for readers of all ages, so I’m going to try to be as delicate as I can and leave out some of the details. The most important thing to remember is that monads are nothing to be ashamed of, it’s okay if you have them or if you don’t, and they are beautiful and healthy when shared with someone you love.
That said, we need to back up a bit and give some context. Monads are a part of a type of programming called functional programming. That sounds really computer sciencey, but don’t be scared! We’ve seen that word before. Maybe you know a functional alcoholic. It’s basically the same thing. It’s saying that even though you are programming, it manages to work somehow and no one notices there’s a problem.
You’re probably thinking, “well the opposite kind of programming must be non-functional programming,” or programming that doesn’t work. Good guess! But actually the other big way of programming is object-oriented programming, which basically means programming oriented around disagreeing, or objecting to the code that’s already there.
“Now my head’s spinning!” you’d probably say if this was the 50s. “If you have to object to code that’s there, how do you even get code on a blank screen?” This is a really clever process called bootstrapping, which refers to the phrase “pull yourself up by your own bootstraps,” which is something rich people like to say. One of these rich people was an early tech guy named Xander Brickus who said it so often people called him Captain Bootstrap. He invented this technique and that’s why it’s called bootstrapping.
Anyway, what you do is you have one of the programmers on the team, either the one with the thickest skin or the one you hate the most, just put on a blindfold and type for 5 minutes while people yell the 10 most famous Monty Python and Douglas Adams quotes at them from all sides, to disorient them and stop them from having original thoughts. Hopefully by the end, you have a bunch of illegible garbage. Another way to achieve this result is to import the text from a comment section and run it through a parser to clean it up a little.
Now that you’ve seeded the codebase, you have all the other programmers look at what the first guy wrote and come with objections. They can then go ahead and write an improvement to it, or they can tell the first programmer how stupid he or she is before writing an improvement to it. Both methods have their proponents. This is why the first programmer should either be mentally tough or, in your eyes, deserve abuse.
So back to functional programming. If there isn’t any such thing as non-functional programming, and object-oriented programming apparently works, more or less, why would they call it functional programming? Well, the short story is that everyone wants to believe their programming works. Functional programming advocates just come right at you and explicitly say it in the name, like they’re daring you to say it doesn’t. Object-oriented programming advocates are more uneasy with that kind of bald-faced lie and tend to evade the question of whether it works by saying, “Look, the important thing is that we question everything. Everyone knows skepticism is healthy,” and if you say, “That doesn’t even remotely answer the question,” they’ll say, “I like that! Objecting to my answer means you have a critical mind!” and you’ll feel so flattered you forget to press them on it.
So to catch up: functional programming claims to work. All other types of programming also claim to work. Do any of them work? That’s not for us to judge.
But is there anything else to functional programming other than the claim that it works? Some would say no. But that’s incorrect. It also has a mildly disturbing obsession with purity. I’m not talking about obsession with people’s purity, which is more of a side hobby, but the purity of functions.
A function is a bunch of code instructions you group together in case you need to do the same thing a bunch of times. Like that little text blurb you keep in notepad to paste into a reply every time your mom emails you.
“Now, wait just a gosh darned minute,” you might be saying, if this was the 40s, “a function you say? Does that have anything to do with why it’s called functional programming?” No, probably not, I’ve never thought about it.
Anyway, you can write functions in a way where they accept input and/or give you output when they’re done. Like you can write a function where you can provide some input like the name of a famous person, and the function will give you back a quote that person definitely never said. So you give the function a name like “Albert Einstein,” and it whirrs and clicks and spits out, “I can’t believe I ate the whole thing!” Which is a bug because he definitely said that.
A pure function is a function where, if you give it the same input, it always gives you the same output. So it always gives you that quote from Albert Einstein’s AlkaSeltzer commercial whenever you put in his name. It doesn’t sneak in a mysterious input from anywhere (like a database, some secret environment settings, or Iran) and it doesn’t sneak out contraband output to some other place (like a hard drive, the network, or Nicaragua).
Anyway, in functional programming, you are always supposed to have pure functions because otherwise it’s filthy and disgusting and you’ll be executed by stoning if you are caught.
Which brings us back to monads. In such a puritanical programming philosophy, how can there be room for something like monads? Again, I’m going to have to tread delicately for the younger readers, but often times in a repressive society, the ways people find in private to… express forbidden urges… often far surpasses the level of those expressions in a more libertine society, taking on forms that would make many in the “liberated” society blush.
So there are a LOT of rules that govern monads (you wouldn’t want them overrunning society), but within those strictures, boy, people go nuts. I am definitely not one to judge but I feel like that even a very liberal, accepting person ought to draw a line at some point or we are no better than the animals. But I don’t want to judge.
I’ll probably follow up at some point with a deeper dive into monads for adult readers only.
Anyway, I hope you were able to take away less about monads than you knew to begin with. I know it’s a tall order if you didn’t know anything about them to begin with, but it’s okay if you didn’t get all of it, nobody gets dumb overnight. Keep reading and I’m sure one day you’ll know as little about computer science as I do.