FoldList

The usage message of FoldList is shown below, and of course it's closely related to Fold. For a long time I wondered where something like this would have useful applications.

? FoldList

FoldList[f, x, {a, b, ... }] gives {x, f[x, a], f[f[x, a], b], ... }. More…

Jürgen Tischer used FoldList to solve a common problem in the MathGroup. The solution is given below which takes a list of angles and adds to, or subtracts from, each element a multiple of 360°  to minimize the jump from the previous element. This trick forms the heart of the SmoothPhase package that I have posted on MathSource.

lst = {5, 35, 65, 95, 125, 155, -175, -145, -115, -85, -55, -25} ; FoldList[(Round[(#1 - #2)/360] 360 + #2) &, First[lst], Rest[lst]]

{5, 35, 65, 95, 125, 155, 185, 215, 245, 275, 305, 335}

Another elegant application of FoldList is given in the section on Clever little programs.


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page