I have a piece of code where I would like to do the something like the following:
ComposeList[Table[Which[
m < 3, f[#, m] &,
m < 6, f[#, m + 1] &
], {m, 1, 5, 1}], x]
The idea here is to repeatedly apply f
to x
, but with the second argument of f
changing in some way as it goes. However, this does not work, because pure functions are left unevaluated. That means that the m
in f[#,m]
stays as m
when the table is created, which is then meaningless later on.
If I try and fix this by wrapping f[#,m]
in Evaluate
, it evaluates not only m
, but also f
. This is a problem for me, because it tries to evaluate f
with that Slot
still in the first argument, and f
is very complicated and it fails. I really need the resulting table to look like {f[#,1], f[#,2],...}
.
The only solution I came up with is as follows:
ComposeList[Table[Which[
m < 3, f @@@ Function[Evaluate[qpzrqrq[#, m]]],
m < 6, f @@@ Function[Evaluate[qpzrqrq[#, m + 1]]]
], {m, 1, 5, 1}], x]
Here I am evaluating the pure function with a different head (qpzrqrq
) which I hope is not actually defined anywhere. This evaluates the arguments, and then I replaced qpzrqrq
with the actual function f
. However, this is definitely a hack, and relies on qpzrqrq
being undefined. Does anyone have a better solution to this problem?
Fold
should be helpful to you. $\endgroup$ – MarcoB Jun 22 '15 at 12:24