这是两周前我们作业的0分问题所以如果看起来像家庭作业请不要翻转。 它应该做的是在另一个字符串中找到一个字符串(以它的顺序)。
isPart:: Eq a => [a] -> [a] -> Bool
isPart [] _ = True
isPart _ [] = False
isPart (x:xs) (y:ys)
| x == y = moveone xs ys
| otherwise = isPart (x:xs) ys
moveone:: Eq a => [a] -> [a] -> Bool
moveone [] _ = True
moveone (x:xs) (y:ys)
| x == y = moveone xs ys
| otherwise = isPart (x:xs) ys
所以,如果我输入isPart" house" "树"它应该是真的,但如果我输入isPart" house" " Treheouse"
我遇到的问题是,一旦Char被发现相等,它就会从moveone的列表中删除,所以一旦找到h,该函数将只查找" ouse"
我知道我无法保存字符串,以便返回它。请不要解决它,只是给我一些想法。
答案 0 :(得分:1)
你可以试试这个:
如果字符不同,首先从False
返回moveone
。
接下来,从isPart
而不是简单地调用moveone
添加替代方案:
| x == y = (moveone xs ys) || isPart (x:xs) ys
因为匹配可能在任何角色都失败,而不仅仅是第一个角色。
作为旁注,请在isPart
中添加x列表的模式别名:xList@(x:xs)
;否则你重建每个递归调用的列表。 (虽然你的编译器可能会优化它。)