Question

In Haskell, there is:

(>>=) :: Monad m => m a -> (a -> m b) -> m b

Is there a function:(?)

bind2 :: Monad m => m a -> m b -> (a -> b -> m c) -> m c
Was it helpful?

Solution 2

This is what do notation is meant for.

bind2 :: (Monad m) => m a -> m b -> (a -> b -> m c) -> m c
bind2 ma mb f = do
  a <- ma
  b <- mb
  f a b

It's so simple that I probably wouldn't even define an extra operator for it, rather I'd just use do notation directly.

OTHER TIPS

Not quite, but you could use

bind2 :: Monad m => m a -> m b -> (a -> b -> m c) -> m c
bind2 x y f = join $ liftM2 f x y

or, using only first principles (>>=), like this (try it in ghci)

Prelude> :set +t

Prelude> let bind2 x y f = x >>= \ a -> y >>= \ b -> f a b
bind2 :: Monad m => m a -> m a1 -> (a -> a1 -> m b) -> m b

Prelude> let bind2 x y f = do a <- x ; b <- y ; f a b
bind2 :: Monad m => m t -> m t1 -> (t -> t1 -> m b) -> m b
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top