[Haskell] Probably a trivial thing for people knowing Haskell

Paul Johnson paul at cogito.org.uk
Sun Oct 19 13:48:10 EDT 2008

Friedrich wrote:
> Ok to  be more concrete is the laziness "hidden" here?
> check_line line sum count =  
>     let match = matchRegex regexp line
>         in case match of
>                Just strs -> (sum + read (head strs) :: Integer, count + 1)
>                Nothing -> (sum, count)
Probably.  I would guess that "sum" and "count" are not being forced 
(i.e. evaluated) until the end of the computation, so instead of 
computing the result of "sum + read (head strs)" your program is just 
creating a thunk.  Because this is in a loop, you wind up with a chain 
of thunks.

Try putting turning the "Just" line into something like

   Just strs -> (seq sum $ sum + read (head strs) :: Integer, seq count 
$ count + 1)

That would be my guess.  But I could be wrong.


More information about the Haskell mailing list