1
$\begingroup$

Given an operator with two argumentsf[#1,#2]& and some positive (evaluated) integer n, I want to generate the operator f[...f[f[#,1],2]...,n]&.

I triedComposition[Sequence@@Table[f[#,i]&,{i,1,n}]], but Table does not return what I would expect when the expression used is an operator. More precisely, Table[f[#,i]&,{i,1,n}] returns {f[#1,i]&,...,f[#1,i]}, instead of {f[#1,1]&,...,f[#1,n]&}.

A solution to my original problem is thus found if given f[#1,#2]& and n I could generate {f[#1,1]&,...,f[#1,n]&}.

Any help?

$\endgroup$
3
  • 2
    $\begingroup$ Fold[f,#,Range[5]]& ? $\endgroup$
    – cvgmt
    Mar 11 at 11:57
  • $\begingroup$ That's it. I feel stupid for asking now. Thanks! $\endgroup$
    – atabler
    Mar 11 at 12:04
  • $\begingroup$ Fold is powerful,and I am not so familiar with it. $\endgroup$
    – cvgmt
    Mar 11 at 12:06
2
$\begingroup$
Fold[f,#,Range[5]]&

We have given the answer to this question, here we just add another example which surprise me and indicate that Fold is so powerful.

Fold[Sqrt[#1] + #2 &,Reverse@Range[10]]

$$ 1+\sqrt{2+\sqrt{3+\sqrt{4+\sqrt{5+\sqrt{6+\sqrt{7+\sqrt{8+\sqrt{9+\sqrt{10}}}}}}}}}$$

$\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.