Skip to content

Latest commit

 

History

History
31 lines (25 loc) · 824 Bytes

crazy_io.md

File metadata and controls

31 lines (25 loc) · 824 Bytes

Haskell crazy IO quiz

This is a collection of weird bugs in Haskell programs due to lazy IO. The answer to all these snippets is "don't use lazy IO". The quiz part is finding out what goes wrong.

Provided by Peaker in Freenode/#haskell:

-- May throw an exception
do isEqual <- ((== content) <$> readFile filename)
                    `catch` \SomeException {} -> return False
   unless isEqual $ writeFile filename content
-- Does not time out after 3 seconds
timeout (seconds 3) $ readFile "foo"
-- May print nothing or truncated result
do x <- withFile "foo" ReadMode $ \h -> ... hGetContents h ...
   print x
-- May throw an exception
-- BS = ByteString
do x <- BS.withFile "foo" ReadMode $ \h -> ... BS.hGetContents h ...
   print x