[Haskell-cafe] Comments and suggestions on code

Luke Palmer lrpalmer at gmail.com
Sat Jan 12 20:09:07 EST 2008

On Jan 13, 2008 12:42 AM, Andre Nathan <andre at digirati.com.br> wrote:
> On Sat, 2008-01-12 at 16:00 -0800, Jonathan Cast wrote:
> > Wait, the last entry?  If you're just printing out the values, then
> > no --- those should have been garbage collected already.
> Won't they be garbage collected only after the last entry is used,
> though? Since getDirectoryEntries returns a list, won't its elements
> have to be kept until the list is not used anymore, i.e., after the last
> entry is processed?

Well, if you're using the list like this:

   map (\i -> f (list !! i)) [0..10000]

Then yes (it will not be garbage collected), but if you're using the list
like this:

   map f list

Then no (depending on the surroundings, of course).

Recall what a list is:

    data List a = Empty | Cons a (List a)

So once you process the first element and move to its tail, if there are no
references to the original list, only its tail, then the first element
will be garbage collected.  Which is why you can do things like:

    filter isPowerOfTwo [1..]

And get a list back without running out of memory when you get as high as 2^32.


More information about the Haskell-Cafe mailing list