Guest User

Untitled

a guest
Nov 20th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. addop, mulop :: Parser (Expr -> Expr -> Expr)
  2. addop = (infixOp "+" Add) <|> (infixOp "-" Sub)
  3. mulop = infixOp "*" Mul
  4.  
  5. int :: Parser Expr
  6. int = do
  7. n <- number
  8. return (Lit n)
  9.  
  10. expr :: Parser Expr
  11. expr = term `chainl1` addop
  12.  
  13. term :: Parser Expr
  14. term = factor `chainl1` mulop
  15.  
  16. factor :: Parser Expr
  17. factor = int <|> parens expr
  18.  
  19. run :: String -> Expr
  20. run = runParser expr
  21.  
  22. eval :: Expr -> Int
  23. eval ex = case ex of
  24. Add a b -> eval a + eval b
  25. Mul a b -> eval a * eval b
  26. Sub a b -> eval a - eval b
  27. Lit n -> n
  28.  
  29. evExpr :: String -> Maybe Integer
  30. evExpr [] = Nothing
  31. evExpr str = Just (eval (run str))
  32.  
  33. • Couldn't match type ‘Int’ with ‘Integer’
  34.  
  35. Expected type: Maybe Integer
  36. Actual type: Maybe Int
  37. • In the expression: Just (eval (run str))
  38. In an equation for ‘evExpr’: evExpr str = Just (eval (run str))
  39. |
  40. 37 | evExpr str = Just (eval (run str))
  41. | ^^^^^^^^^^^^^^^^^^^^^
  42.  
  43. • Couldn't match type ‘SourceName
  44. -> [Char] -> Either ParseError Expr’
  45. with ‘Expr’
  46. Expected type: String -> Expr
  47. Actual type: () -> SourceName -> [Char] -> Either ParseError Expr
  48. • Probable cause: ‘runParser’ is applied to too few arguments
  49. In the expression: runParser expr
  50. In an equation for ‘run’: run = runParser expr
  51. |
  52. 26 | run = runParser expr
  53. | ^^^^^^^^^^^^^^
Add Comment
Please, Sign In to add comment