Answers for "expr -> maybe int"

0

expr -> maybe int

addMaybes Nothing _ = Nothing
addMaybes _ Nothing = Nothing
addMaybes (Just x) (Just y) = Just (x + y)

subMaybes Nothing _ = Nothing
subMaybes _ Nothing = Nothing
subMaybes (Just x) (Just y) = Just (x - y)
Posted by: Guest on March-09-2022
0

expr -> maybe int

multMaybes :: Maybe Int -> Maybe Int -> Maybe Int
multMaybes Nothing _ = Nothing
multMaybes _ Nothing = Nothing
multMaybes (Just x) (Just y) = Just (x * y)
Posted by: Guest on March-09-2022
0

expr -> maybe int

expression :: Math -> Maybe Int
expression (Val n)        = Just n
expression (Add e1 e2)    = liftA2 (+) (expression e1) (expression e2)
expression (Sub e1 e2)    = liftA2 (-) (expression e1) (expression e2)
expression (Mult e1 e2)   = liftA2 (*) (expression e1) (expression e2)
expression (Div e1 e2)
  | r2 /= Just 0          = liftA2 div (expression e1) r2
  | otherwise             = Nothing
  where r2 = expression e2
Posted by: Guest on March-09-2022

Browse Popular Code Answers by Language