使用“.”将一个字符串剪切成一个字符串列表
Cut a string into a list of strings with '.'
我必须编写一个 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
我必须编写一个 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