[Haskell-cafe] Comments on reading two ints off Bytestring

Paulo J. Matos pocm at soton.ac.uk
Sun Dec 23 07:32:15 EST 2007


Hello all,

It is either too difficult to get two integers of a bytestring, in
which case something should be done to ease the process or I should
learn much more Haskell. I guess the latter is the correct guess.

I have a bytestring containing two naturals. I was to get them as
efficiently as possible. Here's my code:
parseHeader :: BS.ByteString -> (Int, Int)
parseHeader bs =
        let first = BS.readInt $ BS.dropWhile (not . isDigit) bs
        in
          if(isNothing first)
          then
              error "Couldn't find first natural."
          else
              let second = BS.readInt $ BS.dropWhile (not . isDigit) $
snd $ fromJust first
              in
                if(isNothing second)
                then
                    error "Couldn't find second natural."
                else
                    (fst $ fromJust first, fst $ fromJust second)

This seems to work:
> parseHeader $ BS.pack "hello 252 359"
(252,359)

Is there a better way?

Cheers,

-- 
Paulo Jorge Matos - pocm at soton.ac.uk
http://www.personal.soton.ac.uk/pocm
PhD Student @ ECS
University of Southampton, UK


More information about the Haskell-Cafe mailing list