Reputation: 3633
Consider the following program:
module Main where
import Control.Monad.List
main = runListT $ do
x <- ListT $ return $ [0..1000000000]
lift $ print x
Ideally, we would want the list to be garbage collected as we consume it, so that this program only uses constant memory. But when I compile and run it with
ghc Main.hs -O2 -o Main
I see that it keep using more and more memory. How do I convince Haskell to GC the consumed elements of the list?
Upvotes: 5
Views: 129
Reputation: 35089
The ListT
in transformers
doesn't stream or run in constant space. The ListT
in pipes
does!
import Control.Monad (mzero)
import Pipes
main = runListT (do
x <- Select (each [0..1000000000])
lift (print x)
mzero )
I also just uploaded pipes-4.1.4
today, which relaxes runListT
to not require the mzero
at the end, so then it would just be:
-- Requires `pipes-4.1.4`
import Pipes
main = runListT (do
x <- Select (each [0..1000000000])
lift (print x) )
Upvotes: 10