Skip to content

Instantly share code, notes, and snippets.

@vst
Last active June 21, 2021 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vst/a1a49e0047c8fd24adbd1759bd14f864 to your computer and use it in GitHub Desktop.
Save vst/a1a49e0047c8fd24adbd1759bd14f864 to your computer and use it in GitHub Desktop.
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