Safe Haskell | Safe-Infered |
---|
A convenient set of useful conditional operators.
- if' :: Bool -> a -> a -> a
- (??) :: a -> a -> Bool -> a
- bool :: a -> a -> Bool -> a
- select :: (a -> Bool) -> (a -> b) -> (a -> b) -> a -> b
- cond :: [(Bool, a)] -> a
- condPlus :: MonadPlus m => [(Bool, a)] -> m a
- ifM :: Monad m => m Bool -> m a -> m a -> m a
- (<||>) :: Monad m => m Bool -> m Bool -> m Bool
- (<&&>) :: Monad m => m Bool -> m Bool -> m Bool
- notM :: Monad m => m Bool -> m Bool
- condM :: Monad m => [(m Bool, m a)] -> m a
- condPlusM :: MonadPlus m => [(m Bool, m a)] -> m a
- guardM :: MonadPlus m => m Bool -> m ()
- whenM :: Monad m => m Bool -> m () -> m ()
- unlessM :: Monad m => m Bool -> m () -> m ()
- whileM :: Monad m => m Bool -> m a -> m ()
- untilM :: Monad m => m Bool -> m a -> m ()
- while1M :: Monad m => m Bool -> m a -> m a
- until1M :: Monad m => m Bool -> m a -> m a
Simple conditional operators
bool :: a -> a -> Bool -> aSource
A catamorphism for the Bool type. This is analogous to foldr, maybe, and either. The first argument is the false case, the second argument is the true case, and the last argument is the predicate value.
Higher-order conditional
select :: (a -> Bool) -> (a -> b) -> (a -> b) -> a -> bSource
Composes a predicate function and 2 functions into a single function. The first function is called when the predicate yields True, the second when the predicate yields False.
Note that after importing Control.Monad.Instances, select
becomes a
special case of ifM
.
Lisp-style conditional operators
cond :: [(Bool, a)] -> aSource
Lisp-style conditionals. If no conditions match, then a runtime exception is thrown. Here's a trivial example:
signum x = cond [(x > 0 , 1 ) ,(x < 0 , -1) ,(otherwise , 0 )]