Created
April 6, 2019 19:03
-
-
Save SeijiEmery/d41b8d5a9e7ad9e54fa74a773d9faa13 to your computer and use it in GitHub Desktop.
Haskell Arith
This file contains hidden or 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
data Expr | |
= Const Integer -- values (integers) | |
| Add Expr Expr -- addition | |
| Mul Expr Expr -- multiplication | |
| If Expr Expr Expr Expr -- conditional branching (If a == b then c else d) | |
-- Not turing complete, but should be capable of evaluating most functions. | |
deriving (Show, Eq) | |
eval :: Expr -> Integer | |
eval (Const x) = x | |
eval (Add a b) = (eval a) + (eval b) | |
eval (Mul a b) = (eval a) * (eval b) | |
eval (If a b c d) = eval $ if a == b then c else d | |
-- parse, etc TBD | |
parse :: String -> Maybe Expr | |
data Token | |
= Num Integer | |
| Add | Mul | If | |
deriving (Show, Eq) | |
tokenize :: String -> Maybe [Token] | |
resolvePrecedence :: [Token] -> Maybe Expr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment