Haskell isn't garbage collecting head list?

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 like the list to be garbage collected, since we consume it, so this program uses only read-only memory. But when I compile and run it with

ghc Main.hs -O2 -o Main

I see that he uses memory more and more. How to convince Haskell of GC consumable list items?

+5
source share
1 answer

ListT in transformers does not work or does not work in constant space. 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 downloaded pipes-4.1.4 today, which relaxes runListT so as not to require mzero at the end, so this will be simple:

 -- Requires `pipes-4.1.4` import Pipes main = runListT (do x <- Select (each [0..1000000000]) lift (print x) ) 
+10
source

Source: https://habr.com/ru/post/1207546/


All Articles