Yousry Abdallah <[EMAIL PROTECTED]> writes:

> the following program produces a space leak:

Yes, it does.  GHC's definition of findIndices is the following:

        findIndices      :: (a -> Bool) -> [a] -> [Int]
        findIndices p xs = [ i | (x,i) <- zip xs [0..], p x]

which has a problem: the large CAF created by [0..], which GHC
erroneously seems to pull out to the top level.  My definition of
findIndices would be:

        findIndices'      :: (a -> Bool) -> [a] -> [Int]
        findIndices' p xs = loop 0# p xs
          where
                loop n p [] = []
                loop n p (x:xs) | p x = I# n : loop (n +# 1#) p xs
                                | otherwise = loop (n +# 1#) p xs

See, much nicer :-)

The CAF problem will be fixed at some point in the future, we are
currently rewriting GHC's run-time system with support for garbage
collection of CAF's (amongst other things).

Cheers,
        Simon

-- 
Simon Marlow                                             [EMAIL PROTECTED]
University of Glasgow                       http://www.dcs.gla.ac.uk/~simonm/
finger for PGP public key

Reply via email to