# Iterating a Module

I have a Module that can act on other Modules, which is given as

Kk[EE,p,z1,z2,z3,K_func]


and I am trying to iterate it, such that

Kk[EE,p,z1,z2,z3,Kk[EE,#2,z2,z3,z4,Kk[EE,#1,z3,z4,z5....]&]&]


for n many times. The problem is that the previous iteration needs to be a function of p and the values of z change during each iteration or shift by one, so I cannot simply use the Nest function.

This works manually if I define functions, for example, for the 2nd iteration:

KK[p_]:=KKKK[EE,z3,z4,p] (This is the initial function)

KK[p_]:=Kk[EE,p,z2,z3,z4,KK]

KK[p_]:=Kk[EE,p,z1,z2,z3,KK]


But, I want this in a Do loop. So, my current code is:

vz = {EE/2, 12, 11, 5};

vz = Reverse[vz];
lz = Length[vz];

kk[p_] := KKKK[EE, p, vz[], vz[]]; (Define the initial function)

Do[
ki = i - 1;
v1 = i - 1;
v2 = i;
v3 = i + 1;
kk[i][p_] := Kk[EE, p, v3, v2, v1, kk[ki]];
,{i, 2, 3, 1}];


But this doesn't seem to work. Any suggestions? Apologies if I'm a bit ignorant, this is my first time using Stack Exchange!

• I don't see why Nest wouldn't work: Nest[Kk[E,p,#]&,KKKK[E,p],n] – Lukas Lang May 10 '18 at 14:42
• I should have also added that Kk is a function of 3 other values, which can be called z1,z2,z3. These values change with each iteration, so I don't think Nest can be used – Jordan May 10 '18 at 14:47
• Then please add this information to the question. Also, having global variables in mathematica (or anywhere really) is generally a bad idea – Lukas Lang May 10 '18 at 14:48
• Okay, thank you for you advice! – Jordan May 10 '18 at 15:07
• Have you looked at Fold? Together with Partition, it should be able to give you what you want – Lukas Lang May 10 '18 at 15:13