Skip to content

Instantly share code, notes, and snippets.

@SeijiEmery
Created April 6, 2019 19:03
Show Gist options
  • Save SeijiEmery/d41b8d5a9e7ad9e54fa74a773d9faa13 to your computer and use it in GitHub Desktop.
Save SeijiEmery/d41b8d5a9e7ad9e54fa74a773d9faa13 to your computer and use it in GitHub Desktop.
Haskell Arith
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