Comment by s-zeng
7 hours ago
There's a relatively popular point of view amongst Haskell programmers that explicit recursion is the goto of functional programming; a dedicated folding or traversing function provides more clarity on what exactly the function intends to do. The extreme end of this is recursion schemes and memes like zygohistomorphic prepomorphisms, which are almost certainly overkill on lists proper but might be useful when traversing bigger recursive structures. Personally, I almost always prefer to find a monoid to map the list elements into, and use `fold :: (Monoid m, Foldable t) => t m -> m`. It's essentially the equivalent of using `sum()` instead of `reduce()` in python
Every time I spend a lot of thought on a problem I always go back to F-Algebras and F-CoAlgebras.
Most recursion is through fold and unfold
Folding 'fold' into contortions to use it as a generic List iterator does not "provide clarity".
In functional programming, there is no "generic list iterator", though. It matters what the output is, whether you need to look at the value of previous iterations, etc.