3 ms·
This is at the risk of courting GPT-fatigue, but this "what is a monad" question seems done to death. I'd ask here: For what is the following understanding of a
by motohagiography 4y ago
This is at the risk of courting GPT-fatigue, but this "what is a monad" question seems done to death. I'd ask here: For what is the following understanding of a monad insufficient?
> what is an example of a monad
"One example of a monad is the Maybe monad, which is often used to represent computations that may or may not have a result. The Maybe monad allows you to define computations that may or may not return a value, and provides methods for handling the absence of a value in a predictable and composable way. For example, you might use the Maybe monad to represent a computation that retrieves a user's profile from a database. If the user exists, the computation will return the user's profile; if the user does not exist, the computation will return nothing. The Maybe monad provides methods for handling both cases in a clean and declarative manner, without having to resort to explicit checks for null values or other error-prone techniques."
> what is an example of the maybe monad
...< some example code >
"In this example, the getUserProfile function uses the Maybe.fromValue method to wrap the user object that is returned from the database lookup in a Maybe monad. This allows us to chain methods such as map and orElse on the result of the getUserProfile function, without having to worry about null or undefined values. If the user exists, the computation will return the user's name in all uppercase letters. If the user does not exist, the orElse method will return the default value 'USER NOT FOUND' instead."
In this view, it's a function that returns a known but abstracted symbol of some underlying state. It sounds like the function true() is a monad in the sense that it returns or doesnt, but instead of TRUE, a generic monad could be something like isElephant, notHotdog, isSandwich, where the state represented by the monad can become an attribute (or a condition) of some other entity, depending on the order in which you evaluate the relationship between the monad and what it is an abstraction for.
I have either just won the "explain monads" quest, or failed quite publicly at it while writing something persuasive but misleading, but if I were to create an abstract state like isSandwich, could I be using a monad?
- yamtaddle 4y agoFor me, the hardest part of understanding monads was convincing myself that the super-simple thing I just thought I'd understood was, in fact, correct, given the hype about how difficult they are to understand (and, indeed, a lot of the tutorials are weirdly-terrible) Similar story for pointers, actually. And recursion.
- LudwigNagasena 4y ago"A monad is a monoid in the category of endofunctors, what's the problem?"
- trafnar 4y agoI distinctly remembering this happening when I was learning to program and learned what a "function" was. My brain was telling me it must be some other more complicated thing.
- tsimionescu 4y agoI don't think your explanation really makes it clear how list is also a monad, or how async is a monad. Edit: should have also mentioned IO here. It also misses the point about what having support in your language for monads actually buys you - this is in fact what I've always missed: some examples of operations that apply to any monad, which would motivate why I want Monad to be a type in itself. The only example I ever grokked is Haskell's do notation, but that is not that compelling, being compiler magic and not something I could write myself too easily.