[Haskell] Probably a trivial thing for people knowing Haskell

Chris Kuklewicz haskell at list.mightyreason.com
Sat Oct 18 09:12:52 EDT 2008


The totals in "sum" and "count" are not computed until printed.  This is too 
lazy.  You start with '0' and (+) things to it, but never examine or force the 
value, so man many (+) thunks are built up in memory.

If you use bang patterns then the change can be made here, to !sum !count:

> 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)

This will force evaluation before every check_line call.

More information about the Haskell mailing list