How do I use recursive function in Haskell?
I'm writing an evaluator for a little expression language, but I'm stuck on the Rec construct.
This is the language:
data Expr = Var Nm | Lam (Nm,Ty) Expr | App Expr Expr
| Val Int | Add Expr Expr | If Expr Expr Expr
| Rec Expr
And this the evaluator so far:
type Env = [ (Nm, Value) ]
data Value = Clos Env Expr
| Vint Int
deriving Show
ev :: Env -> Expr -> Value
ev _ (Val n) = Vint n
ev env (Add e1 e2) = Vint (n1 + n2)
where
Vint n1 = ev env e1
Vint n2 = ev env e2
ev env (If e e1 e0) = if n==0 then ev env e0 else ev env e1
where
Vint n = ev env e
ev env (Var x) = case lookup x env of
Nothing -> error (x)
Just v -> v
ev env (Lam x e) = Clos env (Lam x e)
ev env (App e1 e2) = case v1 of
Clos env1 (Lam (x,t) e) -> ev ((x,v2):env1) e
where
v1 = ev env e1
v2 = ev env e2
I would like to call the actual recursive function and execute it to get the sum from 1 to 100.
This is my test function I want to evaluate:
t1 = Rec (Lam ("f", INT :-> INT) . Lam ("i", INT) $
If (Var "i")
(Var "i" `Add` (App (Var "f") (Var "i" `Add` Val(-1))))
(Var "i")
)
ev [] (App t1 (Val 100))
asnwer : Vint 5050
I've tried to make expression.
but,I got this error, ":(6,1)-(29,98): Non-exhaustive patterns in function ev"
I think there is a running error because the recursive function is not working properly.
Can you help me solve the problem?