module Has (r,p,s) where
import Prelude ((==),Bool(..),otherwise,(||),Eq)
import qualified Data.List as L
filter :: (a -> Bool) -> [a] -> [a]
filter _pred [] = []
filter pred (x:xs)
| pred x = x : filter pred xs
| otherwise = filter pred xs
problem1: This filter
is copied from GHC
's library, but why it consumes a growing number of memory in contrast with the directly imported filter
, which consumes a constant number of memory.
elem :: (Eq a) => a -> [a] -> Bool
elem _ [] = False
elem x (y:ys) = x==y || elem x ys
problem2: This filter
is copied from GHC
's library, but why it consumes a growing number of memory like the directly used elem
, which also consumes a growing number of memory in contrast with the directly imported filter
.
r = L.filter (==1000000000000) [0..]
p = filter (==1000000000000) [0..]
s = 1000000000000 `elem` [0..]
GHC version:7.4.2 OS:Ubuntu 12.10 Compiled with -O2 to optimize
As the above filter
and elem
's definitions imply both p = filter (==1000000000000) [0..]
and s = 1000000000000 `elem` [0..]
's [0..]
should be garbage collected gradually. But both p
and s
consumes a growing number of memory. And r
which is defined with the directly imported filter
consumes a constant number of memory.
My question is why directly imported functions in GHC differ so much with functions I write with the source code copied from GHC Libraries. I wonded if there is something wrong with GHC?
I has a further question: The above code is abstracted from a project I writed, and the project also faces the problem of "consumes a growing number of memory, which should be garbage collected in theory". So I want to know that is there a way to find which variable takes up so much memory in GHC.
Thanks for your reading.