[Haskell-cafe] External Sort: Sort a 10-million integer file
with just 256M of ram.
Albert Y. C. Lai
trebla at vex.net
Sat Oct 25 14:02:14 EDT 2008
Bulat Ziganshin wrote:
> Hello Thomas,
>
> Thursday, October 23, 2008, 8:41:04 PM, you wrote:
>
>> The problem is not fitting a 10^8 element list in memory, the
>> following works fine
>> (when compiled, though not in ghci):
>> t = putStrLn . show . last $ [1..10^8::Int]
>
> this runs in 1k space, thanks to lazy evaluation. 10^8-length list
> needs ~3gb of memory, it was discussed just a few days ago
To elaborate, t does this: compute the next item in the list, throw the
previous item away, until there is no next item, now we have something
to print. We never keep the whole list.
But this may keep the whole list:
u = (putStrLn . show . last $ list) >> (putStrLn . show . head $ list)
where list = [1..10^8::Int]
Have fun!
More information about the Haskell-Cafe
mailing list