mail at nh2.me
Tue Sep 11 21:24:30 CEST 2012
On 11/09/12 19:57, Henning Thielemann wrote:
> However, it might be that you filter out some elements and thus consume
> only some of the elements. In this case it might be better to have a
Exactly. But the even more problematic case is when *you* don't do
anything with it, but write a library for others. You don't know what
they are going to do with the list you give them, and they don't know
(and shouldn't need to know) about the implementation detail that you
are using scanl - they only see the type, e.g. [Int], and can easily
fall into the trap of calling last on that.
I give a real-world example:
I wrote a small statistics library that calculates moving sums,
averages, standard deviations and so on. You give it the infinite list,
it could give you the infinite list of moving sums of size n (sums of
successive n elements).
movingWindowSum :: Int -> [Int] -> [Int]
Somewhere in movingWindowSum I use "scanl (+) 0". If the user of my
library calls last on that, which is not unexpected for my use case, or
is only interested in certain parts of that list, he gets a space leak
due to my implementation detail.
So I either have to disclose my implementation detail or hand-write a
scanl' into my library.
As a side note:
Data.List.sum [1..100000000] eats all my memory in my ghci.
Why does it use foldl, not foldl'?
More information about the Libraries