3

I'm working through Write You a Haskell, and I'm on the part where he implements "Nanoparsec", a Haskell parser from first principles. I'm getting stuck on the Alternative instance of the parser, specifically the some and many implementations:

instance Alternative Parser where
  -- empty = ...
  -- (<|>) p q = ...
  some v = some_v
    where
      many_v = some_v <|> pure []
      some_v = (:) <$> v <*> many_v
  many v = many_v
    where
      many_v = some_v <|> pure []
      some_v = (:) <$> v <*> many_v

I have no idea what these two functions are doing. From what I can see, some takes a parser, applies it, and concatenates the result until the input is used up. But many looks like it does the same thing. What's happening here?

duplode
  • 32,686
  • 7
  • 74
  • 141
dopatraman
  • 12,737
  • 26
  • 82
  • 143

0 Answers0