FoldPair

FoldPair[f,y0,list]

gives the last element of FoldPairList[f,y0,list].

FoldPair[f,y0,list,g]

gives the last element of FoldPairList[f,y0,list,g].

FoldPair[f,{a0,a1,a2,}]

is equivalent to FoldPair[f,a0,{a1,a2,}].

Details

  • The function f in FoldPair[f,y0,list] always takes two arguments and is expected to return a pair {x,y} of expressions.
  • The expression expr in FoldPair[f,y0,expr] can be any nonatomic expression, with any head.
  • You can use Throw to exit from FoldPair before it is finished.

Examples

open allclose all

Basic Examples  (3)

Scope  (1)

Use a list in the third argument:

Use an arbitrary head in the third argument:

Generalizations & Extensions  (1)

Use Throw to exit a FoldPair:

Applications  (2)

Find the last sublist of an arbitrary partition of a list:

A discrete-time state-space system is represented using a state equation and output equation . Simulate such a system to get the final output:

Properties & Relations  (2)

FoldPair returns the last element of the list returned by FoldPairList:

FoldPair does not return by default the remainder of the process:

Use Last in the fourth argument to get the remainder:

Use Identity in the fourth argument to get both:

Possible Issues  (2)

The function in the first argument of FoldPair must return a pair of expressions:

FoldPairList[f,y0,{}] returns an empty list, with no last element:

Therefore FoldPair[f,y0,{}] stays unevaluated:

Introduced in 2015
 (10.2)