1

How to use Evaluate inside function definition so that is acts as Evaluate only whet function is called?

This is toy exampl, where I want to define function inside another function:

Clear[y, x];
f1[a_] := Module[
   {test, testF},
   test = a x + 2;
   testF[x_] := Evaluate[test];
   testF[y]
];
f1[6]

I want result "2 + 6 y", but get "2 + 6 x"

  • 2
    Evaluate is completely unnecessary here -- you could just as well define test[x_] := a x + 2. I assume your actual problem is more complicated than this, but your minimal example is not a very good one. Perhaps you should include your actual question, even if more complicated. See also What is the XY problem?. – AccidentalFourierTransform Dec 06 '18 at 02:38
  • 2
    To be more specific, Clear[y, x]; f1[a_] := Module[{test, testF}, test = a x + 2; Set @@{testF[x_], test}; testF[y]]; f1[6] will resolve your problem. Notice Evaluate is not relevant here, what's causing trouble is renaming inside Module, which has been explained in the post linked above. – xzczd Dec 06 '18 at 06:24

0 Answers0