I'm trying to solve a Church numerals parser I have a custom type which distinguishes between variable, lambda and application
type Var = String
data Term =
Variable Var
| Lambda Var Term
| Apply Term Term
deriving Show
I have a function called church
I can define a cases manually for different variations of church. So let's say:
church 0Lambda "f" (Lambda "x" (Variable "x"))church 1should outputLambda "f" (Lambda "x" (Apply (Variable "f") (Variable "x")))church 2should outputLambda "f" (Lambda "x" (Apply (Variable "f") (Apply (Variable "f") (Variable "x"))))
and so on.
I've tried recursively call the church function as per:
church :: Int -> Term
church 0 = Lambda "f" (Lambda "x" (Variable "x"))
church i = Apply (church(i -1)) (Apply (Variable "f") (Variable "x"))
However that keeps repeating also the part with Lambda "f" (Lambda "x"
The other approach I've tried was
church :: Int -> Term
church 0 = Lambda "f" (Lambda "x" (Variable "x"))
church i = Apply (church (i -1)) (Apply (Variable "f") (Variable "x"))
However this also yields result with copied lambdas. Am I missing something here? How can I only repeat the application part (Apply (Variable "f") (Variable "x"))