MonadPlus definition for Haskell IO

后端 未结 3 479
独厮守ぢ
独厮守ぢ 2021-01-04 06:17

I was just writing a quick bit of code, and I wanted to use the guard function in the IO Monad. However, there is no definition of MonadPlus for IO which means that we canno

3条回答
  •  佛祖请我去吃肉
    2021-01-04 07:14

    There're functions precisely made for this: in Control.Monad, the functions when and its counterpart unless. Anthony's answer can be rewritten as such:

    handleFlags :: [Flag] -> IO ()
    handleFlags flags =
        when (Help `elem` flags) $ putStrLn "Usage: program_name options..."
    

    specifications:

    when :: (Applicative m) => Bool -> m () -> m ()
    unless bool = when (not bool)
    

    Link to docs on hackage.haskell.org

    If more is needed, here's a link to another package, specifically monad-oriented and with several more utilities: Control.Monad.IfElse

提交回复
热议问题