So, I'm trying to make a function to rename variables in a Lambda Calculus expression, but I can't seem to figure out how to get a Char that's not already in use so I can set it as the new name to a variable.
I've already got a list with all the Chars that are being used as names to variables, so what I need is just a function that receives this list e returns a differnt Char, i.e. one that's not in use.
A function like that:
getNewVarName :: [Char] -> Char
I don't really use lists, or Haskell, for that matter, so I hope you can excuse my silly question.
// Edit 1
-- acceptableNames is passed as the second argument to getNewVarName
acceptableNames = ['a'..'z'] ++ ['A'..'Z']
getNewVarName :: [Char] -> [Char] -> Char
-- vars is the list with all the names actually being used
-- acceptable == acceptableNames
getNewVarName vars acceptable
| (not(null acceptable)) = let e = head acceptable
in if(elem e vars)
then getNewVarName vars (tail acceptable)
else e
| otherwise = '$' -- returns '$' just if all acceptableNames are used up
Basically, what it does is return the first Char in acceptableNames
that is not contained in vars
. Apparently, it works.
I realize there must be lots of better ways to do it, but, considering my knowledge on Haskell, that was the way that made sense to me.
Please, let me know of any ways I can improve this code.
// Edit 2
@luqui answer worked perfectly in this case. Way simpler than my solution.