2 ms·
The important thing to know first is that a monad is not a single thing like "Optional". "monad" is a pattern or "interface" (called a "typeclass" in Haskell),
by gowld 1y ago
The important thing to know first is that a monad is not a single thing like "Optional". "monad" is a pattern or "interface" (called a "typeclass" in Haskell), that has many implementations, (Optional, Either, List, State Transormer, IO (Input/Output), Logger, Continuation, etc). Sort of how "Visitor" pattern in C++/Java is not a single thing.
https://hackage.haskell.org/package/base-4.21.0.0/docs/Control-Monad-Instances.html https://hackage.haskell.org/package/base-4.21.0.0/docs/Contr...
https://book.realworldhaskell.org/read/monads.html https://book.realworldhaskell.org/read/monads.html
A common metaphor for monad is "executable semicolons". They are effectively a way to add (structured) hook computations (that always returns a specific type of value) to run every time a "main" computation (akin to a "statement" in other languages) occurs "in" the monad.
It's sort of like a decorator in Python, but more structured. It lets you write a series of simple computational steps (transforming values), and then "dress them up" / "clean them up" by adding a specific computation to run after each step.
- Ryder123 1y agoThis makes SchemaLoad's comment perfectly clear. (but do I appreciate the effort you put into your reply - reading that monad's are more like interfaces is new information to me, and might help down the road)
- tickettotranai 1y agoSomehow I hear this all the time, but the haskell people have to realize that code patterns are absolutely a thing in all languages, ever? A lack of syntactic sugar doesn't mean monads don't exist in other languages. Typeclasses are a distraction, the point is computation ignoring annoying contexty stuff (file not found errors, null on failure, etc) and there's dozens of examples in literally every language ever. Not all problems are solved with a technical definition.
- tome 1y ago> the haskell people have to realize that code patterns are absolutely a thing in all languages, ever? A lack of syntactic sugar doesn't mean monads don't exist in other languages. Where did you get the impression that Haskell people don't realize that code patterns are a thing in other languages, or that monads don't exist in other languages? Besides the syntactic sugar of "do notation", what Haskell has that most other languages don't have is the ability to abstract over monads, since it has higher kinded types. That is, you can write a generic operation like mapM :: (Traversable t, Monad m) => (a -> m b) -> t a -> m (t b) that works for all monads. Most other type languages force you to write mapM separately for each monad you want it to work with.