The fold Function

Learn the higher-order function fold and see how it can be defined on lists, tree structures, and other algebraic datatypes.

The fold function on lists

Often, we need to combine list elements into a single value. For example, given a list, we might want to calculate the sum or the product of its elements. Such a computation pattern can be realized in the functional paradigm through a powerful function abstraction called fold.

Let’s define a function, sum_list, to calculate the sum of all numbers from a list of integers.

Get hands-on with 1400+ tech skills courses.