# FoldList

FoldList[f,x,{a,b,}]

gives {x,f[x,a],f[f[x,a],b],}.

FoldList[f,{a,b,c,}]

gives {a,f[a,b],f[f[a,b],c],}.

FoldList[f]

represents an operator form of FoldList that can be applied to expressions.

# Details • FoldList[Plus,0,list] generates cumulative sums of the elements in list.
• With a length n list, FoldList generates a list of length . »
• The head of list in FoldList[f,x,list] need not be List.
• FoldList[f][list] is equivalent to FoldList[f,list].
• FoldList[f][x,list] is equivalent to FoldList[f,x,list].

# Examples

open allclose all

## Basic Examples(6)

Cumulative sums of the elements of the list:

Cumulative powers:

Start from the first element of the list:

Use the operator form of FoldList on one argument:

Use the operator form of FoldList on two arguments:

## Scope(2)

Perform two subsequent permutations:

Perform a chain of cross products:

## Generalizations & Extensions(2)

The head need not be List:

Fold to the right:

## Applications(15)

Compute successive factorials:

Compute products of successive primes:

Cumulative sums:

Build up a continued fraction:

Build up a nested polynomial (Horner form):

Build up a number from digits:

Form alternating sums:

Generate a random walk:

Find the running maximum of a list:

Find successively deeper parts in an expression:

Successively append to a list:

Build up a binary tree:

Build up a left-branching binary tree:

Make function compositions:

Compute a power using a power tree with successive squaring:

## Properties & Relations(5)

FoldList makes a list of length :

Rest gives a result of length :

Folding with an empty list does not apply the function at all:

Fold gives the last element of FoldList:

Functions that ignore their second argument give the same result as in NestList:

Accumulate is equivalent to FoldList with Plus:

## Possible Issues(1)

FoldList[f,{}] evaluates to an empty list, without a last element:

Therefore Fold[f,{}] stays unevaluated:

## Neat Examples(1)

Compute the minimum number of coins of different value needed to make up an amount :