# FoldList with Composition and parameter

Good day to everyone!

I have two (just for simple example) functions and they compose a map:

m[t_]:=RotationMatrix[t].#&
s[k_]:={#[],#[]-k2 #[]^2}&
map=Composition[m[0.2],s]


If I use this map in NestList all works nice:

NestList[map,{0.01,0.001},1000]


But now I want to change the first argument t of m[t] at each (or with some step) iteration. For this I was hoping to use FoldList, but this does not work:

FoldList[Composition[m[0.2+#2]&,s],{0.01,0.001},Range[0.01,0.02,0.001]]


How can I use Composition in FoldList?

EDIT

I'm not sure can I add some more wishes or should I post another question.

To speed-up iterations, I can Compile functions:

mc=Compile[{t,{x,_Real,1}},{{Cos[t],-Sin[t]},{Sin[t],Cos[t]}}.x]
sc=Compile[{k2,{x,_Real,1}},{x[],x[]-k2 x[]^2}]


Using With technique that @kglr advised in comment I can write:

FoldList[With[{xx = #2},RightComposition[mc[.2+xx,#]&,sc[2,#]&]],
{0.01,0.001},Range[0.01,0.02,0.001]]


This works. But what confuses me is that #2 is in purple and Composition does not work when replaced RightComposition. What is the difference in this case between Composition and RightComposition? Am I using Compile correctly?

• you probably meant s[k2_]:=...?
– kglr
Apr 16, 2021 at 12:35
• FoldList[With[{xx = #2}, Composition[m[.2 + xx], s]@#] &,...]?
– kglr
Apr 16, 2021 at 12:49
• @kglr, yes, that works! You can post this as answer, and porbably add explanation of why direct using #2 fails. Apr 16, 2021 at 14:24
• @kglr, I edited question, could you give me some hints, or answers? Apr 16, 2021 at 15:49
• Also, you haven't correctly understood @kglr 's comment. If I rewrite it as FoldList[Function[{foldarg1, foldarg2}, With[{xx = foldarg2}, Composition[m[.2 + xx], s]@foldarg1]], ...], is it easier to understand? Apr 18, 2021 at 2:30