<br>Please see my questions inside comments {-- --} :<br>Thanks!<br><br>---<br>module Parser where<br><br>import Data.Char<br><br>type Parse a b = [a] -&gt; [(b, [a])]<br><br>{--<br>Newbie: a parser for a list of objects?<br>
<br>I am working with the section&nbsp; 17.5 &quot;Case study: parsing expressions&quot; of the book &quot;Haskell The Craft of Functional Programming&quot;, where a parser for a list of objects is defined. <br>I called this function pList in order to avoid confusion with &#39;list&#39; as a term for data structure.
<br>Please help me to understand how pList works (please, see the rest of the code at the end of this message):<br>--}<br>&nbsp;<br>pList :: Parse a b -&gt; Parse a [b]<br>pList p = (succeed []) `alt`<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((p &gt;*&gt; pList p) `build` (uncurry (:)))
<br><br>{--<br>First of all, I don&#39;t quite understand why there must be a choice (&#39;alt&#39;) between the function (&#39;succeed&#39;) that always returns an empty list and the other part? This results in adding [] to the front, why? 
<br><br>I thought that &#39;simplified&#39; version of pList should still work fine. Trying to prove this I wrote :<br>--}<br><br>pL1 :: Parse a b -&gt; Parse a [b]<br>pL1 p = (p &gt;*&gt; pL1 p) `build` (uncurry (:))<br>
<br>{--<br>Which, as expected, does not work correctly - just gives an empty list [] -&nbsp; but I don&#39;t understand why:<br><br>*Parser&gt; t1 &quot;12345&quot;<br>[]<br>*Parser&gt;<br><br>Also, I don&#39;t understand why the textbook version of pList gives this result: 
<br><br>*Parser&gt; test &quot;12345&quot;<br>[(&quot;&quot;,&quot;12345&quot;),(&quot;1&quot;,&quot;2345&quot;),(&quot;12&quot;,&quot;345&quot;),(&quot;123&quot;,&quot;45&quot;),(&quot;1234&quot;,&quot;5&quot;),(&quot;12345&quot;,&quot;&quot;)]
<br>*Parser&gt;<br><br>In particular, I don&#39;t understand where the first element (&quot;&quot;,&quot;12345&quot;) of the resulting list comes from? <br><br>I am trying to figure out how pList recursively unfolds. To my mind operators in the expression:
<br><br>(succeed []) `alt`((p &gt;*&gt; pList p) `build` (uncurry (:)))<br><br>has the following execution order:<br>1)&nbsp; &gt;*&gt;<br>2) &#39;build&#39;<br>3) &#39;alt&#39;<br><br>It seems that operation &gt;*&gt; should be done as many times as many elements the input list has. Right?
<br><br>Signature:<br><br>(&gt;*&gt;) :: Parse a b -&gt; Parse a c -&gt; Parse a (b, c)&nbsp; &nbsp;<br><br>implies that second argument of the expression:<br><br>p &gt;*&gt; pList p<br><br>should be of type &#39;Parse a c&#39; but in this application it is of type &#39;Parse a b -&gt; Parse a [b]&#39;
<br>How can that be?<br>How recursion termination conditinon is expressed in pList?<br>--}<br><br>none :: Parse a b<br>none inp = []<br><br>succeed :: b -&gt; Parse a b<br>succeed val inp = [(val, inp)]<br><br>suc:: b -&gt; [a] -&gt; [(b, [a])]
<br>suc val inp = [(val, inp)]<br><br>spot :: (a -&gt; Bool) -&gt; Parse a a<br>spot p [] = []<br>spot p (x:xs) <br>&nbsp;&nbsp;&nbsp;&nbsp; | p x = [(x, xs)]<br>&nbsp;&nbsp;&nbsp;&nbsp; | otherwise = []<br><br>alt :: Parse a b -&gt; Parse a b -&gt; Parse a b<br>
alt p1 p2 inp = p1 inp ++ p2 inp<br><br>bracket = spot (==&#39;(&#39;)<br>dash = spot (== &#39;-&#39;)<br>dig = spot isDigit<br>alpha = spot isAlpha<br><br>infixr 5 &gt;*&gt;<br><br>(&gt;*&gt;) :: Parse a b -&gt; Parse a c -&gt; Parse a (b, c)
<br>(&gt;*&gt;) p1 p2 inp = [((x,y), rem2) |(x, rem1) &lt;- p1 inp, (y, rem2) &lt;- p2 rem1]<br><br>build :: Parse a b -&gt; (b -&gt; c) -&gt; Parse a c<br>build p f inp = [ (f x, rem) | (x, rem) &lt;- p inp]<br><br>test = pList dig 
<br>t1 = pL1 dig <br><br>