4 ms·
>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
by tangus 3y ago
>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?
- galdor 3y agoThe 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.
- thedufer 3y agoThe 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`.