I am trying to write a recursive function in Mathematica whose argument is a list. If the list is of length 1, it returns a value. If not, the function breaks it down into several smaller lists according to some rules and the function is then evaluated on those lists. Here is my code :
f[u_] :=
(Print["u : ", u];
If[Length[u] == 1,
Subscript[T, u[[1]]] - Subscript[\[Lambda], 1]^u[[1]],
v = SetPartitions[Length[u]];
aux[v_] := Sum[u[[v[[i]]]], {i, 1, Length[v]}];
res = Map[aux, v, {2}];
res = Drop[res, -1];
Print[res];
Product[Subscript[T, u[[i]]], {i, 1, Length[u]}] -
Sum[f[res[[i]]], {i, 1, Length[res]}]]
)
It works fine for
f[{1, 2}]
or
f[{3}]
but it does not work anymore when the list is of length 3 or more, like for example
f[{1,1,2}].
Here is the error message I get :
f[{1, 1, 2}]
u : {1,1,2}
{{4},{1,3},{2,2},{3,1}}
u : {4}
u : {1,3}
{{4}}
u : {4}
Part::partw: Part 3 of {{4}} does not exist. >>
u : {{4}}[[3]]
{{{{7}}}}
u : {{{7}}}
Part::partw: Part 4 of {{{{7}}}} does not exist. >>
u : {{{{7}}}}[[4]]
{{{{{{11}}}}}}
u : {{{{{11}}}}}
Does anyone have an idea what to do ? I guess it has something to do with the variables res being overwritten, but I don't know how to get round the problem....
Thank you !