"Simon Marlow" <[EMAIL PROTECTED]> writes:

> > > -- | add data from a file to the histogram
> > > addFile :: FiniteMap String Int -> String -> IO (FiniteMap 
> > String Int)
> > > addFile fm name = do
> > >             x <- readFile name
> > >             return (addHist fm x)
> > > 
> > > -- | add data from all files in a directory to the histogram
> > > addDir :: FiniteMap String Int -> String -> IO (FiniteMap 
> > String Int)
> > > addDir fm dir = do
> > >           dc <- getDirectoryContents dir
> > >           fs <- filterM doesFileExist (map ((dir++"/")++) dc)
> > >           foldM addFile fm fs

> It's not possible to tell from this code whether the readFiles will be
> fully evaluated or not: it depends on how much evaluation addHist does,
> and to what extend the result FiniteMap is demanded, amongst other
> things.

Of course.  Never cut code, I suppose; I thought the parts my
understanding would be weakest would be the monadic stuff, not this: 

> addHist :: FiniteMap String Int -> String -> FiniteMap String Int
> addHist fm = foldl add1 fm . words
>    where add1 f w = 
>             case lookupFM f w of
>                                Just n -> addToFM f w (n+1)
>                                Nothing -> addToFM f w 1

I felt pretty sure that this would evaluate the string to the end.  Am
I wrong?
 
> These things are always tricky to understand, which is why I recommend
> not using lazy I/O.  File reading is not a pure operation: running out
> of file descriptors is a good counter-example.

Okay.  Perhaps renaming "readFile" to "unsafeReadFile"? :-)

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants
_______________________________________________
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell

Reply via email to