I'm trying to create a list of functions by replacing variables in a template. I tried this:
In[1]:= Table[Function[{x,y}, (x + y) v], {v, {x,y}}]
Out[1]= {Function[{x, y}, (x + y) v], Function[{x, y}, (x + y) v]}
But these functions have v
in them. I was expecting to get:
{Function[{x, y}, (x + y) x], Function[{x, y}, (x + y) y]}
I thought this would work because I can do something similar with Solve
:
In[2]:= Table[Solve[(x + y) v == 1 && x == y], {v, {x, y}}]
Out[2]= {{{x -> -(1/Sqrt[2]), y -> -(1/Sqrt[2])}, …
This is equivalent to:
In[3]:= {Solve[(x + y) x == 1 && x == y], Solve[(x + y) y == 1 && x == y]}
Out[3]= {{{x -> -(1/Sqrt[2]), y -> -(1/Sqrt[2])}, …
It seems like it's replacing v
with x
/y
in the Solve
example, but not the Function
example. Substituting Function[{x,y}, (x + y) Evaluate[v]]
for Function[{x,y}, (x + y) v]
doesn't help. What's going on here? How can I generate a list of similar functions?
Function[{x, y}, (x + y) #] & /@ {x, y}
$\endgroup$Function
'sHoldAll
attribute. Use/.
or something like what Bob Hanlon is suggesting to work around it. $\endgroup$Evaluate
evaluates beforeTable
, soTable
's internal expression is still a function withHoldAll
. $\endgroup$