Proposal: Strict scanl, scanl1 and mapAccumL
Henning Thielemann
lemming at henning-thielemann.de
Mon Nov 12 13:25:56 CET 2012
On Mon, 12 Nov 2012, Bas van Dijk wrote:
> On 12 November 2012 11:17, Henning Thielemann
> <lemming at henning-thielemann.de> wrote:
>> My impression is that making 'seq' available as function without a typeclass
>> constraint was a step in the wrong direction. Then foldl' and friends were
>> the second step in the wrong direction and scanl' would be the third step.
>> For 'seq' I would propose we first start with a cleanly typed 'seq' and base
>> foldl' functions on this function instead of the built-in 'seq'.
>
> I think I agree that putting seq in a type class would have been
> better (especially since we can now derive things generically).
> However if we are going to change this we have to change a lot of code
> anyway. Why not add these strict functions now and change all of them
> later (incl. all the strict functions in containers) when the Seq type
> class gets added (if that ever happens...)?
I don't think of changing it. We could provide a package that exports "the
right 'seq'" and then encourage people to use this instead of Prelude.seq.
>> But I assume that most of the time where foldl' is used, actually a
>> deepseq-foldl' is meant. I have often seen foldl' in Haskell library
>> code that had not the intended effect since the accumulator was a lazy
>> pair or a Map.
>
> If your accumulator in foldl' is a lazy pair you should just seq the new
> elements before returning the pair. Not doing that is just silly. I
> don't see the need for deepseq there.
Silly or not, it is simple to forget it, especially if the foldl' was
written once with a simple accumulator type which is later changed to a
pair type.
> In case your accumulator is a Map you really want to use foldl'.
Why not a deepseq foldl'?
More information about the Libraries
mailing list