[Haskell] Re: state of HaXml?

Stefan Karrmann S.Karrmann at web.de
Thu Jan 4 12:31:26 EST 2007


My 2 cent:

Why does seq not help? See code below.

Simon Marlow (Thu, Jan 04, 2007 at 03:08:45PM +0000):
> and the original code was this:
> 
>   load fn = do handle <- IO.openFile fn IO.ReadMode
>                contents <- IO.hGetContents handle
>                IO.hClose handle
>                return $ XP.xmlParse fn contents
> 
> Sure, you can replace the openFile/hGetContents pair by readFile, but the 
> real problem is the presence of the hClose.  Removing that will solve your 
> problem (but note that you now have no control over when the file is 
> actually closed).

load fn = do handle <- IO.openFile fn IO.ReadMode
             contents <- IO.hGetContents handle
             let res = XP.xmlParse fn contents
             seq res $ IO.hClose handle -- maybe use deepSeq
             return $ res

load fn = do handle <- IO.openFile fn IO.ReadMode
             contents <- IO.hGetContents handle
             let len = length contents
             seq len $ IO.hClose handle
             return $ XP.xmlParse fn contents

Cheers,
-- 
Stefan



More information about the Haskell mailing list