[Haskell-cafe] Re: Need some help with an infinite list

Tom Pledger tom at pledger.gen.nz
Thu Jun 18 17:58:14 EDT 2009


Daniel Peebles <pumpkingod <at> gmail.com> writes:
> My solution attempted to exploit this using Numeric.showIntAtBase but
> failed because of the lack of 0 prefixes in the numbers. If you can
> find a simple way to fix it without duplicating the showIntAtBase
> code, I'd be interested!


Another advantage of the integer & base method is that it doesn't require a
fast-growing amount of memory to keep track of everything between two points in
the list.

e.g.

    Hugs> let mywords = "":[w++[ch] | w <- mywords, ch <- ['a'..'z']] in
mywords!!1000000
    "
    ERROR - Garbage collection fails to reclaim sufficient space

or

    Hugs> let sss = [""] : [ [ c:s | c <- ['a'..'z'], s <- ss ] | ss <- sss ] in
concat (tail sss) !! 1000000
    "
    ERROR - Garbage collection fails to reclaim sufficient space


I'm not sure offhand why Reid Barton's replicateM solution doesn't have the same
problem.  Is it a benefit of the lack of sharing Matthew Brecknell mentioned?

    Control.Monad> concatMap (\n -> replicateM n ['a'..'z']) [1..] !! 5000000
    "jxlks"

Regards,
Tom




More information about the Haskell-Cafe mailing list