-
-
Save SAdams601/6497d91ea344c0521f87 to your computer and use it in GitHub Desktop.
The State monad parse
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
type ParseState = State String | |
parse :: String -> ParseState Expr | |
parse (ch:chs) | |
| isAlpha(ch) = do | |
put chs | |
return (Var ch) | |
| isDigit(ch) = do | |
put chs | |
return (N (fromEnum ch - fromEnum '0')) | |
| ch == '(' = do | |
e1 <- parse(chs) | |
rest1 <- get | |
let (op:rest2) = rest1 | |
e2 <- parse(rest2) | |
rest3 <- get | |
let (')':restFinal) = rest3 | |
put restFinal | |
return $ apply op e1 e2 | |
| ch =='<' = do | |
let (v:':':rest) = chs | |
e <- parse(rest) | |
restFinal <- get | |
put restFinal | |
return (Assign v e) | |
where apply '+' = Add | |
apply '-' = Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment