After reading some very basic haskell now I know how to "chain" monadic actions using bind, like:
echo = getLine >>= putStrLn
(>>=) operator is very handy in this fashion, but what if I want to chain monadic actions (or functors) that take multiple arguments?
Given that (>>=) :: m a -> (a -> m b) -> m b it seems like (>>=) can supply only one argument.
For example, writeFile takes two arguments (a FilePath and the contents). Suppose I have a monadic action that returns a FilePath, and another action that returns a String to write. How can I combine them with writeFile, without using the do-notation, but in a general way?
Is there any function with the type: m a -> m b -> (a -> b -> m c) -> m c that can do this?
putStrLnis not a monad. It's a function that returns a monad (theIOaction). Second,>>=(read 'bind') doesn't chain monads directly. It allows you to call functions on monadic values. - Xeojoin $ writeFile <$> getFilepath <*> getSomeString. Figuring out what this does is left as an exercise for the reader. - XeoIOaction is not a monad either. TheIOtype (and associated instance) is the monad here. - Tom Ellis