title: How do you chain an arbitrarily long series of atomic parsers using applicatives?
body: <p>Let's say I have this parser type:</p>
  
  <pre><code>newtype Parser a = Parser { runParser :: String -&gt; Maybe (a, String) }
  </code></pre>
  
  <p>And this atomic parser unit:</p>
  
  <pre><code>satisfy :: ( Char -&gt; Bool ) -&gt; Parser Char
  satisfy g = Parser $ \stream -&gt; case stream of
      (x:xs) | g x -&gt; Just ( x, xs )
      otherwise    -&gt; Nothing
  </code></pre>
  
  <p>The parser implements these three interfaces:</p>
  
  <pre><code>instance Functor Parser where
      fmap g ( Parser p ) = Parser $ \xs0 -&gt; p xs0 &gt;&gt;= \(x,xs) -&gt; return ( g x, xs )
  
  
  instance Applicative Parser where
      pure a                      = Parser $ \xs0 -&gt; Just ( a, xs0 )
      (Parser p1) &lt;*&gt; (Parser p2) = Parser $ \xs0 -&gt; do
          (x1, xs1) &lt;- p1 xs0
          (x2, xs2) &lt;- p2 xs1
          return ( x1 x2, xs2 )
  
  instance Alternative Parser where
      empty                        = Parser $ const Nothing
      (Parser p1) &lt;|&gt; (Parser p2)  = Parser $ \ss -&gt; let ss1 = p1 ss in case ss1 of
          Nothing  -&gt; p2 ss
          _        -&gt; ss1
  </code></pre>
  
  <p>Now as I understand, I can now pop up to a higher level of abstraction and build more complex parsers by chaining <code>satisfy</code> using the applicative interface. In example:</p>
  
  <pre><code>-- | A parser that parses the first two chars in the stream if they are upper case
  uParser = satisfy isUpper
  parser1 = ( (:) &lt;$&gt; uParser ) &lt;*&gt; ( (\x -&gt; [x]) &lt;$&gt; uParser )
  runParser parser1 "HEllo" = Just ("HE","llo")
  runParser parser1 "Hello" = Nothing
  </code></pre>
  
  <p>This is great, now what if I want to structure a computation such that the parser parses all cap letters in the stream until it encounters a lowercase letter? Use case:</p>
  
  <pre><code>runParser idealParser "hello"             = Nothing
  runParser idealParser "HEllo"             = Just ("HE","llo")
  runParser idealParser "HELLOIAMnotincaps" = Just ("HELLOIAM", "notincaps")
  </code></pre>
  
  <p>How do I express this notion of non-determined length?</p>