Proposal: strictify foldl'

Niklas Hambüchen mail at nh2.me
Mon Nov 3 17:35:30 UTC 2014


On 03/11/14 18:16, Henning Thielemann wrote:
> If 'seq' is faster than 'deepseq' it leaves things unevaluated and thus
> builds up unevaluated expressions, right?

Yes.

What I mean is you could write a function function (using repeated
foldl' inside) that is written such that its return value is fully
evaluated (by carefully using just as many seqs as necessary for that).

If foldl' did a full deepseq on its return value, then this function
would seq the same data more times than necessary.

I will try to give a (probably not so excellent) example: Let's say you
want to `fmap (*2)` over a tree and implement it in recursive `foldl'
(:)` style over the children of each node. Then deepseq in foldl' would
trigger a full traversal of every subtree in the tree, adding a (*n)
runtime factor.

Yes, in this case you could probably just use laziness all well, using
only the current foldl' and do deepseq outside, but I can imagine that
there are cases similar to my example where that doesn't work, and were
you want explicit seq control (but I can't come up with a perfect
example for this yet).


More information about the Libraries mailing list