plutus-pioneer-program/code/week04/src/Week04/Maybe.hs

27 lines
784 B
Haskell
Raw Normal View History

2021-04-23 17:56:06 +02:00
module Week04.Maybe where
import Text.Read (readMaybe)
2021-04-27 09:14:58 +02:00
import Week04.Monad
2021-04-23 17:56:06 +02:00
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)
2021-04-27 09:14:58 +02:00
foo'' :: String -> String -> String -> Maybe Int
foo'' x y z = threeInts (readMaybe x) (readMaybe y) (readMaybe z)