What do the "some" and "many" functions of Alternative do?

123 views Asked by At

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?

0

There are 0 answers