“=~"raise "No instance for (RegexContext Regex [Char] [String])"; [英] "=~" raise "No instance for (RegexContext Regex [Char] [String])"

查看:28
本文介绍了“=~"raise "No instance for (RegexContext Regex [Char] [String])";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

操作系统:MacOSX 10.7.1来自 brew 的 GHC 和 Haskell 平台.

OS: MacOSX 10.7.1 GHC and Haskell-platform from brew.

   GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
    Loading package ghc-prim ... linking ... done.
    Loading package integer-gmp ... linking ... done.
    Loading package base ... linking ... done.
    Loading package ffi-1.0 ... linking ... done.
    Prelude> :m +Text.Regex.Posix
    Prelude Text.Regex.Posix> "foo" =~ "o" :: [String]

    <interactive>:1:7:
        No instance for (RegexContext Regex [Char] [String])
          arising from a use of `=~'
        Possible fix:
          add an instance declaration for
          (RegexContext Regex [Char] [String])
        In the expression: "foo" =~ "o" :: [String]
        In an equation for `it': it = "foo" =~ "o" :: [String]
    Prelude Text.Regex.Posix> "foo" =~ "o" :: String
    Loading package array-0.3.0.2 ... linking ... done.
    Loading package bytestring-0.9.1.10 ... linking ... done.
    Loading package containers-0.4.0.0 ... linking ... done.
    Loading package transformers-0.2.2.0 ... linking ... done.
    Loading package mtl-2.0.1.0 ... linking ... done.
    Loading package regex-base-0.93.2 ... linking ... done.
    Loading package regex-posix-0.95.1 ... linking ... done.
    "o"

我相信库更新了.我认为 "foo" =~ "o" :: [String] 的输出是 ["o", "o"]

I believe libraries updated. And I think the output of "foo" =~ "o" :: [String] is ["o", "o"]

任何建议将不胜感激.

推荐答案

ghci> getAllTextMatches ("foo" =~ "o" :: AllTextMatches [] String)
["o", "o"]

我在 Haskell 中使用的正则表达式不多(我认为 Dan Burton 的回答 更为地道.

I haven't used regexes in Haskell much (which is I think Dan Burton's answer is more idiomatic).

所以我想出来的方法是我查看了您的类型错误 No instance for (RegexContext Regex [Char] [String]),然后弹出 ghci:

So the way I figured this out is I looked at your type error No instance for (RegexContext Regex [Char] [String]), and popped into ghci:

ghci> :t (=~)
(=~)
  :: (RegexMaker Regex CompOption ExecOption source,
      RegexContext Regex source1 target) =>
     source1 -> source -> target

所以 RegexContext Regex [Char] [String] 是一个包含 "foo" =~ "o" :: [String] 返回类型的类.因此,我查看了该类的哪些实例确实存在,以便找出允许的返回值:

So RegexContext Regex [Char] [String] is a class that includes the return type of "foo" =~ "o" :: [String]. So I looked to see what instances of this class did exist, so I could find out what the return value was allowed to be:

ghci> :i RegexContext
class RegexLike
        regex source => RegexContext regex source target where
  match :: regex -> source -> target
  matchM :: Monad m => regex -> source -> m target
        -- Defined in Text.Regex.Base.RegexLike
instance RegexContext Regex String String
  -- Defined in Text.Regex.Posix.String
instance RegexLike a b => RegexContext a b [[b]]
  -- Defined in Text.Regex.Base.Context
...
instance RegexLike a b => RegexContext a b (AllTextMatches [] b)
  -- Defined in Text.Regex.Base.Context
...

AllTextMatches 名称似乎表明了您要查找的内容,所以我检查了一下:

The AllTextMatches name seemed to indicate what you were looking for, so I checked that out:

ghci> :i AllTextMatches
newtype AllTextMatches f b
  = AllTextMatches {getAllTextMatches :: f b}
        -- Defined in Text.Regex.Base.RegexLike
instance RegexLike a b => RegexContext a b (AllTextMatches [] b)
  -- Defined in Text.Regex.Base.Context

所以这是用于提取所有文本匹配的类型,正如我所怀疑的.我需要做的就是表明我想要该类型的返回值.

So this was the type to use to extract all the text matches, as I suspected. All I needed to do was indicate that I wanted a return value of that type.

还要注意 [[b]] 的可能返回类型,我假设它返回一个包含每个完整匹配项及其所有子匹配项的列表:

Note also the possible return type of [[b]], which I assume returns a list of lists containing each complete match and all its submatches:

ghci> "foo" =~ "o" :: [[String]]
[["o"],["o"]]
ghci> "foo bar baz" =~ "[aeiou](.)" :: [[String]]
[["oo","o"],["ar","r"],["az","z"]]

所以也许这就是您要使用的类型,而不是 [String].当 [[String]] 存在时,我可以看到 [String] 有点模棱两可——应该 "foo bar baz" =~ "[aeiou](.)" :: [String] be fst("foo bar baz" =~ "[aeiou](.)" :: [[String]])map fst ("foo bar baz" =~ "[aeiou](.)" :: [[String]]).

So maybe that's the type you meant to use, instead of [String]. I could see [String] as being slightly ambiguous when [[String]] existed - should "foo bar baz" =~ "[aeiou](.)" :: [String] be fst ("foo bar baz" =~ "[aeiou](.)" :: [[String]]) or map fst ("foo bar baz" =~ "[aeiou](.)" :: [[String]]).

这篇关于“=~"raise "No instance for (RegexContext Regex [Char] [String])";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