有没有更好的方法来编写“string contains X”方法?

发布于 2024-12-25 03:43:07 字数 778 浏览 2 评论 0 原文

刚开始使用 Haskell 并意识到(据我所知)没有直接的方法来检查字符串以查看它是否包含较小的字符串。所以我想我应该尝试一下。

本质上,这个想法是检查两个字符串的大小是否相同并且相等。如果被检查的字符串较长,则递归地删除头部并再次运行检查,直到被检查的字符串长度相同。

其余的可能性我使用模式匹配来处理。这就是我想出的:

stringExists "" wordToCheckAgainst = False
stringExists wordToCheckFor "" = False
stringExists wordToCheckFor wordToCheckAgainst | length wordToCheckAgainst < length wordToCheckFor = False
                                               | length wordToCheckAgainst == length wordToCheckFor = wordToCheckAgainst == wordToCheckFor
                                               | take (length wordToCheckFor) wordToCheckAgainst == wordToCheckFor = True
                                               | otherwise = stringExists wordToCheckFor (tail wordToCheckAgainst)

Just stared using Haskell and realized (at far as I can tell) there is no direct way to check a string to see if it contains a smaller string. So I figured I'd just take a shot at it.

Essentially the idea was to check if the two strings were the same size and were equal. If the string being checked was longer, recursively lop of the head and run the check again until the string being checked was the same length.

The rest of the possibilities I used pattern matching to handle them. This is what I came up with:

stringExists "" wordToCheckAgainst = False
stringExists wordToCheckFor "" = False
stringExists wordToCheckFor wordToCheckAgainst | length wordToCheckAgainst < length wordToCheckFor = False
                                               | length wordToCheckAgainst == length wordToCheckFor = wordToCheckAgainst == wordToCheckFor
                                               | take (length wordToCheckFor) wordToCheckAgainst == wordToCheckFor = True
                                               | otherwise = stringExists wordToCheckFor (tail wordToCheckAgainst)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荭秂 2025-01-01 03:43:07

如果您在 Hoogle 中搜索您要查找的函数的签名(String -> ; String -> Bool) 你应该看到 isInfixOf 位居前列结果之一。

If you search Hoogle for the signature of the function you're looking for (String -> String -> Bool) you should see isInfixOf among the top results.

凶凌 2025-01-01 03:43:07

isInfixOf<来自 Data.List 的 /a> 肯定会解决问题,但是如果遇到更长的干草堆或反常的针,您应该考虑更高级的字符串匹配算法具有更好的平均和最坏情况复杂度。


1 考虑一个非常长的字符串,仅由 a 和一根开头有很多 a 的针组成,最后有一个 b结束。

isInfixOf from Data.List will surely solve the problem, however in case of longer haystacks or perverse¹ needles you should consider more advanced string matching algorithms with a much better average and worst case complexity.


¹ Consider a really long string consisting only of a's and a needle with a lot of a's at the beginning and one b at the end.

落墨 2025-01-01 03:43:07

考虑使用 text(text 在 Hackage 上,现在也是 Haskell Platform 的一部分),满足您的文本处理需求。它提供了 Unicode 文本类型,比内置的基于列表的 String 更节省时间和空间。对于字符串搜索,text实现一个 基于 Boyer-Moore 的算法,它比 Data.List.isInfixOf 使用的朴素方法具有更好的复杂性。

使用示例:

Prelude> :s -XOverloadedStrings
Prelude> import qualified Data.Text as T
Prelude Data.Text> T.breakOnAll "abc" "defabcged"
[("def","abcged")]
Prelude Data.Text> T.isInfixOf "abc" "defabcged"
True

Consider using the text package(text on Hackage, now also part of Haskell Platform) for your text-processing needs. It provides a Unicode text type, which is more time- and space-efficient than the built-in list-based String. For string search, the text package implements a Boyer-Moore-based algorithm, which has better complexity than the naïve method used by Data.List.isInfixOf.

Usage example:

Prelude> :s -XOverloadedStrings
Prelude> import qualified Data.Text as T
Prelude Data.Text> T.breakOnAll "abc" "defabcged"
[("def","abcged")]
Prelude Data.Text> T.isInfixOf "abc" "defabcged"
True
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文