我是 Haskell 的新手。
我想传递两个项目:
首先,作为字符串的数字列表 ["1","2","3","4"]
我想看看这些项目中的每一项是否出现在随机字符串中数字说 "14321"
例如 myFunction ["6","2","3"] "1234"
我的返回值将是一个可能类型,我想在其中返回 Just("the number found", "14321")
。
第一个返回参数是在列表中找到的数字,第二个是最初传递的字符串。
问题是,我只想返回匹配项的第一个实例作为可能,如果没有匹配项则什么都不返回。
我不确定如何进行这样的循环,检查是否匹配,如果匹配则返回并完成或继续转到列表中的下一项。
我有这个:
isMatch :: [String] -> String -> Maybe (String,String)
isMatch (x:xs) test | x `isInfixOf` test = Just (x, test)
| otherwise = Nothing
如果第一个匹配项为真,它应该可以工作,但如果不是,我如何让它继续到列表中的下一个匹配项?
提前致谢
最佳答案
让我音译您的代码:
If the first element in the list is found, then return it. Else abort.
但你想要的是
If the first element in the list is found, then return it. Else do the same thing, but with just the rest of the list.
还有
If the list is empty, nothing can be found, so return
Nothing
.
这对你有帮助吗?
关于haskell 如果为真则中断或继续循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23755808/