# Need a function that, given f, a function of two arguments, calls f on successive elements of the list and the result of the last evaluation of f [closed]

Suppose I have a list

{a, a, a, ...}


and a function

y = f[x1, x2]


Is it possible to return a list like

{a, f[a,a], f[a, f[a,a]], ... ]


This may look like a little bit confusing. Actually, it just applies the function across consecutive elements in this list in order.

1st time, take a and a and act on a:

{a, f[a, a], a, ...}


2nd time, take a and the new a and act on a:

{a, f[a, a], f[a, f[a, a]], ...}


In other words, although the problem is very similar to Span a function across several consecutive elements in a list, the order / history of the application matters.

Is there a way that it can be realized elegantly without using For loop?

FoldList[f[#2, #1]&, Array[a, 5]]


{a, f[a, a], f[a, f[a, a]], f[a, f[a, f[a, a]]], f[a, f[a, f[a, f[a, a]]]]}

• Thanks so much for the quick answer. I couldn't believe there exists exactly a function designed for this. – Andy Jul 10 '17 at 16:17
Map[
Reverse@Array[a, #] //. {a[x_], p : Repeated[a[_], {2, Infinity}]} :>
{a[x], {p}} /. List :> f &, Range]

(* {a, f[a, a], f[a, f[a, a]], f[a, f[a, f[a, a]]],
f[a, f[a, f[a, f[a, a]]]]} *)