[Haskell-beginners] A first try

David Place d at vidplace.com
Mon Jun 27 15:48:00 CEST 2011

On Jun 27, 2011, at 9:32 AM, Heinrich Apfelmus wrote:

> Note that my version of  withFile  has a different type and replaces hGetContents  entirely.
> Regardless, in both cases the intention is that the file contents is evaluated completely *inside* the scope of  withFile . In particular, a correct way to write your example would be
>    print10 = withFile' "/usr/share/dict/words" $ print . take 10
> (This is completely analogous to how Iteratees require you to process a file. The difference is that Iteratees expresses demand for the characters inside a file via  >>=  whereas lazy IO expresses demand via `seq` .)

So, the type of your withFile would be

withFile :: FilePath -> IOMode -> (Handle -> IO ()) -> IO ()

completely prohibiting return values from withFile?  Sounds good.  In that case, you don't need to change the behavior of withFile at all, just its type.

It's hGetContents that bothers me.  I have found that friends who I have  convinced to learn Haskell always trip over this right away.  They want to write a simple program that processes a file and immediately get tangled up with lazy IO and looking up the definition of deepseq.  It's kind of embarrassing.  Sadly, they never pick creating beautiful, incremental lazy algorithms with sharing as their "Hello World" projects.

David Place   
Owner, Panpipes Ho! LLC
d at vidplace.com

More information about the Beginners mailing list