Skip to content

Instantly share code, notes, and snippets.

@sshine
Created October 21, 2014 14:57
Show Gist options
  • Save sshine/cbf01a69fda541ffd1b1 to your computer and use it in GitHub Desktop.
Save sshine/cbf01a69fda541ffd1b1 to your computer and use it in GitHub Desktop.
import Prelude hiding (exp)
import Data.Char
import Text.ParserCombinators.ReadP
data Exp = Plus Exp Exp
| Pow Exp Exp
| Num Int
deriving (Show)
exp :: ReadP Exp
exp = do
e <- prim
e' <- exp1 e
eof
return e'
exp1 :: Exp -> ReadP Exp
exp1 e1 = do string "+"
e2 <- exp2
exp1 (Plus e1 e2)
+++ exp2' e1
+++ return e1
exp2 :: ReadP Exp
exp2 = do
e <- prim
exp2' e
exp2' :: Exp -> ReadP Exp
exp2' e1 = do string "^"
e2 <- exp2
return (Pow e1 e2)
+++ return e1
prim :: ReadP Exp
prim = do
n <- num
return (Num n)
num :: ReadP Int
num = do
s <- many1 (satisfy isDigit)
return (read s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment