I need to writea function titleRec :: [String] -> [String] which, given a list of words, capitalises them as a title should be capitalised. The proper capitalisation of a title (for our purposes) is as follows: The first word should be capitalised. Any other word should be capitalised if it is at least four letters long.
I came as far as,
capitalisedRec :: String -> String
capitalisedRec [] = []
capitalisedRec (x:xs) = toUpper x : lowered xs
where
lowered [] = []
lowered (x:xs) = toLower x : lowered xs
capitilized2 :: String -> String
capitilized2 xs = [toLower x | x <-xs]
titleRec :: [String] -> [String]
titleRec [] = []
titleRec (x:xs) = capitalisedRec x : adjust xs
where
adjust [] = []
adjust (y:ys) = [if (length y >= 4) then capitalisedRec y else capitilized2 y | y<- ys]
But when I run it on a list of strings, for example titleRec ["tHe", "bOSun", "ANd", "thE", "BriDGe"] it results in ["The","and","the","Bridge"], basically skips over the second word. I don't understand why.
CodePudding user response:
Your code reads:
adjust (y:ys) =
[ if (length y >= 4)
then capitalisedRec y
else capitilized2 y
| y <- ys]
but the first y
is not used later on, discarding it! The latter y
is unrelated, and shadows the first one.
A minimal modification fix would be:
adjust (y:ys) =
[ if (length y2 >= 4)
then capitalisedRec y2
else capitilized2 y2
| y2 <- y:ys]
I would strongly recommend that you always keep warnings enabled using -Wall
. Doing so, GHC would warn about y
being unused (and about the shadowing as well).