我遇到了来自Real World Haskell第8章的以下示例ghci交互的问题。在rampion对a related question的回答的帮助下,预期输出应为:
> :m +Text.Regex.Posix Data.ByteString.Char8
> getAllTextMatches $ pack "foo" =~ pack "o" :: [(Int, Int)]
[(1,1),(2,1)]
相反,我看到一个缺少的实例错误:
> getAllTextMatches $ pack "foo" =~ pack "o" :: [(Int, Int)]
No instance for (RegexContext
Regex ByteString (AllTextMatches [] (Int, Int)))
arising from a use of `=~'
Possible fix:
add an instance declaration for
(RegexContext Regex ByteString (AllTextMatches [] (Int, Int)))
In the second argument of `($)', namely `pack "foo" =~ pack "o"'
In the expression:
getAllTextMatches $ pack "foo" =~ pack "o" :: [(Int, Int)]
In an equation for `it':
it = getAllTextMatches $ pack "foo" =~ pack "o" :: [(Int, Int)]
是什么给了什么?
答案 0 :(得分:1)
从错误开始:
No instance for (RegexContext
Regex ByteString (AllTextMatches [] (Int, Int)))
查看范围内的内容,AllTextMatches
只定义了一个RegexContext
实例:
λ :info AllTextMatches
...
instance RegexLike a b => RegexContext a b (AllTextMatches [] b)
-- Defined in ‘Text.Regex.Base.Context’
范围内只有两个RegexLike
个实例:
λ :i RegexLike
...
instance RegexLike Regex String
-- Defined in ‘Text.Regex.Posix.String’
instance RegexLike Regex ByteString
-- Defined in ‘Text.Regex.Posix.ByteString’
由于我们在这里处理ByteString
,我们必须使用RegexLike Regex ByteString
实例,这样我们就可以在a
中推断b
和AllTextMatches
'RegexContext
的实例为Regex
和ByteString
,因此我们有:
RegexContext Regex ByteString (AllTextMatches [] ByteString)
如果我们要求:
λ getAllTextMatches (pack "foo" =~ pack "o") :: [ByteString]
["o","o"]
有效!
但这并没有给我们提供像(MatchOffset, MatchLength)
这样的东西,这有点令人失望。
我猜你正在尝试to run this example?
在那里的评论中有相当多的讨论,但是它的长期和短期是Text.Regex.Posix
的API自写入RWH以来显然发生了变化。不太令人惊讶,因为现在0.95.2
和when RWH was first published in November 2008 the latest version was 0.72.3
。
这些天,to get a list of all the match positions and offsets use getAllMatches
代替了。
λ getAllMatches (pack "foo" =~ pack "o") :: [(Int, Int)]
[(1,1),(2,1)]