Skip to content

Instantly share code, notes, and snippets.

@vst
Last active Jun 21, 2021
Embed
What would you like to do?
Haskell Auxiliary Function: ifM
-- | See https://gist.github.com/vst/a1a49e0047c8fd24adbd1759bd14f864
--
import Data.Bool (bool)
-- | Monadic version of @if-then-else@.
--
-- >>> ifM (pure False) (pure "Nice") (pure "Oh no!")
-- "Oh no!"
-- >>> ifM (pure True) (pure "Nice") (pure "Oh no!")
-- "Nice"
ifM :: Monad m => m Bool -> m a -> m a -> m a
ifM p t f = p >>= bool f t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment