Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Este código es más feo que pegarle a un padre con un calcetín sudado lleno de clavos.
- Loading branch information
1 parent
2f58352
commit fa3e64a
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
data Expr = Val Int | Add Expr Expr | Sub Expr Expr | Mul Expr Expr | Div Expr Expr | ||
|
||
eval1 :: Expr -> Int | ||
eval1 (Val n) = n | ||
eval1 (Add a b) = eval1 a + eval1 b | ||
eval1 (Sub a b) = eval1 a - eval1 b | ||
eval1 (Mul a b) = eval1 a * eval1 b | ||
eval1 (Div a b) = eval1 a `div` eval1 b | ||
|
||
eval2 :: Expr -> Maybe Int | ||
eval2 (Val n) = Just n | ||
eval2 (Add a b) = case (eval2 a, eval2 b) of | ||
(Just a, Just b) -> Just (a+b) | ||
(Nothing, _) -> Nothing | ||
(_, Nothing) -> Nothing | ||
eval2 (Sub a b) = case (eval2 a, eval2 b) of | ||
(Just a, Just b) -> Just (a-b) | ||
(Nothing, _) -> Nothing | ||
(_, Nothing) -> Nothing | ||
eval2 (Mul a b) = case (eval2 a, eval2 b) of | ||
(Just a, Just b) -> Just (a*b) | ||
(Nothing, _) -> Nothing | ||
(_, Nothing) -> Nothing | ||
eval2 (Div a b) = case (eval2 a, eval2 b) of | ||
(Just a, Just 0) -> Nothing | ||
(Just a, Just b) -> Just (div a b) | ||
(Nothing, _) -> Nothing | ||
(_, Nothing) -> Nothing | ||
|
||
eval3 :: Expr -> Either String Int | ||
eval3 (Val n) = Right n | ||
eval3 (Add a b) = case (eval3 a, eval3 b) of | ||
(Right a, Right b) -> Right (a+b) | ||
(Left k, _) -> Left k | ||
(_, Left k) -> Left k | ||
eval3 (Sub a b) = case (eval3 a, eval3 b) of | ||
(Right a, Right b) -> Right (a-b) | ||
(Left k, _) -> Left k | ||
(_, Left k) -> Left k | ||
eval3 (Mul a b) = case (eval3 a, eval3 b) of | ||
(Right a, Right b) -> Right (a*b) | ||
(Left k, _) -> Left k | ||
(_, Left k) -> Left k | ||
eval3 (Div a b) = case (eval3 a, eval3 b) of | ||
(Right a, Right 0) -> Left "div0" | ||
(Right a, Right b) -> Right (div a b) | ||
(Left k, _) -> Left k | ||
(_, Left k) -> Left k | ||
|
||
|
||
|
||
|