[Haskell-cafe] Re: Processing of large files

Tomasz Zielonka t.zielonka at students.mimuw.edu.pl
Thu Nov 4 03:07:57 EST 2004

On Thu, Nov 04, 2004 at 10:37:25AM +0300, Alexander Kogan wrote:
> Hi!
> > How about this?
> >
> >   let a' = addToFM_C (+) a x 1 in
> >       maybe () (`seq` ()) (lookupFM a' x) `seq` a'
> >
> > It worked for me.
> Thank you. It works for me too, but I don't understand why and how ;-))
> Could you explain?

Scott's solution forces (lookupFM a' x) to head normal form (or is it
weak head normal form). This means that the value of type (Maybe v) is
evaluated as much that it is known whether it is Nothing, Just _ or _|_
(bottom). This is probably enough to evaluate the path from FiniteMap's
tree root to x. However (lookupFM a' x) in head normal form can be still
something like this:
    Just <unevaluated: 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1>
So we have to force the value at key 'x' also. My code uses maybe, which
may obfuscate things a bit. Here's another version, which should be
easier to understand:

    let a' = addToFM_C (+) a x 1 in
    case lookupFM a' x of
	Nothing -> a'
	Just v -> v `seq` a'

There are less seq's in this version, because matching (lookupFM a' x)
in 'case' will be enough to force it.

Best regards,

.signature: Too many levels of symbolic links

More information about the Haskell-Cafe mailing list