Hello,
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.