module Week04.Maybe where import Text.Read (readMaybe) import Week04.Monad foo :: String -> String -> String -> Maybe Int foo x y z = case readMaybe x of Nothing -> Nothing Just k -> case readMaybe y of Nothing -> Nothing Just l -> case readMaybe z of Nothing -> Nothing Just m -> Just (k + l + m) bindMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b bindMaybe Nothing _ = Nothing bindMaybe (Just x) f = f x foo' :: String -> String -> String -> Maybe Int foo' x y z = readMaybe x `bindMaybe` \k -> readMaybe y `bindMaybe` \l -> readMaybe z `bindMaybe` \m -> Just (k + l + m) foo'' :: String -> String -> String -> Maybe Int foo'' x y z = threeInts (readMaybe x) (readMaybe y) (readMaybe z)