考虑以下repl会话:
λ import Text.ParserCombinators.ReadP
λ x $$ y = readP_to_S x y
-- This auxiliary function makes things tidier.
λ many get $$ "abc"
[("","abc"),("a","bc"),("ab","c"),("abc","")]
-- This is reasonable.
λ fmap head (many get) $$ "abc"
[(*** Exception: Prelude.head: empty list
-- Wut?
λ fmap last (many get) $$ "abc"
[(*** Exception: Prelude.last: empty list
-- This works neither.
λ fmap id (many get) $$ "abc"
[("","abc"),("a","bc"),("ab","c"),("abc","")]
-- The list is there until I try to chop its head!
我的问题:
这里发生了什么?
如何提取单个(最好是最长的)解析结果?
P.S。我的目标是构建一个解析器组合器,它贪婪地返回给定解析器的重复应用程序。 (get
在这个例子中,但实际上我有一个更复杂的逻辑。)砍掉中间结果列表是我认为会做的一种方法,但我对任何方法都没问题,除了最好不要转换为ReadS
并返回。