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

Isaac Dupree isaacdupree at charter.net
Mon Dec 24 13:18:29 EST 2007

Paulo J. Matos wrote:
> On Dec 23, 2007 12:44 PM, Isaac Dupree <isaacdupree at charter.net> wrote:
>> -- this should work too
>> parseHeader3 :: BS.ByteString -> Maybe (Int, Int)
>> --note accurate type signature, which helps us use Maybe failure-monad,
>> --although losing your separate error messages
> Oh gee, I just noticed that my type sig is in fact not correct. How
> come GHC doesn't complain?

well, it is correct for Haskell if you want program failure for parse 
failure... it's just not a _total_ function unless you use Maybe (which 
determines whether you can have the code that uses parseHeader decide 
what to do in the case of a failure)

>> parseHeader3 bs = do
>>    (x, rest) <- BS.readInt $ BS.dropWhile (not . isDigit) bs
>>    (y, _) <- BS.readInt $ BS.dropWhile (not . isDigit) rest
>>    return (x, y)
> What happens then if the first BS.readInt return Nothing???
>> --or to be clearer without syntactic sugar, that is
>> parseHeader3 bs =
>>    (BS.readInt $ BS.dropWhile (not . isDigit) bs)
>>      >>= \(x, rest) ->
>>             (BS.readInt $ BS.dropWhile (not . isDigit) rest)
>>                >>= \(y, _) ->
>>                       return (x, y)

when the first one returns Nothing, the whole expression becomes Nothing 
without examining the later parts of computation (as Chaddaï said)


More information about the Haskell-Cafe mailing list