I believe some of the respondents of this question have missed the broader implication of the fold
function as an abstract tool. Yes, sum
can do the same thing for a list of integers, but this is a trivial case. fold
is more generic. It is useful when you have a sequence of data structures of varying shape and want to cleanly express an aggregation. So instead of having to build up a for
loop with an aggregate variable and manually recompute it each time, a fold
function (or the Python version, which reduce
appears to correspond to) allows the programmer to express the intent of the aggregation much more plainly by simply providing two things: