I'm doing an exercise that requires me to write a function that capitalize all the first letters of the words of a string.
Here is what I did so far:
upperFirst:: String -> String
upperFirst str = let
upperFirstForEachWord (firstLetter:others) = toUpper firstLetter : map toLower others
in unwords (map upperFirstForEachWord (words str))
And this is how it works:
upperFirst "" = ""
upperFirst "hello friends!" = "Hello Friends!"
But also:
upperFirst " " = ""
upperFirst " a a a " = "A A A"
I'm losing the white spaces at the beginning, at the end and the double ones because of the function words
.
How can I keep them recursively (without working on all the possible cases)?
Thank you for any help!