AdamSpitz•

Maybe this is a silly question, but what is foldl (as opposed to foldl') useful for?

(My vague understanding is that there are lots of uses for foldl' and lots of uses for foldr, but I don't think I've ever heard someone say, "...and you should make sure to use foldl here, not foldl'." And now here's this proposal that we actually remove one of the existing uses. So I'm confused about what foldl is even meant to be used for.)

10
phadej•

It is not silly. Short answer it is barely useful, e.g. for implementing last. https://www.well-typed.com/blog/2014/04/fixing-foldl/

11
generalbaguette•

Though you can also implement `last` with `foldr` relatively easily.

1
phadej•

But why to traverse the whole structure from the right, if you can just peek from the left.

Not all structures are single-list-like: Trees, SnocList, ...

1
generalbaguette•

Sure, for a SnocList, something like foldl is universal.

I thought we were talking about normal lists?

2
pdr77•

Seems sensible.

However min' in the where clause on line 1111 should probably be called max' instead.

5
TheKing01•

An example of lazy numbers in the wild is lazy peano numbers. A strict fold couldn't handle infinite lists of them correctly.

4
amalloy•

First, as discussed in the linked thread, the current sum already doesn't work for Peano numbers, because it uses foldl. You could change it to foldl' to make it work for strict numbers, or to foldr for lazy numbers; either would be an improvement.

Further, my understanding is that Peano numbers are more or less the only halfway-common use for lazy numbers, just like genericLength is more or less only a good idea for Peano numbers. I don't think it's great to be maximally general at the expense of the normal case, when the alternative is a faster general case but making things slightly awkward for people who are already trying to do awkward stuff like Peano numbers.

17
TheKing01•

I was thinking of the solution where sum uses foldr and sum' uses foldl'. The compiler would replace sum with sum' if the number type is strict.

3
bss03•

Even then, it's difficult to imagine sum or product of an infinite list of those to be incredibly useful. Comparing the result some ways would work, but other (lazy) comparisons would still just keep spinning looking for non-zero or non-unit (resp.) elements in the tail.

I'm a pretty stanch defender of laziness by default, but I'm pretty sure this change is for the better, and sufficiently lazy expressions that would be ruined by this strictness are rare enough to force them to implement lazySum / lazyProduct.

15
FindlaechOP•

Here the proposal is to have a fold that is strict in its accumulator.

1
FindlaechOP•

sigh of course I wrote the wrong mailing-list name in the title, it's of course the Libraries ML.

3