News for hackers. Stuff that matters.
Re:Notes on Haskell: What's Wrong with the For Loop by Erwin on Wednesday March 09, 2011 @05:14AM (#2304472)
I've seen it used for a long time. The only problem with it is the quadratic time complexity: it's equivalent to ((N0 + N1) + N2) + N3 ... so you copy the same elements over and over again as the sum builtin doesn't know to use anything but the + operator to build each element. (I vaguely recall some discussion about making it more efficient on sequences of sequences).

A more efficient version would be: def sumseq(l): r = []; map(r.extend,l); return r

which would also work on any type of sequence as input, even if mixed. A quick test shows the quadratic time complexity being noticeable at about 128 items, where sumseq is 10x as fast and 1751x as fast at 16384 items.

There are no comments on this comment yet.