16
$\begingroup$

In trawling through the documentation I've found the functions FoldPairList and SequenceFoldList. Examples are given of how to use FoldPairList:

  • Partition a list into sublists of different lengths using TakeDrop
  • Break an amount of money into bills of given values using QuotientRemainder
  • A discrete-time state-space system is represented using a state equation and output equation. (see wikipedia)

and for SequenceFoldList (both using Plus):

  • an implementation of the Fibonacci sequence
  • Show convergence to the exact solution when increasing the order of extrapolation.

These examples show some utility of these functions, but ultimately are not illuminating.

I'm looking for intuition behind these potentially useful functions.

It should be possible to view these functions through the paradigm of functional program (or Category Theory). In particular:

I'm no expert in any of this stuff. My guess is that these functions are included in Mathematica more as utility functions used internally for implementing other functions.

$\endgroup$
7
$\begingroup$

Fold[f, u, list] is Nest[f, u, n] with in addition the possibily to inject in f a value from list at each iteration.

That is to say, at each iteration, the input of f is feed with :

  • the preceding output of f exactly
  • a value of list.

FoldPair is designed to reinject a value that is different from the exact ouput of f.

That is to say, at each iteration, the input of f is feed with :

  • the first part of output of f. Indeed, the use of FoldPair assumes that f returns a pair of data.
  • a value of list.

It is easy to implement FoldPair with Fold, but the syntax is not straightforward to understand. The problem with FoldPair is that it is not very clear too.

FoldPair[{p[#1, #2], q[#1, #2]} &, u, {1, 2, 3, 4}]

p[q[q[q[u, 1], 2], 3], 4]

FoldPair implemented with Fold:

First @ Fold[{p[#1[[1]], #2], q[#1[[1]], #2]} &, {u,}, {1, 2, 3, 4}]

p[q[q[q[u, 1], 2], 3], 4]

That's the main ideas. The rest is a matter of ordering and desencapsulation of a pair of data in a List (choice beetween f[#1,#2] @@ {data1,data2} or f[#[[1]],#[[2]]] @ {data1,data2}.

FoldPairList is the list of the successives outputs that FoldPair would give for each sublist.

$\endgroup$
  • $\begingroup$ Thanks for that. You explanation "FoldPair is designed to reinject a value that is different from the exact output of f." seems to align most with the "state space" paradigm above. I'm still struggling to understand this function. $\endgroup$ – pdmclean Dec 16 '16 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.