haskell 如果为真则中断或继续循环

标签 haskell functional-programming

我是 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/

相关文章:

haskell - 无法使用 ghc-7.8 构建 lambdabot (@ haskell-src-exts)

ubuntu - 安装 pandoc 的问题

haskell - 为什么 MonadIO 是特定于 IO 的,而不是更通用的 MonadTrans?

haskell - 如何扩展 Haskell 类型实例?

performance - `modify` 什么时候复制向量?

functional-programming - 用无限流替换符号表达式

arrays - 使用元胞数组进行平方欧氏计算

javascript - lambda : Fold an object

javascript - 如何在 JavaScript 中使用 reduce 而不是 for 循环构建 contains 函数?

haskell - 如何查询默认方法的类型?