September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,28 +1,47 @@
|
|||
{-# LANGUAGE FlexibleContexts #-}
|
||||
|
||||
import Text.Parsec
|
||||
import Text.Parsec.Expr
|
||||
import Text.Parsec.Combinator
|
||||
import Data.Functor
|
||||
import Data.Function (on)
|
||||
|
||||
data Exp = Num Int
|
||||
| Add Exp Exp
|
||||
| Sub Exp Exp
|
||||
| Mul Exp Exp
|
||||
| Div Exp Exp
|
||||
data Exp
|
||||
= Num Int
|
||||
| Add Exp
|
||||
Exp
|
||||
| Sub Exp
|
||||
Exp
|
||||
| Mul Exp
|
||||
Exp
|
||||
| Div Exp
|
||||
Exp
|
||||
|
||||
expr
|
||||
:: Stream s m Char
|
||||
=> ParsecT s u m Exp
|
||||
expr = buildExpressionParser table factor
|
||||
where table = [[op "*" (Mul) AssocLeft, op "/" (Div) AssocLeft]
|
||||
,[op "+" (Add) AssocLeft, op "-" (Sub) AssocLeft]]
|
||||
op s f assoc = Infix (f <$ string s) assoc
|
||||
factor = (between `on` char) '(' ')' expr
|
||||
<|> (Num . read <$> many1 digit)
|
||||
where
|
||||
table =
|
||||
[ [op "*" Mul AssocLeft, op "/" Div AssocLeft]
|
||||
, [op "+" Add AssocLeft, op "-" Sub AssocLeft]
|
||||
]
|
||||
op s f = Infix (f <$ string s)
|
||||
factor = (between `on` char) '(' ')' expr <|> (Num . read <$> many1 digit)
|
||||
|
||||
eval :: Num a => Exp -> a
|
||||
eval (Num x) = fromIntegral x
|
||||
eval (Add a b) = eval a + eval b
|
||||
eval (Sub a b) = eval a - eval b
|
||||
eval (Mul a b) = eval a * eval b
|
||||
eval
|
||||
:: Integral a
|
||||
=> Exp -> a
|
||||
eval (Num x) = fromIntegral x
|
||||
eval (Add a b) = eval a + eval b
|
||||
eval (Sub a b) = eval a - eval b
|
||||
eval (Mul a b) = eval a * eval b
|
||||
eval (Div a b) = eval a `div` eval b
|
||||
|
||||
solution :: Num a => String -> a
|
||||
solution
|
||||
:: Integral a
|
||||
=> String -> a
|
||||
solution = either (const (error "Did not parse")) eval . parse expr ""
|
||||
|
||||
main :: IO ()
|
||||
main = print $ solution "(1+3)*7"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue