我必须编写一个 cutString::String -> [String]
函数,该函数必须将一个字符串剪切成一个字符串列表。它应该在'。字符。
例如:
cutString "this.is.a.string" == ["this","is","a","string"]
cutString "" == [""]
cutString ".." == ["",""]
cutString ".this.is.a.string." == ["","this","is","a","string",""]
到目前为止,我有这个:
cutString [] = []
cutString [x]
| x == '.' = []
| otherwise = [[x]]
cutString (x:xs) = cutString [x] ++ cutString xs
这只省略了句点(这部分是可取的),但也逐个字符地切割整个字符串。
像这样:
["t","e","s","t","t","e","s","t","2"]
最佳答案
您只是在查看第一个字符是否是一个点,表示具有一个元素的字符串。您应该检查每个元素。如果第一个字符是点,我们会生成一个空字符串,然后是通过递归生成的其余组。如果字符不是点,我们将递归调用的第一个子列表放在前面,例如:
cutString :: String -> [String]
cutString [] = [""]
cutString ('.':xs) = "" : cutString xs
cutString (x:xs) = let <strong>~(y:ys)</strong> = cutString xs in <strong>(x:y)</strong> : ys
关于list - 用 '.' 将一个字符串切割成一个字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70405363/