Reputation: 2311
Problem Statement: Parse the string "AB"
in "ABCDFGABIJABGHA"
Constraint: Using ReadP
Expected Solution : (["AB","AB","AB"],"whatever is left")
Attempt:
getAll :: ReadP a -> ReadP [a]
getAll p = many loop
where
loop = p <|> (get >> loop)
readP_to_S (getAll $ string "AB") "ABCDFGABIJABGHA"
[([],"ABCDFGABIJABGHA"),(["AB"],"CDFGABIJABGHA"),(["AB","AB"],"IJABGHA"),(["AB"],"IJABGHA"),(["AB","AB","AB"],"GHA"),(["AB","AB"],"GHA"),(["AB","AB"],"GHA"),(["AB"],"GHA")]
I wanted the last state to be (["AB","AB","AB"],"GHA")
. Is it possible to do the same using ReadP
?
Upvotes: 0
Views: 66
Reputation: 1365
The problem is that you are doing a symmetric choice with <|>
. If you want your parser to match all the p
s without exception use the provided left-biased choice: <++
.
getAll :: ReadP a -> ReadP [a]
getAll p = many loop
where
loop = p <++ (get >> loop)
Upvotes: 1