I have to write a cutString :: String -> [String]
function that has to cut a string into a list of strings. It should cut at the '.' characters.
For example:
cutString "this.is.a.string" == ["this","is","a","string"]
cutString "" == [""]
cutString ".." == ["",""]
cutString ".this.is.a.string." == ["","this","is","a","string",""]
So far, I have this:
cutString [] = []
cutString [x]
| x == '.' = []
| otherwise = [[x]]
cutString (x:xs) = cutString [x] cutString xs
Which leaves out only the periods (this part is desirable), but also cuts the whole string up character by character.
Like this
["t","e","s","t","t","e","s","t","2"]
CodePudding user response:
You are only looking if the first character is a dot for a string with exactly one element. You should check this for each element. In case the first character is a dot, we yield an empty string followed by the rest of the groups that are generated through recursion. In case the character is not a dot, we prepend the first sublist of the recursive call, like:
cutString :: String -> [String]
cutString [] = [""]
cutString ('.':xs) = "" : cutString xs
cutString (x:xs) = let ~(y:ys) = cutString xs in (x:y) : ys