r/ProgrammerHumor • u/Keywarn • Dec 18 '17
My function programming lecturer's 'Map of The Course'
12
2
u/ralgrado Dec 18 '17
Yeah, the dragon would've killed me. Never really got monads :/
3
u/derleth Dec 18 '17
Eh, at the level of programming languages, a monad is a box with a value inside of it.
What's fun is when the box can be empty, too.
For example:
readInt :: String -> Maybe Integer
readIntis an imaginary function which takes a string and either parses it into an integer or fails to; either way, in Haskell a function has to return something, so what should we return if the parse fails?
Nothing. Return a value which cannot be confused for a valid result. If you think of monads as boxes,Nothingis the empty box.Here's a simple example of how it works in practice:
case (readInt str) of Just v -> putStrLn ("Got the integer " ++ (show v)) Nothing -> putStrLn "NO INTEGER FOUND! LAUNCH MISSILES!"
Just vis what theMaybemonad looks like when the box contains a value, andNothingis what it looks like when it does not. Notice that we went fromJust vtovwithout missing a step: You can take values out of theMaybemonad very easily.The
IOmonad is what most people think of when they think of Haskell monads, butIOis special. The language is constructed such that you can't take values out of it, just shift them from oneIOmonad box to another, in sequence. This is how Haskell imposes a sequence on actions which communicate with the outside world.3
u/Delearyus Dec 19 '17
Boxes aren't a very good way to think about monads. A list is a monad, so clearly the box can have multiple elements in it, but a binary tree is not a monad. Also, the 'boxiness' of monads doesn't give a good intuition of how monads are meant to be used.
The box example actually fits much better for the Functor typeclass, which includes all monads as well as arbitrary 'boxes' like trees, and the typeclass function (fmap) actually makes sense given the box analogy (ie when you have a box with some stuff in it, you want to be able to access the contents of the box without opening it)
Honestly it's my personal opinion that people who talk about trying to 'understand monads' are going about things the wrong way. Like, if you want to learn programming, you don't start by learning the general theory of programming languages, you probably start by learning one language, and then another, and over time you start to see what things are similar and different between them, and you develop an intuitive understanding of programming language theory in a much more natural way. Trying to understand monads is the same, just focus on learning to use Maybes and Lists and IOs and eventually you will develop an understanding of monads that can then be supplemented with some higher-level category theory stuff if you want
1
0
5
u/elaru Dec 18 '17
Mind to share who your lecturer is, and where I can find their course materials? I'd really like to take a look :)