HOME

TheInfoList



OR:

In
functional programming In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declarat ...
, fold (also termed reduce, accumulate, aggregate, compress, or inject) refers to a family of
higher-order function In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following: * takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itself ...
s that analyze a recursive data structure and through use of a given combining operation, recombine the results of recursively processing its constituent parts, building up a return value. Typically, a fold is presented with a combining function, a top
node In general, a node is a localized swelling (a "knot") or a point of intersection (a vertex). Node may refer to: In mathematics * Vertex (graph theory), a vertex in a mathematical graph *Vertex (geometry), a point where two or more curves, lines ...
of a
data structure In computer science, a data structure is a data organization and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the relationships amo ...
, and possibly some default values to be used under certain conditions. The fold then proceeds to combine elements of the data structure's
hierarchy A hierarchy (from Ancient Greek, Greek: , from , 'president of sacred rites') is an arrangement of items (objects, names, values, categories, etc.) that are represented as being "above", "below", or "at the same level as" one another. Hierarchy ...
, using the function in a systematic way. Folds are in a sense dual to unfolds, which take a ''seed'' value and apply a function corecursively to decide how to progressively construct a corecursive data structure, whereas a fold recursively breaks that structure down, replacing it with the results of applying a combining function at each node on its terminal values and the recursive results ( catamorphism, versus anamorphism of unfolds).


As structural transformations

Folds can be regarded as consistently replacing the structural components of a data structure with functions and values. Lists, for example, are built up in many functional languages from two primitives: any list is either an empty list, commonly called ''nil''  ([]), or is constructed by prefixing an element in front of another list, creating what is called a ''cons'' 
node In general, a node is a localized swelling (a "knot") or a point of intersection (a vertex). Node may refer to: In mathematics * Vertex (graph theory), a vertex in a mathematical graph *Vertex (geometry), a point where two or more curves, lines ...
Cons(X1,Cons(X2,Cons(...(Cons(Xn,nil))))) ), resulting from application of a cons function (written down as a colon (:) in Haskell). One can view a fold on lists as ''replacing''  the ''nil'' at the end of the list with a specific value, and ''replacing'' each ''cons'' with a specific function. These replacements can be viewed as a diagram: There's another way to perform the structural transformation in a consistent manner, with the order of the two links of each node flipped when fed into the combining function: These pictures illustrate ''right'' and ''left'' fold of a list visually. They also highlight the fact that foldr (:) [] is the identity function on lists (a ''shallow copy'' in Lisp (programming language), Lisp parlance), as replacing ''cons'' with cons and ''nil'' with nil will not change the result. The left fold diagram suggests an easy way to reverse a list, foldl (flip (:)) []. Note that the parameters to cons must be flipped, because the element to add is now the right hand parameter of the combining function. Another easy result to see from this vantage-point is to write the higher-order map function in terms of foldr, by composing the function to act on the elements with cons, as: map f = foldr ((:) . f) [] where the period (.) is an operator denoting Function composition (computer science), function composition. This way of looking at things provides a simple route to designing fold-like functions on other algebraic data types and structures, like various sorts of trees. One writes a function which recursively replaces the constructors of the datatype with provided functions, and any constant values of the type with provided values. Such a function is generally referred to as a catamorphism.


On lists

