[Haskell-beginners] Precedence of Infix Operators in Do Syntax

Avery Robinson avery at averyrobinson.name
Wed Nov 23 05:03:51 CET 2011


Hello,

I was reading this block of code from
http://jasani.org/2008/02/18/command-line-haskell-and-error-handling-examples/

main = do
  m <- hGetContents stdin
  nums <- mapM readM . lines $ m
  print (sum nums)
  `catch` (\e -> hPutStrLn stderr ("couldn't sum lines: " ++ show e))

readM :: (Monad m, Read a) => String -> m a
readM s | [x] <- parse = return x
        | otherwise    = fail $ "Failed to parse \"" ++ s ++ "\" as a
number."
  where
    parse = [x | (x,_) <- reads s]

I don't understand how line 5 works. I thought that the do notation there
would be the same as:

main = hGetContents stdin >>= \m ->
       mapM readM . lines $ m >>= \nums ->
       print (sum nums) >>
       `catch` (\e -> hPutStrLn stderr ("couldn't sum lines: " ++ show e))

But I don't understand how that makes any sense with that infix `catch`. It
looks like catch doesn't even have a first argument. Please enlighten me.

Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20111122/b5ee87d7/attachment.htm>


More information about the Beginners mailing list