如何存储此返回类型:[(x,y)]?

发布于 2024-12-10 09:57:54 字数 1048 浏览 0 评论 0原文

我有一个这样的函数:

example           :: [Char] -> [Char]
example myString  = ...................
                    where
                      pat        = "something"
                      returnList = myString =~ pat :: [(MatchOffset,MatchLength)]

我的问题是我不知道如何存储从调用 myString =~ pat :: [(MatchOffset,MatchLength)] 返回的值我

不能只需将其存储在单个变量名中,就像我在这里所做的那样,但我不确定如何存储它。

目前它给出了这个错误:

No instance for (RegexContext
                   Regex [Char] [(MatchOffset, MatchLength)])
  arising from a use of `=~'
Possible fix:
  add an instance declaration for
  (RegexContext Regex [Char] [(MatchOffset, MatchLength)])
In the expression: myString =~ pat :: [(MatchOffset, MatchLength)]
In an equation for `returnList':
    returnList = myString =~ pat :: [(MatchOffset, MatchLength)]
In an equation for `example':
    example myString
      = ....................
      where
          pat = "something"
          returnList = myString =~ pat :: [(MatchOffset, MatchLength)]

I have a function like this:

example           :: [Char] -> [Char]
example myString  = ...................
                    where
                      pat        = "something"
                      returnList = myString =~ pat :: [(MatchOffset,MatchLength)]

My problem is that I don't know how to store the values I get back from calling myString =~ pat :: [(MatchOffset,MatchLength)]

I can't just store it in a single variable name as I have done here, but I'm not sure how I do store it.

It currently gives this error:

No instance for (RegexContext
                   Regex [Char] [(MatchOffset, MatchLength)])
  arising from a use of `=~'
Possible fix:
  add an instance declaration for
  (RegexContext Regex [Char] [(MatchOffset, MatchLength)])
In the expression: myString =~ pat :: [(MatchOffset, MatchLength)]
In an equation for `returnList':
    returnList = myString =~ pat :: [(MatchOffset, MatchLength)]
In an equation for `example':
    example myString
      = ....................
      where
          pat = "something"
          returnList = myString =~ pat :: [(MatchOffset, MatchLength)]

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

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

发布评论

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

评论(1

时光礼记 2024-12-17 09:57:54

查看 实例可用于 RegexLike 类,您想要的值可能是 AllMatches [] (MatchOffset, MatchLength) 类型,它简单地包装了元组列表(MatchOffset, MatchLength) 为新类型。然后可以使用 getAllMatches 函数访问该列表。所以你可以这样做:

returnList = getAllMatches (myString =~ pat) :: [(MatchOffset,MatchLength)]

Looking at the instances available for class RegexLike, the value you want is probably of type AllMatches [] (MatchOffset, MatchLength), which simply wraps the list of tuples (MatchOffset, MatchLength) to a newtype. The list can then be accessed using the getAllMatches function. So you can do this:

returnList = getAllMatches (myString =~ pat) :: [(MatchOffset,MatchLength)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文