guard True = pure () guard False = empty
>>> safeDiv 4 0 Nothing
>>> safeDiv 4 2 Just 2A definition of safeDiv using guards, but not guard:
safeDiv :: Int -> Int -> Maybe Int safeDiv x y | y /= 0 = Just (x `div` y) | otherwise = NothingA 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)
guard True = pure () guard False = empty
>>> safeDiv 4 0 Nothing
>>> safeDiv 4 2 Just 2A definition of safeDiv using guards, but not guard:
safeDiv :: Int -> Int -> Maybe Int safeDiv x y | y /= 0 = Just (x `div` y) | otherwise = NothingA 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)
guard True = pure () guard False = empty
>>> safeDiv 4 0 Nothing >>> safeDiv 4 2 Just 2A definition of safeDiv using guards, but not guard:
safeDiv :: Int -> Int -> Maybe Int safeDiv x y | y /= 0 = Just (x `div` y) | otherwise = NothingA 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)
| otherwise = () ===== guard (var "otherwise") unit
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
guarded even 2 == [2] guarded odd 2 == Nothing guarded (not.null) "My Name" == Just "My Name"