The folding of the list ,2,3,4,5/code> with the addition operator would result in 15, the sum of the elements of the list ,2,3,4,5/code>. To a rough approximation, one can think of this fold as replacing the commas in the list with the + operation, giving 1 + 2 + 3 + 4 + 5. In the example above, + is an associative operation, so the final result will be the same regardless of parenthesization, although the specific way in which it is calculated will be different. In the general case of non-associative binary functions, the order in which the elements are combined may influence the final result's value. On lists, there are two obvious ways to carry this out: either by combining the first element with the result of recursively combining the rest (called a right fold), or by combining the result of recursively combining all elements but the last one, with the last element (called a left fold). This corresponds to a binary ''operator'' being either right-associative or left-associative, in Haskell's or
Prolog Prolog is a logic programming language that has its origins in artificial intelligence, automated theorem proving, and computational linguistics. Prolog has its roots in first-order logic, a formal logic. Unlike many other programming language ...
's terminology. With a right fold, the sum would be parenthesized as 1 + (2 + (3 + (4 + 5))), whereas with a left fold it would be parenthesized as (((1 + 2) + 3) + 4) + 5. In practice, it is convenient and natural to have an initial value which in the case of a right fold is used when one reaches the end of the list, and in the case of a left fold is what is initially combined with the first element of the list. In the example above, the value 0 (the additive identity) would be chosen as an initial value, giving 1 + (2 + (3 + (4 + (5 + 0)))) for the right fold, and ((((0 + 1) + 2) + 3) + 4) + 5 for the left fold. For multiplication, an initial choice of 0 wouldn't work: 0 * 1 * 2 * 3 * 4 * 5 = 0. The
identity element In mathematics, an identity element or neutral element of a binary operation is an element that leaves unchanged every element when the operation is applied. For example, 0 is an identity element of the addition of real numbers. This concept is use ...
for multiplication is 1. This would give us the outcome 1 * 1 * 2 * 3 * 4 * 5 = 120 = 5!.


Linear vs. tree-like folds

The use of an initial value is necessary when the combining function ''f''  is asymmetrical in its types (e.g. a → b → b), i.e. when the type of its result is different from the type of the list's elements. Then an initial value must be used, with the same type as that of ''f'' 's result, for a ''linear'' chain of applications to be possible. Whether it will be left- or right-oriented will be determined by the types expected of its arguments by the combining function. If it is the second argument that must be of the same type as the result, then ''f''  could be seen as a binary operation that ''associates on the right'', and vice versa. When the function is a
magma Magma () is the molten or semi-molten natural material from which all igneous rocks are formed. Magma (sometimes colloquially but incorrectly referred to as ''lava'') is found beneath the surface of the Earth, and evidence of magmatism has also ...
, i.e. symmetrical in its types (a → a → a), and the result type is the same as the list elements' type, the parentheses may be placed in arbitrary fashion thus creating a
binary tree In computer science, a binary tree is a tree data structure in which each node has at most two children, referred to as the ''left child'' and the ''right child''. That is, it is a ''k''-ary tree with . A recursive definition using set theor ...
of nested sub-expressions, e.g., ((1 + 2) + (3 + 4)) + 5. If the binary operation ''f''  is associative this value will be well-defined, i.e., same for any parenthesization, although the operational details of how it is calculated will be different. This can have significant impact on efficiency if ''f''  is non-strict. Whereas linear folds are node-oriented and operate in a consistent manner for each
node In general, a node is a localized swelling (a "knot") or a point of intersection (a vertex). Node may refer to: In mathematics * Vertex (graph theory), a vertex in a mathematical graph *Vertex (geometry), a point where two or more curves, lines ...
of a
list A list is a Set (mathematics), set of discrete items of information collected and set forth in some format for utility, entertainment, or other purposes. A list may be memorialized in any number of ways, including existing only in the mind of t ...
, tree-like folds are whole-list oriented and operate in a consistent manner across ''groups'' of nodes.


Special folds for non-empty lists

One often wants to choose the
identity element In mathematics, an identity element or neutral element of a binary operation is an element that leaves unchanged every element when the operation is applied. For example, 0 is an identity element of the addition of real numbers. This concept is use ...
of the operation ''f'' as the initial value ''z''. When no initial value seems appropriate, for example, when one wants to fold the function which computes the maximum of its two parameters over a non-empty list to get the maximum element of the list, there are variants of foldr and foldl which use the last and first element of the list respectively as the initial value. In Haskell and several other languages, these are called foldr1 and foldl1, the 1 making reference to the automatic provision of an initial element, and the fact that the lists they are applied to must have at least one element. These folds use type-symmetrical binary operation: the types of both its arguments, and its result, must be the same. Richard Bird in his 2010 book proposesRichard Bird, "Pearls of Functional Algorithm Design", Cambridge University Press 2010, , p. 42 "a general fold function on non-empty lists" foldrn which transforms its last element, by applying an additional argument function to it, into a value of the result type before starting the folding itself, and is thus able to use type-asymmetrical binary operation like the regular foldr to produce a result of type different from the list's elements type.


