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:


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

  • $\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 '19 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 '19 at 17:21

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]

  • $\begingroup$ that's nice! Thank you. $\endgroup$ – amator2357 Jun 6 '19 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.