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

Isaac Dupree isaacdupree at charter.net
Mon Dec 24 18:52:08 EST 2007

Brandon S. Allbery KF8NH wrote:
> On Dec 24, 2007, at 13:18 , Isaac Dupree wrote:
>> Paulo J. Matos wrote:
>>> On Dec 23, 2007 12:44 PM, Isaac Dupree <isaacdupree at charter.net> wrote:
>>>> 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???
>> when the first one returns Nothing, the whole expression becomes 
>> Nothing without examining the later parts of computation (as Chaddaï 
>> said)
> One thng that's not obvious here is that pattern match failure 
> translates to a call to "fail", which in the definition of Monad for 
> Maybe becomes Nothing.
> (Hm.  Isaac:  I thought that translation only happened for the "do" 
> sugar, and in the direct case you must do it yourself or Haskell raises 
> the "incomplete pattern match" exception?)

Tuple-matching never fails (except for _|_) -- there's only one 
constructor.  In this case it's only the intrinsic failure of 
BS.readInt.  You're thinking of something like

    [a,b] <- readListOfInts foo
    return (a+b)
--readListOfInts is a function I made up :: String -> Maybe [Int]

which can fail
(1) if readListOfInts returns Nothing
(2) because of the do-notation, also if the list doesn't have exactly 
two elements in it.


More information about the Haskell-Cafe mailing list