I agree on all counts! The hex-handling logic here is so straightforward that it&#39;s hardly worth bothering with. In fact, my application&#39;s code as it stands looks very similar to what you wrote. I&#39;m really asking because I want to be more &quot;fluent&quot; in attoparsec.<div>

<div><br></div><div>So the question remains: is there a way to limit a parser to a finite chunk of input? Perhaps a way to run the &#39;take n&#39; parser on the input and then run another parser on its result? This smells like monadic behavior, but of course with different semantics than the Monad instance for Parser.</div>

<div><br></div><div>Mike S Craig<br>(908) 328 8030<br>
<br><br><div class="gmail_quote">On Fri, Sep 23, 2011 at 11:23 PM, Evan Laforge <span dir="ltr">&lt;<a href="mailto:qdunkan@gmail.com">qdunkan@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

BTW you probably want &#39;data Color = Color !Word8 !Word8 !Word8&#39;<br>
<div><div></div><div class="h5"><br>
On Fri, Sep 23, 2011 at 8:21 PM, Evan Laforge &lt;<a href="mailto:qdunkan@gmail.com">qdunkan@gmail.com</a>&gt; wrote:<br>
&gt; On Fri, Sep 23, 2011 at 8:04 PM, Michael Craig &lt;<a href="mailto:mkscrg@gmail.com">mkscrg@gmail.com</a>&gt; wrote:<br>
&gt;&gt; Suppose we want to parse a 24-bit hex color value:<br>
&gt;&gt; input :: ByteString<br>
&gt;&gt; input = &quot;af093c blah blah blah&quot;<br>
&gt;&gt; type Color = (Word8, Word8, Word8)<br>
&gt;&gt;<br>
&gt;&gt; Attoparsec.Char8 exports a nice hexadecimal parser, but it consumes all<br>
&gt;&gt; available hex-flavored input. I&#39;d like to make it consume exactly two bytes,<br>
&gt;&gt; so I could write my color parser like this:<br>
&gt;&gt;<br>
&gt;&gt; color :: Parser Color<br>
&gt;&gt; color = do<br>
&gt;&gt;     r &lt;- hex2<br>
&gt;&gt;     g &lt;- hex2<br>
&gt;&gt;     b &lt;- hex2<br>
&gt;&gt;     return $ Color (r, g, b)<br>
&gt;&gt; hex2 :: Parser Word8<br>
&gt;&gt; hex2 = ???<br>
&gt;&gt;<br>
&gt;&gt; So my question is &quot;how do I write hex2?&quot; I could easily rewrite hexadecimal,<br>
&gt;&gt; but it would be nicer to reuse its hex-handling logic.<br>
&gt;<br>
&gt; If it&#39;s easy enough to write inline, might as well do so.  And it&#39;s<br>
&gt; fun with Applicative :)<br>
&gt;<br>
&gt; hex2 = (+) &lt;$&gt; ((*16) &lt;$&gt; higit) &lt;*&gt; higit<br>
&gt; higit = subtract (fromEnum &#39;0&#39;) &lt;$&gt; satisfy isHexDigit<br>
&gt; color = Color &lt;$&gt; hex2 &lt;*&gt; hex2 &lt;*&gt; hex2<br>
&gt;<br>
</div></div></blockquote></div><br></div></div>