Space leak question

Will Ness will_n48 at yahoo.com
Sun Jul 24 12:18:02 CEST 2011


Hello,

in reformulation of a code with no space leak, the leak reappeares.

It takes near constant space to get at the n-th elt in the produced list here 
(http://ideone.com/fiifl):

 {-# OPTIONS_GHC -O2 -fno-cse #-}
 primes = 2 : ([3,5..] `minus`
                foldi (\(x:xs) -> (x:) . union xs)
                      [[x*x, x*x+2*x..] | x<- ys])
  where
   ys = 3 : ([5,7..] `minus`
                foldi (\(x:xs) -> (x:) . union xs)
                      [[x*x, x*x+2*x..] | x<- ys])
   foldi f (x:xs)  = f x (foldi f (pairs f xs))
   pairs f (x:y:t) = f x y : pairs f t


Here it doesn't (http://ideone.com/m744a):

 {-# OPTIONS_GHC -O2 -fno-cse #-}
 primes = 2 : g (fix g)
  where
   g = (3:) . ([5,7..] `minus`)
            . foldi (\(x:xs) -> (x:) . union xs)
            . map (\x-> [x*x, x*x+2*x..])

I expected the 2nd to be equivalent to the 1st.

In Hugs (Nov 2002) the reported stats and run times are very similar for the 
both versions.

Is this a GHC thing, or a language thing?

Thanks,




More information about the Glasgow-haskell-users mailing list