1
$\begingroup$

I have a code like

size = 1;
NestList[size = size*.5; Composition[size*# &, f], x, 3]

{x,0.5 f[x],0.5 f[0.5 f[x]],0.5 f[0.5 f[0.5 f[x]]]}

As the result,the size always is 0.5.How to make the size = size*.5 work?I think the Composition have took effect the process.If I want to reserve the Composition,how to adjust my code?

$\endgroup$
2
$\begingroup$

NestList does not hold its arguments so the size update occurs only once. I suppose you want something like this:

size = 1;
NestList[Composition[(size = size*.5; size*#) &, f], x, 3]

(*  {x, 0.5 f[x], 0.25 f[0.5 f[x]], 0.125 f[0.25 f[0.5 f[x]]]}  *)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks very very much..This logic cost my much time.. $\endgroup$ – yode May 25 '17 at 12:05
1
$\begingroup$

You can get more functional-style code using FoldList:

FoldList[(#2 f[#1]) &, x, 1/2.^Range[3]]
{x, 0.5 f[x], 0.25 f[0.5 f[x]], 0.125 f[0.25 f[0.5 f[x]]]}
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks,and actually I want to keep my Composition. $\endgroup$ – yode May 25 '17 at 11:55

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.