The bind
function (>>=
) has the signature:
m a -> (a -> m b) -> m b
However, I want a function with the signature:
m (t a) -> (a -> m (t b)) -> m (t b)
Specifically, I have a function that given an Integer, it returns a list of integers in within an IO:
f :: Int -> IO [Int]
but I want to apply it to an IO of list of Integers
and I can't use the regular bind function because it is wrapped in two containers i.e. a list contained in an IO. Searching on hoogle doesn't help.
I am using the following approach to implement this:
Let's say the implementation of the function is:
f :: Int -> IO [Int]
f x = do
return $ [x-10, x+10]
I am using two helper functions to get what I want:
f' :: [Int] -> IO [Int]
f' xs = do
list <- traverse f xs
return $ join list
f'' :: IO [Int] -> IO [Int]
f'' xs = do
ys <- xs
f' ys
The above works but I would like to know if there is a better / idiomatic way to implement this in haskell?