Implementation


Linear folds

Using Haskell as an example, foldl and foldr can be formulated in a few equations. foldl :: (b -> a -> b) -> b -> -> b foldl f z [] = z foldl f z (x:xs) = foldl f (f z x) xs If the list is empty, the result is the initial value. If not, fold the tail of the list using as new initial value the result of applying f to the old initial value and the first element. foldr :: (a -> b -> b) -> b -> -> b foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs) If the list is empty, the result is the initial value z. If not, apply f to the first element and the result of folding the rest.


Tree-like folds

Lists can be folded over in a tree-like fashion, both for finite and for indefinitely defined lists: foldt f z [] = z foldt f z = f x z foldt f z xs = foldt f z (pairs f xs) foldi f z [] = z foldi f z (x:xs) = f x (foldi f z (pairs f xs)) pairs f (x:y:t) = f x y : pairs f t pairs _ t = t In the case of foldi function, to avoid its runaway evaluation on ''indefinitely'' defined lists the function f must ''not always'' demand its second argument's value, at least not all of it, or not immediately (see
example Example may refer to: * ''exempli gratia'' (e.g.), usually read out in English as "for example" * .example, reserved as a domain name that may not be installed as a top-level domain of the Internet ** example.com, example.net, example.org, an ...
below).


Folds for non-empty lists

foldl1 f = x foldl1 f (x:y:xs) = foldl1 f (f x y : xs) foldr1 f = x foldr1 f (x:xs) = f x (foldr1 f xs) foldt1 f = x foldt1 f (x:y:xs) = foldt1 f (f x y : pairs f xs) foldi1 f = x foldi1 f (x:xs) = f x (foldi1 f (pairs f xs))


Evaluation order considerations

