I have a function
parseArgs :: [String] -> StdGen -> IO ()
which selects the function to run. The main looks like
main = parseArgs <$> getArgs <*> getStdGen >>= id
The problem I have, parseArgs <$> getArgs <*> getStdGen
is of type IO (IO ())
, which I extract using (>>= id)
which is of type Monad m => m (m b) -> m b
. Is there a way to avoid requiring the "extraction" of the value while having just a single line function?
You could pair up the arguments and put them through a single bind:
This avoids the extraction from nested IO. Admittedly it's no shorter but I find it easier to think about.
It fits a general pattern of
doTheWork =<< getAllTheInputs
which might be the way you'd end up arranging things anyway, if the code was more complicated.The easiest way would be with
join
:Personally, I would prefer the form
where
Or just use a do
You can define an operator for this:
If you have a function of type
then you can write
where each
xi
has typem ai
.In your case it would be simply