gpt4 book ai didi

parsing - 什么时候可以使用读取进行模棱两可的解析?

转载 作者:行者123 更新时间:2023-12-04 04:29:48 24 4
gpt4 key购买 nike

Standard Haskell Classes页面上关于 reads :: (Read a) => String -> [(a,String)] 的内容如下:

Normally, a parser returns a singleton list, containing a value of type a that was read from the input string and the remaining string that follows what was parsed. If no parse was possible, however, the result is the empty list, and if there is more than one possible parse (an ambiguity), the resulting list contains more than one pair.



这种模棱两可在什么情况下或例子中表现出来?

最佳答案

import Text.Read

data Foo = Bar Int | Baz Double deriving Show

instance Read Foo where
readPrec = fmap Bar readPrec +++ fmap Baz readPrec

在本例中,解析器尝试解析 IntDouble .如果两者都可以解析,则解析器返回两个值。

导致:
> read "4" :: Foo
*** Exception: Prelude.read: ambiguous parse


> reads "4" :: [(Foo,String)]
[(Bar 4,""),(Baz 4.0,"")]

此处修复歧义的最简单方法是通过替换 choice operator +++ 来选择一个解析。与 selective choice <++ .

关于parsing - 什么时候可以使用读取进行模棱两可的解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35474497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com