I'm trying to define a function that takes in a list (a1,a2,a3,...,an) and spits out (a1^1, a2^2, a3^3,...,an^n) using recursion. So far I've tried a lot of different things that don't work, and I would appreciate some help.
1 Answer
Using Mathematica's destructuring and list multiplication:
f[{a_, b___}] := Join[{a}, {b}*f[{b}]]
f[{a1, a2, a3, a4, a5}]
{a1, a2^2, a3^3, a4^4, a5^5}
In the comments you mention a second problem:
f2[{a_, b___}] := Join[{a, a}, f2[{b}]]
f2[{}] := {}
f2[{a1, a2, a3, a4, a5}]
{a1, a1, a2, a2, a3, a3, a4, a4, a5, a5}
Though practically this is better done in Mathematica without recursion:
Riffle[#, #] & @ {a1, a2, a3, a4, a5}
list^Range[Length[list]]
. $\endgroup$