A short while ago I was contemplating the question, whether every list function is expressible in terms of an application of foldr
.
The restriction is that there has to be exactly one application of foldr
and no additional workarounds.
For example, creating an intermediate pair of results and returning just one of its components or preprocessing the input list [a]
to a more enriched form [(a, b)]
are not allowed.
Baltasar Trancón Widemann has pointed out that this is not the case and that the avg
function that computes the average of a given list is a counterexample. This short text proves why this is the case.
The function we wish to define is
avg :: Fractional a => [a] -> Maybe a