Yet another weakly defined bug report

Dean Herington heringto@cs.unc.edu
Thu, 13 Feb 2003 09:14:42 -0500


"Ketil Z. Malde" wrote:

> "Simon Marlow" <simonmar@microsoft.com> 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?

The string won't be evaluated until the finite map is demanded.  None of the
code you've shown so far makes that demand.

-- Dean