Linking Multiple Arguments

Haskell has:

(>>=) :: 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
+4
source share
3 answers

This is what designations are 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 is so simple that I probably would not even define an additional operator for it, rather I would just use the notation directly.

+5
source

Not really, but you can use

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

or using only the first principles (→ =) like this (try 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
+5
source

Source: https://habr.com/ru/post/1525633/


All Articles