[Haskell-cafe] Re: Processing of large files
Alexander Kogan
alexander at kogan.nnov.ru
Tue Nov 2 02:48:18 EST 2004
Hi!
> The list of tuples _does_ need to be strict. Beyond that, as Ketil Malde
> said, you should not use foldl -- instead, foldl' is the best version to
> use when you are recalculating the result every time a new list item is
> processed.
Thanks! I did the following:
merge [] x = [(x,1)]
merge (e@(a,b):xs) x | x == a = let b' = b+1 in b' `seq` (a,b'):xs
| otherwise = e : merge xs x
foldl' f z xs = lgo z xs
where
lgo z [] = z
lgo z (x:xs) = (lgo $! (f z x)) xs
procFile =
putStrLn .
show .
foldl' merge [] .
words
and it works!
But I wonder why the very useful function foldl' as I define it is not
included into Prelude? I think, many people work with large lists or
streams...
--
Alexander Kogan
Institute of Applied Physics
Russian Academy of Sciences
More information about the Haskell-Cafe
mailing list