# 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[1], a[2], a[3], ...}


and a function

y = f[x1, x2]


Is it possible to return a list like

{a[1], f[a[2],a[1]], f[a[3], f[a[2],a[1]]], ... ]


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[1] and a[2] and act on a[2]:

{a[1], f[a[2], a[1]], a[3], ...}


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

{a[1], f[a[2], a[1]], f[a[3], f[a[2], a[1]]], ...}


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?

## closed as off-topic by LLlAMnYP, LCarvalho, MarcoB, m_goldberg, garejJul 12 '17 at 6:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – LLlAMnYP, LCarvalho, MarcoB, m_goldberg, garej
If this question can be reworded to fit the rules in the help center, please edit the question.

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


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

• 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[5]]

(* {a[1], f[a[2], a[1]], f[a[3], f[a[2], a[1]]], f[a[4], f[a[3], f[a[2], a[1]]]],
f[a[5], f[a[4], f[a[3], f[a[2], a[1]]]]]} *)