# FoldLink

The ** FoldLink** is a kind of FunctionLink that implements the reduction of repeated terms. It is used by the ArithmeticLink, a private base class for both PlusLink and TimesLink to consolidate repeated NumberNodes.

`FoldLink`

s can be reduced with the `cog-execute!`

function.
Users should never use the FoldLink atom directly; rather, it is a point in the type hierarchy that provides tooling and functions for other atom types.

### Overview

FoldLinks implement the concept of the fold functor in computer science.

The reduction of FoldLinks proceeds by grouping together similar terms under the fold operation. Thus, for example, the reduction of

(PlusLink (NumberNode 1) (NumberNode 2) (NumberNode 3))

yields

(NumberNode 6)

because the number nodes are all of the same type, and addition is defined on number nodes.

### General comments

The FoldLink reduction routine attempts to be generic, by assuming only that the list to be folded is associative, and not commutative. Thus, arithmetic reduction also makes use of sort phase, so that numbers, variables, and general terms are grouped next to one-another.

The reduction routine for FoldLink is written in C++, but its surprising complexity suggests that perhaps implementing reduction in C++ is not a wise choice. See the cog-reduce! page for additional comments on reduction design.