3
$\begingroup$

I have a function, call it F[a_List,b_,c_,d_] which takes in 4 arguments. Now I want to compose this function with itself, where the number of compositions I want to perform is given by the length of a. For that I thought I could use Fold, but I am not sure how to set it up. There is something else. At each step of Fold, one of my arguments changes, namely achanges and also at each step the output of applying F replaces b. For instance, say a is a list of length 4. I would like to get the final output of:

F[a[[4]],F[a[[3]],F[a[[2]],F[a[[1]],b,c,d],c,d],c,d],c,d]

Can I do that using Fold? Or is there some other, hopefully more efficient way?

$\endgroup$
  • $\begingroup$ @MichaelE2 I believe I do not, b is being successively replaced by the output of applying F as we go through the composition. Does that make sense? $\endgroup$ – amator2357 Jun 6 at 13:12
  • 1
    $\begingroup$ Ah, my bad. I was on a phone and had trouble matching all the brackets by eye. $\endgroup$ – Michael E2 Jun 6 at 17:21
3
$\begingroup$

You can use Fold as follows:

Fold[F[#2, #, c, d] &, b, Array[a, 4]]

F[a[4], F[a[3], F[a[2], F[a[1], b, c, d], c, d], c, d], c, d]

$\endgroup$
  • $\begingroup$ that's nice! Thank you. $\endgroup$ – amator2357 Jun 6 at 14:35

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.