# Build a list from applying a recursive function on another list

I have one list a of length (n+1):

a={a,a,...,a[n]}


I wish to build a list by applying a function f, a recursive non-linear function, that depends on the value of a at both indexes i and (i-1) and on the previous value of the "under construction" list. The first value of list b is defined as b0. Here is the list I would like to get:

b={b0,f(b0,a,a),...,f(b[n-1],a[n-1],a[n]))}


The first and last element of list a won't change but I want to test several values of incrementation, therefore n (the length of vector a) will change.

I have tried using Table, Array, combining it with Module and I managed to call at least one specific value by its index from list a but I can't find a way to do the multiple manipulations as described above.

• Are a[i] numbers? Does the function f return a number? Or the same type of object as a[i]? Apr 19, 2017 at 20:22
• both a and f are functions (that will be numbers only specific values of parameters)
– Elsa
Apr 19, 2017 at 22:22

Here is one way:

avec = Array[a, 4, 0];
bvec = ConstantArray[b, Length[avec - 1]];
Do[
bvec[[i]] = f[bvec[[i - 1]], avec[[i - 1]], avec[[i]]]
, {i, 2, Length[avec]}
]
bvec


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

Or more functional:

bvec2 = FoldList[f[#1, Sequence @@ #2] &, b, Partition[avec, 2, 1]];
bvec == bvec2


True

In:

Clear[a, b, f, g, bs]
g[i_] := f[b[i - 1], a[i - 1], a[i]]
bs[n_] := Range[n] // MapThread[g, {#}] & // Join[{b}, #] &
bs


Out:

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