guard

Conditional failure of Alternative computations. Defined by
guard True  = pure ()
guard False = empty

Examples

Common uses of guard include conditionally signalling an error in an error monad and conditionally rejecting the current choice in an Alternative-based parser. As an example of signalling an error in the error monad Maybe, consider a safe division function safeDiv x y that returns Nothing when the denominator y is zero and Just (x `div` y) otherwise. For example:
>>> safeDiv 4 0
Nothing
>>> safeDiv 4 2
Just 2
A definition of safeDiv using guards, but not guard:
safeDiv :: Int -> Int -> Maybe Int
safeDiv x y | y /= 0    = Just (x `div` y)
| otherwise = Nothing
A definition of safeDiv using guard and Monad do-notation:
safeDiv :: Int -> Int -> Maybe Int
safeDiv x y = do
guard (y /= 0)
return (x `div` y)
Conditional failure of Alternative computations. Defined by
guard True  = pure ()
guard False = empty

Examples

Common uses of guard include conditionally signaling an error in an error monad and conditionally rejecting the current choice in an Alternative-based parser. As an example of signaling an error in the error monad Maybe, consider a safe division function safeDiv x y that returns Nothing when the denominator y is zero and Just (x `div` y) otherwise. For example:
>>> safeDiv 4 0
Nothing
>>> safeDiv 4 2
Just 2
A definition of safeDiv using guards, but not guard:
safeDiv :: Int -> Int -> Maybe Int
safeDiv x y | y /= 0    = Just (x `div` y)
| otherwise = Nothing
A definition of safeDiv using guard and Monad do-notation:
safeDiv :: Int -> Int -> Maybe Int
safeDiv x y = do
guard (y /= 0)
return (x `div` y)
Conditional failure of Alternative computations. Defined by
guard True  = pure ()
guard False = empty

Examples

Common uses of guard include conditionally signaling an error in an error monad and conditionally rejecting the current choice in an Alternative-based parser. As an example of signaling an error in the error monad Maybe, consider a safe division function safeDiv x y that returns Nothing when the denominator y is zero and Just (x `div` y) otherwise. For example:
>>> safeDiv 4 0
Nothing
>>> safeDiv 4 2
Just 2
A definition of safeDiv using guards, but not guard:
safeDiv :: Int -> Int -> Maybe Int
safeDiv x y | y /= 0    = Just (x `div` y)
| otherwise = Nothing
A definition of safeDiv using guard and Monad do-notation:
safeDiv :: Int -> Int -> Maybe Int
safeDiv x y = do
guard (y /= 0)
return (x `div` y)
Conditional failure, returning only if the condition is True.
Generalization of guard
An expression guarded by a single boolean statement.
| otherwise = ()
=====
guard (var "otherwise") unit
Provides an if condition bound to the conjured function's return type. Guards are only alllowed at the root fo the RHS.
last' :: [Int] -> Int
last' [x]  =  x
last' [x,y]  =  y
last' [x,y,z]  =  z
> conjure "last" last'
>   [ pr ([] :: [Int])
>   , prim ":" ((:) :: Int -> [Int] -> [Int])
>   , prim "null" (null :: [Int] -> Bool)
>   , guard
>   , prim "undefined" (undefined :: Int)
>   ]
last :: [Int] -> Int
-- 0.0s, testing 360 combinations of argument values
-- 0.0s, pruning with 5/5 rules
-- 0.0s, 1 candidates of size 1
-- 0.0s, 0 candidates of size 2
-- 0.0s, 0 candidates of size 3
-- 0.0s, 0 candidates of size 4
-- 0.0s, 0 candidates of size 5
-- 0.0s, 0 candidates of size 6
-- 0.0s, 4 candidates of size 7
-- 0.0s, tested 2 candidates
last []  =  undefined
last (x:xs)
| null xs  =  x
| otherwise  =  last xs
Monadic boolean combinators.
This module contains monadic predicates.
A single guard.
Either lifts a value into an alternative context or gives a minimal value depending on a predicate. Works with Alternatives.
guarded even 2 == [2]
guarded odd 2 == Nothing
guarded (not.null) "My Name" == Just "My Name"
A variant of guarded using Functor-wrapped values.
guardedA (return . even) 42    == Just [42]
guardedA (return . odd) 42     == Just []
guardedA (const Nothing) 42    == (Nothing :: Maybe [Int])
Succeed only if the extension is disabled.
Succeed only if the extension is enabled.