[Haskell-cafe] Lazy IO and closing of file handles

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Thu Mar 15 08:52:03 EDT 2007

Ketil Malde wrote:
> Bertram Felgenhauer wrote:
> >>> type Subject = String
> >>> data Email   = Email {from :: From, subject :: Subject} deriving Show
> >data Email   = Email {from :: !From, subject :: !Subject} deriving Show
> ...except that From and Subject are Strings, and thus the strictness 
> annotation only forces WHNF.  I.e., you also need to modify parseEmail 
> to force these.
> -k

You're right. Actually, the program will be strict enough if the From
header always precedes the Subject header (which was the case in my
tests), but that's not immediately obvious.

Modifying getHeader to force its result is the clean solution, say:

          getHeader = forceString . fromMaybe "N/A" . flip lookup headers
          forceString s = length s `seq` s

Having to rely on GC to close the fds quickly enough is another problem;
can this be solved on the library side, maybe by performing GCs when
running out of FDs?


More information about the Haskell-Cafe mailing list