I have this compile function that is supposed to take a String and then yield an Expression.
This is how Expression was defined:
data Expression = Name Char | Lambda Char Expression | Apply Expression Expression
deriving Show
Let's say that the String that the 'compile' function takes is:
"\\x.\\y.yx"
So, at the end the compile function is supposed to yield this as the final Expression:
Lambda 'x' (Lambda 'y' (Name 'y') (Name 'x'))
This is the compile function:
compile :: [Char] -> Expression
compile (x:xs) = if [x] == "\\" then Lambda (head xs) (compile (tail xs))
else if [x] == "." then compile xs
else if null xs then Name x
else Name x
Currently this function obviates the last part of the Expression ((Name 'x')). My question is:
How this function can keep going further with more Names if once I yield a Name I can't keep going calling the same function with the rest of the Expression? Since Expression was defined like that, if Expression is a Name it just has a Name and no more, there is no expression left on it.
I mean, how can I take every single Name that is in Expression, 'telling' Haskell that I want to keep looking for Names and not just stop when a single Name is found.
I thought that maybe creating another function was a good idea, but I can't figure out how to call that function multiple times. Truth is that I am not really used to recursion in Haskell and I really need some help.
How can I do this?