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

时间:2011-10-15 21:02:18

标签: haskell

我有这样的功能:

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)]

1 个答案:

答案 0 :(得分:5)

查看RegexLike类可用的the instances,您想要的值可能是AllMatches [] (MatchOffset, MatchLength)类型,它只是将元组列表(MatchOffset, MatchLength)包装到newtype中。然后可以使用getAllMatches函数访问该列表。所以你可以这样做:

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