In the presence of lazy, or non-strict evaluation, foldr will immediately return the application of ''f'' to the head of the list and the recursive case of folding over the rest of the list. Thus, if ''f'' is able to produce some part of its result without reference to the recursive case on its "right" i.e., in its ''second'' argument, and the rest of the result is never demanded, then the recursion will stop (e.g., ). This allows right folds to operate on infinite lists. By contrast, foldl will immediately call itself with new parameters until it reaches the end of the list. This tail recursion can be efficiently compiled as a loop, but can't deal with infinite lists at all — it will recurse forever in an infinite loop. Having reached the end of the list, an ''expression'' is in effect built by foldl of nested left-deepening f-applications, which is then presented to the caller to be evaluated. Were the function f to refer to its second argument first here, and be able to produce some part of its result without reference to the recursive case (here, on its ''left'' i.e., in its ''first'' argument), then the recursion would stop. This means that while foldr recurses ''on the right'', it allows for a lazy combining function to inspect list's elements from the left; and conversely, while foldl recurses ''on the left'', it allows for a lazy combining function to inspect list's elements from the right, if it so chooses (e.g., ). Reversing a list is also tail-recursive (it can be implemented using ). On ''finite'' lists, that means that left-fold and reverse can be composed to perform a right fold in a tail-recursive way (cf.  ), with a modification to the function f so it reverses the order of its arguments (i.e., ), tail-recursively building a representation of expression that right-fold would build. The extraneous intermediate list structure can be eliminated with the continuation-passing style technique, ; similarly, ( flip is only needed in languages like Haskell with its flipped order of arguments to the combining function of foldl unlike e.g., in Scheme where the same order of arguments is used for combining functions to both foldl and ). Another technical point is that, in the case of left folds using lazy evaluation, the new initial parameter is not being evaluated before the recursive call is made. This can lead to stack overflows when one reaches the end of the list and tries to evaluate the resulting potentially gigantic expression. For this reason, such languages often provide a stricter variant of left folding which forces the evaluation of the initial parameter before making the recursive call. In Haskell this is the foldl' (note the apostrophe, pronounced 'prime') function in the Data.List library (one needs to be aware of the fact though that forcing a value built with a lazy data constructor won't force its constituents automatically by itself). Combined with tail recursion, such folds approach the efficiency of loops, ensuring constant space operation, when lazy evaluation of the final result is impossible or undesirable.


Examples

Using a Haskell interpreter, the structural transformations which fold functions perform can be illustrated by constructing a string: λ> foldr (\x y -> concat (",x,"+",y,")" "0" (map show ..13 "(1+(2+(3+(4+(5+(6+(7+(8+(9+(10+(11+(12+(13+0)))))))))))))" λ> foldl (\x y -> concat (",x,"+",y,")" "0" (map show ..13 "(((((((((((((0+1)+2)+3)+4)+5)+6)+7)+8)+9)+10)+11)+12)+13)" λ> foldt (\x y -> concat (",x,"+",y,")" "0" (map show ..13 "(((((1+2)+(3+4))+((5+6)+(7+8)))+(((9+10)+(11+12))+13))+0)" λ> foldi (\x y -> concat (",x,"+",y,")" "0" (map show ..13 "(1+((2+3)+(((4+5)+(6+7))+((((8+9)+(10+11))+(12+13))+0))))" Infinite tree-like folding is demonstrated e.g., in recursive primes production by unbounded sieve of Eratosthenes in Haskell: primes = 2 : _Y ((3 :) . minus ,7... foldi (\(x:xs) ys -> x : union xs ys) [] . map (\p-> [p*p, p*p+2*p..])) _Y g = g (_Y g) -- = g . g . g . g . ... where the function Haskell features#union, union operates on ordered lists in a local manner to efficiently produce their
set union In set theory, the union (denoted by ∪) of a collection of sets is the set of all elements in the collection. It is one of the fundamental operations through which sets can be combined and related to each other. A refers to a union of ze ...
, and minus their set difference. A finite prefix of primes is concisely defined as a folding of set difference operation over the lists of enumerated multiples of integers, as primesTo n = foldl1 minus 2*x,3*x..n, x <- ..n For finite lists, e.g.,
merge sort In computer science, merge sort (also commonly spelled as mergesort and as ) is an efficient, general-purpose, and comparison sort, comparison-based sorting algorithm. Most implementations of merge sort are Sorting algorithm#Stability, stable, wh ...
(and its duplicates-removing variety, nubsort) could be easily defined using tree-like folding as mergesort xs = foldt merge [] [ , x <- xs] nubsort xs = foldt union [] [ , x <- xs] with the function merge a duplicates-preserving variant of union. Functions head and last could have been defined through folding as head = foldr (\x r -> x) (error "head: Empty list") last = foldl (\a x -> x) (error "last: Empty list")


In various languages


Universality

Fold is a polymorphic function. For any ''g'' having a definition g [] = v g (x:xs) = f x (g xs) then ''g'' can be expressed as g = foldr f v Also, in a lazy language with infinite lists, a fixed point combinator can be implemented via fold, proving that iterations can be reduced to folds: y f = foldr (\_ -> f) undefined (repeat undefined)


See also

* Aggregate function *
Iterated binary operation In mathematics, an iterated binary operation is an extension of a binary operation on a set ''S'' to a function on finite sequences of elements of ''S'' through repeated application. Common examples include the extension of the addition operation ...
* Catamorphism, a generalization of fold *
Homomorphism In algebra, a homomorphism is a morphism, structure-preserving map (mathematics), map between two algebraic structures of the same type (such as two group (mathematics), groups, two ring (mathematics), rings, or two vector spaces). The word ''homo ...
* Map (higher-order function) * Prefix sum * Recursive data type * Reduction operator * Structural recursion


References

{{Reflist


External links


"Higher order functions — map, fold and filter"



"Fold in Tcl"

"Constructing List Homomorphism from Left and Right Folds"

"The magic foldr"
Higher-order functions Recursion Programming language comparisons Articles with example Haskell code Articles with example Scheme (programming language) code Iteration in programming