[Haskell-cafe] [newbie] processing large logs

Eugene Crosser crosser at average.org
Sun May 14 05:59:52 EDT 2006


Udo Stenzel wrote:
> Eugene Crosser wrote:
>> This is my program:
>> ========
>> module Main where
>> import Data.Map
>> main = printMax . (foldr processLine empty) . lines =<< getContents
>> processLine line map = insertWith (\new old -> new + old) line 1 map
>> printMax map = putStrLn $ show $ foldWithKey
>>    (\key val accum -> if val > (snd accum) then (key,val) else accum)
>>        ("",0) map
>> ========

> You have to force the evaluation of intermediate results.  To do so, you
> have to replace foldr by foldl (foldr is just recursion, foldl is
> accumulator recursion),

Having read "Yet another Haskell tutorial" (note on p.20), doesn't foldl
have to read the complete list before it can start processing it
(beginning from the last element)?  As opposed to foldr that can fetch
elements one by one as they are needed?

Otherwise, point on strictness taken...  Well, apparently the whole deal
is even more weird than it happened at the first glance...

Eugene

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 254 bytes
Desc: OpenPGP digital signature
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20060514/45f5c1ce/signature.bin


More information about the Haskell-Cafe mailing list