1 module MaybeM 2 (Maybe, returnM, eachM, thenM, 3 failM, orM, guardM, filterM, theM, existsM, useM) 4 where 5 6 --1.3: data Maybe x = Just x | Nothing 7 returnM :: x -> Maybe x 8 returnM x = Just x 9 eachM :: Maybe x -> (x -> y) -> Maybe y 10 (Just x) `eachM` f = Just (f x) 11 Nothing `eachM` f = Nothing 12 thenM :: Maybe x -> (x -> Maybe y) -> Maybe y 13 (Just x) `thenM` kM = kM x 14 Nothing `thenM` kM = Nothing 15 failM :: Maybe x 16 failM = Nothing 17 orM :: Maybe x -> Maybe x -> Maybe x 18 (Just x) `orM` yM = Just x 19 Nothing `orM` yM = yM 20 guardM :: Bool -> Maybe x -> Maybe x 21 b `guardM` xM = if b then xM else failM 22 filterM :: (x -> Bool) -> Maybe x -> Maybe x 23 p `filterM` xM = xM `thenM` (\x -> p x `guardM` returnM x) 24 theM :: Maybe x -> x 25 theM (Just x) = x 26 existsM :: Maybe x -> Bool 27 existsM (Just x) = True 28 existsM Nothing = False 29 useM :: x -> Maybe x -> x 30 useM xfail (Just x) = x 31 useM xfail Nothing = xfail