Created
August 23, 2022 23:04
-
-
Save someodd/b538c90fa6f9f3a00c3b583d6ea3d7ff to your computer and use it in GitHub Desktop.
Challenge in parsing math expressions from some kinda AST.
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
import Control.Monad.Reader | |
import Data.Map as Map | |
data Expr = Lit Int | Var String | Add Expr Expr | Let (String, Expr) Expr | |
eval :: Expr -> Reader (Map String Int) Int | |
eval (Lit i) = pure i | |
eval (Var s) = do | |
varMap <- ask | |
case Map.lookup s varMap of | |
Just i -> pure i | |
Nothing -> error "No such variable!" | |
eval (Add expr1 expr2) = do | |
x <- eval expr1 | |
y <- eval expr2 | |
pure $ x + y | |
eval (Let (s, expr1) expr2) = do | |
val <- eval expr1 | |
withReader (modifyReader val) (eval expr2) | |
where | |
modifyReader :: Int -> Map String Int -> Map String Int | |
modifyReader val' r = do | |
Map.insert s val' r | |
evalTest :: Bool | |
evalTest = | |
let letExpr = Let ("z", Add (Lit 5) (Lit 5)) (Add (Var "z") (Lit 10)) | |
x = runReader (eval (Add (Var "x") letExpr)) $ Map.fromList [("x", 3)] | |
in x == 23 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment