# Generating lists where new elements depend on the previous element

I am interested in using some type of list or table in-built Mathematica function to generate a list, where the elements of the new list not only depends on the input values of a previously defined list of triplets which I denote as 'a', but also depends on the output value of the previous element of the new list. In this sense I will provide the list of triplets, the function f which takes the triplets as inputs, and the first element of the new list. Then the list should generate itself.

I provide a simple example, not the actual problem, where I use the % symbol in Mathematica to assign the previous cell output to the next iteration. But of course this is not efficient for long triplet lists. Can anyone advise on a more efficient way of doing this type of problem? Many thanks.

a = {{0, 1, 0}, {0, 1, 0}, {0, 1, 0}, {1, 0, 0}, {0, 0, 1}};

f[x_, a1_, a2_, a3_] := (Cos[x])^a1*(Sin[x])^a2*(x)^a3;
Element1 = Cos[x];
(f[x, a[[1]][[1]], a[[1]][[2]], a[[1]][[3]]]*(%))
(f[x, a[[2]][[1]], a[[2]][[2]], a[[2]][[3]]]*(%))
(f[x, a[[3]][[1]], a[[3]][[2]], a[[3]][[3]]]*(%))
(f[x, a[[4]][[1]], a[[4]][[2]], a[[4]][[3]]]*(%))
(f[x, a[[5]][[1]], a[[5]][[2]], a[[5]][[3]]]*(%))

NewList = {Cos[x], Cos[x]  Sin[x], Cos[x]  Sin[x]^2,
Cos[x]  Sin[x]^3, Cos[x]^2  Sin[x]^3, x  Cos[x]^2  Sin[x]^3}
$$$$

• In the first step, how do you get Cos[x] to convert to Cos[x] Sin[x]?
– Syed
Mar 28 at 6:47
• @Syed The function (f[x, a[[1]][[1]], a[[1]][[2]], a[[1]][[3]]]*(%)) where (%) is the output Cos[x] from the previous line. Mar 28 at 7:07

This is precisely what FoldList does:

a = {{0, 1, 0}, {0, 1, 0}, {0, 1, 0}, {1, 0, 0}, {0, 0, 1}};

f[x_, {a1_, a2_, a3_}] = Cos[x]^a1 * Sin[x]^a2 * x^a3;

FoldList[f[x, #2]*#1 &, Cos[x], a]
(*    {Cos[x], Cos[x] Sin[x], Cos[x] Sin[x]^2, Cos[x] Sin[x]^3,
Cos[x]^2 Sin[x]^3, x Cos[x]^2 Sin[x]^3}                     *)


If I understand your question correctly, then your function f does not agree with your output. In the first step, x==Element1==Cos[x] and {a1,a2,a3}== {0,1,0}. Then your function evaluates to:

f[Cos[x], {0, 1, 0}]

Sin[Cos[x]]


However, you claim it is: Cos[x].

Nevertheless, proceeding with the given function f:

a = {{0, 1, 0}, {0, 1, 0}, {0, 1, 0}, {1, 0, 0}, {0, 0, 1}};
f[x_, {a1_, a2_, a3_}] := (Cos[x])^a1*(Sin[x])^a2*(x)^a3;
Element1 = Cos[x];

tmp = Element1;
Map[
(tmp = f[tmp, #]) &
, a]

{Sin[Cos[x]], Sin[Sin[Cos[x]]], Sin[Sin[Sin[Cos[x]]]],
Cos[Sin[Sin[Sin[Cos[x]]]]], Cos[Sin[Sin[Sin[Cos[x]]]]]}
`