此函数应标记字符串:
split s c xs i j =
if head s == c
then split s c (subStr s i j : xs) j (j + 1)
else if j == length s
then (subStr s i j) : xs
else split s c xs i j + 1
subStr s i j = take j(drop i s)
但是我收到以下错误信息: 没有(Num [[Char]])的实例,因为使用了' split' 可能的解决方法:在(Num [[Char]])
添加实例声明感谢。
好的功能现在是:
split s c xs i j =
if j == length s
then (subStr s i j) : xs
else if head (drop j s) == c
then split s c (subStr s i j : xs) (j + 1) (j + 1)
else split s c xs i (j + 1)
subStr s i j = take j(drop i s)
现在当我使用以下args应用该函数时:split" 123,456,789" ',' [] 0 0我得到结果[" 789"," 456,789"," 123"]这里发生了什么?
答案 0 :(得分:2)
问题在于else split s c xs i j + 1
。您似乎正在尝试为split
的结果添加1。您可能忘记了(j + 1)
我假设split
返回[String]
,是吗?
编辑:您的功能难以理解。奇怪的排序可能是因为你在第3行(subStr s i j) : xs
上预先添加了子串。
尝试使用takeWhile, dropWhile :: (a -> Bool) -> [a] -> [a]
重写您的功能,或者更好的是,使用ByteString
提供正确的字符串处理库
Data.ByteString.Char8.split :: (Char -> Bool) -> ByteString -> [ByteString]