To clean up even more, use StateT B.ByteString Maybe. Then the ByteString threading will be invisible, leading to just "liftM2 (,) readI readI", for suitably defined readI.<br><br><div class="gmail_quote">On Dec 23, 2007 6:45 AM, Bryan O'Sullivan <
<a href="mailto:bos@serpentine.com">bos@serpentine.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d">
Paulo J. Matos wrote:<br><br>> I guess the latter is the correct guess.<br><br></div>Good guess!<br><br>You can take advantage of the fact that the Maybe type is an instance of<br>the Monad typeclass to chain those computations together, getting rid of
<br>all of the explicit case analysis.<br><br>import qualified Data.ByteString.Char8 as B<br>import Data.Char (isDigit)<br><br>readTwoInts :: B.ByteString -> Maybe ((Int, Int), B.ByteString)<br>readTwoInts r = do<br> (a, s) <-
B.readInt . B.dropWhile (not . isDigit) $ r<br> (b, t) <- B.readInt . B.dropWhile (not . isDigit) $ s<br> return ((a, b), t)<br><br>Let's try that in ghci:<br><br> *Main> readTwoInts (B.pack "hello 256 299 remainder")
<br> Just ((256,299)," remainder")<br><br>The case analysis is still happening, it's just being done behind your<br>back by the (>>=) combinator, leaving your code much tidier. (And why<br>is there no explicit use of (>>=) above? Read about desugaring of "do"
<br>notation in the Haskell 98 report.)<br><br>The learning you'll want to do, to be able to reproduce code such as the<br>above, is about monads.<br><br>Cheers,<br><div><div></div><div class="Wj3C7c"><br> <b
<br>_______________________________________________<br>Haskell-Cafe mailing list<br><a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">
http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br></div></div></blockquote></div><br>