[Haskell-cafe] more functions to evaluate

Rodrigo Queiro overdrigzed at gmail.com
Sat Oct 13 08:43:38 EDT 2007

Dan: Sorry, I forgot to Reply to All.

On 12/10/2007, Dan Weston <westondan at imageworks.com> wrote:
> We don't want to make an intermediate list of zeroes and append, since
> that could be wasteful. Just keep adding a zero to the head of our list
> until it gets big enough. Our list is not copied (i.e. it is shared with
> the tail of the result) this way, saving making a copy during reverse.

It's actually much less efficient to create a big function that
prepends a list of zeroes than just to create that list of zeroes and
prepend it.

You will be much better of just using (replicate n e ++) than
(applyNtimes (e:) n).

Contrived benchmark:
Prelude> sum . map length $ [replicate i 0 ++ [1..10] | i <- [1..2000]]
(0.19 secs, 114581032 bytes)
Prelude> sum . map length $ [applyNtimes (0:) i [1..10] | i <- [1..2000]]
(2.51 secs, 242780204 bytes)

More information about the Haskell-Cafe mailing list