Haskell Evaluating Arithmetic Expressions and Assigning Variables

Viewed 220

I have a datatype called expr as follows:

data Expr = C Float | Expr :+ Expr | Expr :- Expr | Expr :* Expr| Expr :/ Expr

What I want to do is extend the Expr datatype to allow variables and 'let' expressions

1 Answers

You need to pass around the active bindings, in what is traditionally called "the environment":

evaluate :: Environment -> Expr -> Float

There are many ways to represent an environment.
You need two operations: looking up a binding to get a value, and extending it with a new binding.

lookup :: Environment -> String -> Float
extend :: Environment -> String -> Float -> Environment

Implementation left as an exercise.

Related