News for hackers. Stuff that matters.
Re:Reduce vs. Fold in Common Lisp by tangus on Sunday June 04, 2023 @06:48PM (#36190290)
>Fold is also simpler than REDUCE since it does not have any special case, making it easier to reason about its behaviour.

If returning the initial value when the list is empty is considered a special case (or "surprising aspect") of REDUCE, then it's the same for FOLD, no?

Re:Reduce vs. Fold in Common Lisp by thedufer on Sunday June 04, 2023 @07:12PM (#36190443)
The initial value is optional for `reduce`, but required for `fold`. If you don't pass the initial value to `reduce`, and the sequence argument is empty, then `reduce` calls the function with no arguments, which is unique - in all other cases, it is called with two arguments. `fold` always calls its function argument with two arguments.

This shows up more clearly in statically-typed functional languages, where variadic functions like this are far less common. In that case, you typically see that `reduce` returns an option type, whereas `fold` does not. The types would look something like `fold :: (a -> b -> a) -> a -> List b -> a` vs `reduce :: (a -> a -> a) -> List a -> Option a`.

Re:Reduce vs. Fold in Common Lisp by galdor on Sunday June 04, 2023 @07:05PM (#36190405)
The most surprising aspect of REDUCE is the way the callback can be called depending on both the length of the list and the presence of absence of an initial value.