Herramientas de usuario

Herramientas del sitio


categorias:sin_monadas

data Exp = Val Int

       | Mas Exp Exp
       | Menos Exp Exp
       | Por Exp Exp
       | Div Exp Exp

{- antes de Div eval :: Exp → Int eval (Val i) = i eval (Mas a b) = eval a + eval b eval (Menos a b) = eval a - eval b eval (Por a b) = eval a * eval b eval (Div a b) = eval a `div` eval b -}

eval :: Exp → Maybe Int eval (Val i) = Just i eval (Mas a b) = case eval a of

                 Just x -> case eval b of
                             Just y -> Just (x+y)
                             Nothing -> Nothing
                 Nothing -> Nothing

eval (Menos a b) = case eval a of

                 Just x -> case eval b of
                             Just y -> Just (x-y)
                             Nothing -> Nothing
                 Nothing -> Nothing

eval (Por a b) = case eval a of

                 Just x -> case eval b of
                             Just y -> Just (x*y)
                             Nothing -> Nothing
                 Nothing -> Nothing

eval (Div a b) = case eval a of

                 Just x -> case eval b of
                             Just y -> if y == 0 then Nothing
                                       else Just (x `div` y)
                             Nothing -> Nothing
                 Nothing -> Nothing
categorias/sin_monadas.txt · Última modificación: 2018/08/10 03:03 por 127.0.0.1