title: Difficulty getting a Parsec parser to skip spaces correctly
body: <p>I'm new to Parsec (and to parsers in general), and I'm having some trouble with this parser I wrote:</p>
  
  <pre><code>list = char '(' *&gt; many (spaces *&gt; some letter) &lt;* spaces &lt;* char ')'
  </code></pre>
  
  <p>The idea is to parse lists in this format (I'm working up to s-expressions):</p>
  
  <pre><code>(firstElement secondElement thirdElement and so on)
  </code></pre>
  
  <p>I wrote this code to test it:</p>
  
  <pre><code>import Control.Applicative
  import Text.ParserCombinators.Parsec hiding (many)
  
  list = char '(' *&gt; many (spaces *&gt; some letter) &lt;* spaces &lt;* char ')'
  
  test s = do
    putStrLn $ "Testing " ++ show s ++ ":"
    parseTest list s
    putStrLn ""
  
  main = do
    test "()"
    test "(hello)"
    test "(hello world)"
    test "( hello world)"
    test "(hello world )"
    test "( )"
  </code></pre>
  
  <p>This is the output I get:</p>
  
  <pre><code>Testing "()":
  []
  
  Testing "(hello)":
  ["hello"]
  
  Testing "(hello world)":
  ["hello","world"]
  
  Testing "( hello world)":
  ["hello","world"]
  
  Testing "(hello world )":
  parse error at (line 1, column 14):
  unexpected ")"
  expecting space or letter
  
  Testing "( )":
  parse error at (line 1, column 3):
  unexpected ")"
  expecting space or letter
  </code></pre>
  
  <p>As you can see, it fails when there is white space between the last element of the list, and the closing <code>)</code>. I don't understand why the white space isn't consumed by the <code>spaces</code> I put in just before <code>&lt;* char ')'</code>. What silly mistake have I made?</p>