Skip to content

Instantly share code, notes, and snippets.

@PhyrexTsai
Last active April 23, 2016 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhyrexTsai/a60baf1e503f7b5e773c794ffaa81df6 to your computer and use it in GitHub Desktop.
Save PhyrexTsai/a60baf1e503f7b5e773c794ffaa81df6 to your computer and use it in GitHub Desktop.
Int expressions.md
type Name = String
data Expr = Var Name
| Lit Int
| Expr :+: Expr
| Expr :*: Expr
deriving (Eq, Show)
eval :: Env -> Expr -> Int
type Env = [(Name, Int)]
eval e (Var s) = lookUp e s
eval e (Lit n) = n
eval e (x :+: y) = eval e x + eval e y
eval e (x :*: y) = eval e x * eval e y
lookUp :: Eq a => [(a,b)] -> a -> b
lookUp xys x = the [ y | (x', y) <- xys, x == x']
where the [x] = x
main = do
print $ eval [("P", 3), ("Q", 4)] ((Var "P" :+: Var "Q") :*: Var "Q") -- 28

Int expressions

Define the “evaluate (eval)” function for the following data types of Int expressions.

Type Name = String
data Expr =  Var Name | Lit Int  --constant
					  | Expr :+: Expr
		              | Expr :*: Expr
	                  deriving (Eq, Show)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment