3
$\begingroup$

I have a function s, which has a list and two numbers as arguments and creates a list:

s[list,n1,n2],

and a function t, which also has a list and two numbers as arguments and creates a list:

t[list,n3,time] 

I want to create the following nested structure:

t[s[t[s[t[s[list,n1,n2],n2,time3],n2,n1],n1,time2],n1,n2],n2,time1]

But I want to call t and s not only 3 times, but k-times. Extra bonus would be a list which contains these nested structures for different values of k with $1\leq k \leq K$.

Any help is very much appreciated. Thank you!

$\endgroup$
4
$\begingroup$
desired = t[s[t[s[t[s[list, n1, n2], n2, time3], n2 ,n1], n1, time2], n1, n2], n2, time1]

k = 3;
n1n2 = {n2, n1};
times = Symbol["time" <> ToString[#]] & /@ Reverse[Range[k]];

Fold[t[s[#, ## & @@ (n1n2 = RotateRight[n1n2])], n1n2[[-1]], #2] &, list, times]

t[s[t[s[t[s[list, n1, n2], n2, time3], n2, n1], n1, time2], n1, n2], n2, time1]

% == desired

True

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.