Home > Net >  is there a way I could have a function in haskell to unwrap two monads and maybe produce the third o
is there a way I could have a function in haskell to unwrap two monads and maybe produce the third o

Time:10-03

So essentially as far as i understand, using

mb_l :: Maybe L
l2mb_c :: L -> Maybe C
thing = mb_l >>= l2mb_c

in Haskell is equivalent to using

fn thing(mb_l: Option<l>, l2mb_c: fn(_: l) -> Option<c>) -> Option<c> {
    l2mb_c(mb_l?)
}

in rust. But in rust I also can do something like

fn thing2(mb_a: Option<a>, mb_b: Option<b>, ab2mb_c: fn(_: a, _: b) -> Option<c>) 
-> Option<c> {
    ab2mb_c(mb_a?, mb_b?)
}

Is there a way I could do this in Haskell?

CodePudding user response:

If you have

mb_a :: Maybe A
mb_b :: Maybe B
ab2mb_c :: A -> B -> Maybe C

you can combine them as

thing = do
  a <- mb_a
  b <- mb_b
  ab2mb_c a b

or, using the applicative style

thing = join $ ab2mb_c <$> mb_a <*> mb_b
  • Related