Kwarrtz
Kwarrtz

Reputation: 2753

Multi-argument monadic bind

Is there a >>= for functions of two arguments? Something like

bind2 :: m a -> m b -> (a -> b -> m c) -> m c

Upvotes: 4

Views: 639

Answers (1)

amalloy
amalloy

Reputation: 91857

I don't know what clever combinators you could use to build this out of the standard library, but at the risk of stating the obvious it is pretty easy to implement yourself:

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

> bind2 (\a b -> [a,b]) [1,2,3] [4,5,6]
[1,4,1,5,1,6,2,4,2,5,2,6,3,4,3,5,3,6]

Upvotes: 6

Related Questions