Here’s my ADT:
data Ex a = I Integer --integer
| Add (Ex a) (Ex a) --add
| Variable a --variable
| Def [(a, Ex a)] (Ex a) --defining local variables
And here’s my evaluate function:
eval :: Ex a -> Integer
eval (I n) = n
eval (Add e1 e2) = eval e1 + eval e2
How do I continue eval for Variable and Def?
Some more hints to help you write the solution yourself.
Some suggestions
lookupon the list of type[(a,Ex a)]and hence needEq aconstraint on that.evalfunction should returnNothingotherwiseJust answer.Variable acase. Which is very simple as you just need tolookupa inxsand return the value after evaluating it iflookupsucceeds elseNothing.The code becomes