[Haskell-beginners] A first try

David Place d at vidplace.com
Sun Jun 26 15:27:47 CEST 2011


On Jun 26, 2011, at 8:25 AM, Heinrich Apfelmus wrote:

> What about the combinator
> 
>  withFile :: FilePath -> (String -> a) -> IO a
>  withFile name f = bracket (openFile name ReadMode) hClose $ \h ->
>      evaluate . f =<< hGetContents h
> 
> ? It gives you the same thing as Iteratees - a way to apply a function to the contents of a file - without the need to rewrite all the existing list functions like  map , lines , words , and so on.

How would you, for instance, implement the program for counting all the words in a list of files that Oleg describes in his message?

> http://okmij.org/ftp/Haskell/Iteratee/Lazy-vs-correct.txt

Nested calls to withFile would require too many open handles.

____________________
David Place   
Owner, Panpipes Ho! LLC
http://panpipesho.com
d at vidplace.com



More information about the Beginners mailing list