【发布时间】:2014-06-26 18:13:43
【问题描述】:
这个函数应该标记一个字符串:
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]]) 处添加实例声明
谢谢。
Ok 函数现在是:
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)
现在当我使用以下参数应用函数时: split "123,456,789" ',' [] 0 0 我得到结果 ["789", "456,789", "123"] 这里发生了什么?
【问题讨论】:
-
你应该包含你的函数类型签名。它使我们更容易说出您要做什么。但我最初的猜测是你只需要在倒数第二行加上括号
j+1。 -
我同意cdk的观点,这个函数可以重写得更清楚。但如果你只是想让这个工作,请
substr s i j = take (j-i) (drop i s)