# Looping variables not replaced by their values

Sometimes Table[ ] leaves the looping variable in the output, instead of replacing it by the values. For example

In:= Table[{v, #[v] &, # /. x -> v &}, {v, {0, 1}}]


gives

Out= {{0, #1[v] &, #1 /. x -> v &}, {1, #1[v] &, #1 /. x -> v &}},


Out= {{0, #1 &, #1 /. x -> 0 &}, {1, #1 &, #1 /. x -> 1 &}}.


Is there a way to force Table[ , {v, {0,1}}] to properly replace v by the values 0 and 1?? Can anyone explain this bizarre behavior?

• You could use a dummy variable u and then replace, as in Table[{u, #[u] &, # /. x -> u &} /. u -> v, {v, {0, 1}}]. Or not use Table: {v, #[v] &, # /. x -> v &} /. Array[{v -> #} &, 2, 0]. – aardvark2012 Nov 2 '17 at 2:57
• Can you explain what you are trying to do? What is the goal of this construction? – bill s Nov 2 '17 at 3:02
• What is #[v] & supposed to give? Can you give what is the expected output of Table[{v, #[v] &, # /. x -> v &}, {v, {0, 1}}]? It is not clear what you want to generate. – Nasser Nov 2 '17 at 3:02
• Nasser: #& is a pure function which sends the input f to the output f. Bill: I was using such functions to as inputs to a somewhat abstract constrained optimization routine I wrote. In particular, I wanted to use the above function tell the optimizer to optimize only over functions f which evaluated to certain values in certain places. I found a work-around, but it very much bothers me when basic functions such as Table[ ] do unexpected things, so I want to know what is going on here. – J Tyson Nov 2 '17 at 3:05

Table[With[{v = v}, {v, #[v] &, # /. x -> v &}], {v, {0, 1}}]