[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Can someone explain to me what a monad is?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 26
Thread images: 2

File: IMG_3229.png (16KB, 650x650px) Image search: [Google]
IMG_3229.png
16KB, 650x650px
Can someone explain to me what a monad is?
>>
>>59525160
burrito
>>
Google it.
>>
>>59525160
Another word for "testicle", I believe
>>
>>59525160
A monad is a generic type constructor with the operations bind and return (sometimes also called unit).
Let a be any type. Then you get a monad by defining a type constructor M, such that:
return has the type a -> M a,
bind has the type M a -> (a -> M b) -> M b

The reason why monads are so difficult to understand at first is that they're an abstract pattern. The only way to understand it, is to find concrete examples that fit the pattern, because it's always easier to understand some concrete example rather than an abstract definition. Once you've seen enough examples you'll recognize the similarities and then you'll be able to identify the pattern. A few that are easy to understand are the maybe monad and the list monad.
>>
It's the basement segment of the Tartarus in Persona 3.
>>
>>59526432
Understanding is measured by how well you can explain something.
You're spouting gibberish and obviously have no idea what you're talking about.
NEXT!
>>
>>59527296
The quality of an explanation is not measured by how well some particular person understands that explanation.
>>
File: joke.jpg (12KB, 385x335px) Image search: [Google]
joke.jpg
12KB, 385x335px
>>59527627
The inherent joke here is the old as in ancient meme that once you understand what a monad is, you lose the ability to explain it to others.

http://sean.voisen.org/blog/2013/10/intro-monads-maybe/
>>
>>59527747
I know all the monad jokes, I just wanted to make that point.
>>
>>59527879
Well, it is a good point
>>
a monad is a context
>>
>>59527912
>a context
So it's a state? That you mutate? Or that mutates whatever you pass in?
>>
Forget fucking monads. The real question is what the fuck a zygohistomorphic prepromorphism is.
>>
>>59525160

YES!
First one for "a monad is just a monoid in the category of endofunctors.."

Basically you wrap things in other things, so you can do operations with them. Then you unwrap them.

Think of ascii symbols that get wrapped to strings. Now you can append those strings (i.e. "bla" + "blu" = "blablu", or: "bla" + "" = "bla") and still have strings. And eventually you can turn a list into ascii chars again.
>>
>>59527922
in pure languages your don't mutate, you create a modified copy
ofc with the exception of impure stuff needed to do io
>>
>>59527937
>Basically you wrap things in other things, so you can do operations with them. Then you unwrap them.
It's already been said: >>59525165
>>
>>59527962

Meh.

But it was a different wording, that doesn't count.. ;__;
>>
>>59527937
>A monad is just a monoid in the category of endofunctors.
Pretty much this. I don't get what everybody's problem is.
>>
fn :: Int -> Int -> Maybe Int
fn x y = if x <= y then pure x else Nothing

fn2 :: Int -> Int -> Maybe Int
fn2 x y = do
first_res <- fn x y -- if this is Nothing, execution "stops" here, and the function will produce a Nothing result
fn (first_res * 2) y

main :: IO ()
main = do
print $ fn2 5 10
print $ fn2 10 5


the monad is a glue between lines in the do block
in the case of IO, the monad doesn't do anything
in the case of Maybe, the monad implementation of maybe early returns if it encounters a Nothing value

it's essentially logic that you put between each line of your do expression, but don't explicitly write it out
>>
OP: What is a monad?

/g/ : It's a burrito. It's a generic type constructor. It's a context. It's a wrapper. It's a modifier. It's a copy-modifier. It's glue.

Either functional programming is super autistic, or I'm having a stroke because I can't fucking comprehend these contradicting analogies.
>>
>>59528079
analogies don't have to be consistent, they are just a tool to express something in a easier to understand manner
>>
>>59528044
fn :: Int -> Int -> Maybe Int
fn x y = if x <= y then pure x else Nothing

fn2 :: Int -> Int -> Maybe Int
fn2 x y = fn x y >>= (\x -> fn (x * 2) y)

main :: IO ()
main = do
print $ fn2 5 10
print $ fn2 10 5


is what this desugars to
the >>= (and >>) functions are responsible for implementing the per-line glue logic of the given monad
the only difference between them is that >> ignores the result of the previous function

in the case of maybe the per-statement binding logic is
    (>>=)  :: Maybe a -> (a -> Maybe b) -> Maybe b
(>>=) m g = case m of
Nothing -> Nothing
Just x -> g x
>>
>>59528096
But they're confusing me and contradict each other. How is this supposed to help me understand?
>>
>>59528127
the only way to learn monads is by dealing with them and reading lots of explanations

you can't actually understand monads, you have to mould your thinking around them, to think in their axioms instead of words and analogies
>>
you put things into a box and you interact with said things only via the bind method. also some autistic laws need to apply like composibility (they should be easly chainable) associativity (like the one in maths or pure functions).

i think the reason why functional programmers use them is because they want their types to encode more informtation like a possible null value (Maybe monad) sideeffects (IO monad) a possible failure (Either monad) but I'm not a functional programmer. if I am wrong feel free to point my mistakes out, might learn something here as well.
Thread posts: 26
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.