Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I find it tedious to write out the following:

a = {1, 2, 3, 4};

Fold[f, First @ a, Rest @ a]
f[f[f[1, 2], 3], 4]

It becomes additionally cumbersome when working with held expressions, e.g.:

b = Hold[1 + 1, 2 + 2, 3 + 3, 4 + 4];

SetAttributes[g, HoldAll];

b /. _[x_, __] :> Fold[g, Unevaluated[x], Rest@b]
g[g[g[1 + 1, 2 + 2], 3 + 3], 4 + 4]

Likewise for FoldList. Is there a shorter syntax to achieve this?

share|improve this question

1 Answer 1

up vote 21 down vote accepted

Good News Everyone!

Two-parameter syntax for Fold and FoldList has been (silently) implemented!

Taliesin Beynon informs me that this was implemented in 2011, so check your older versions as well.

As Naitree notes this is now documented in 10.0.2:

enter image description here enter image description here

Fold[f, a]

FoldList[f, a]
f[f[f[1, 2], 3], 4]

{1, f[1, 2], f[f[1, 2], 3], f[f[f[1, 2], 3], 4]}

And the held expression example:

Fold[g, b]
g[g[g[1 + 1, 2 + 2], 3 + 3], 4 + 4]

For full integration we merely need to update SyntaxInformation to match the implementation:

Unprotect[Fold, FoldList]

SyntaxInformation[Fold]     = {"ArgumentsPattern" -> {_, _, _.}};
SyntaxInformation[FoldList] = {"ArgumentsPattern" -> {_, _., {__}}};

Protect[Fold, FoldList]

(We could also update usage Messages but as this is, for the time being, undocumented I prefer not.)

For older versions you may (further) add the functionality with:

Unprotect[Fold, FoldList]

Fold[f_, h_[a_, b__]]     := Fold[f, Unevaluated @ a, h @ b]
FoldList[f_, h_[a_, b__]] := FoldList[f, Unevaluated @ a, h @ b]

Protect[Fold, FoldList]

Special thanks to those who made this happen!

share|improve this answer
@Mr.Wizard, I thought we had a discussion about it a while back - perhaps I forgot to mention that I could sneak it in;-) I am glad you like it. –  user21 Jul 14 '14 at 8:33
@YiWang, I think, IIRC, it works from V9 onward. –  user21 Jul 14 '14 at 8:51
@Naitree I'll tell you what I do. I use a single line in init.m to load a package which contains my own functions and customizations. This makes it easy to "comment out" that line if needed. I create, update, and manage the .m package file from a companion Notebook (.nb) with Initialization Cells; when you save such a Notebook for the first time it should ask you if you want to create a Package. I save this Notebook/Package to the Applications directory under $UserBaseDirectory. (continued) –  Mr.Wizard Oct 25 '14 at 5:00
For modifications to System` symbols you do not need to create a new Context but for your own functions I believe that you should. However be aware that this can complicate access to your own functions from Cell contexts etc.; see: (9571) and (13293). Let me know if you run into any problems and I'll try to help, or just post a Question about it. –  Mr.Wizard Oct 25 '14 at 5:01
@Naitree By the way: be careful with modifying System functions. There is a reasonable school of thought that says you should never do it. There are usually alternatives, e.g. one could create a MyTools`Fold function and let this supersede the System`Fold function for interactive input. Also be aware that in some cases your modifications may be undone by internal (re)loading; see: (32531), (63656). –  Mr.Wizard Oct 25 '14 at 5:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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