2
$\begingroup$

I am trying to define a function inside a module as following

test1[a_] := Module[{sol1, g1, y},
  sol1 = Reduce[a + x^2 == y, x];
  g1[y_] := Evaluate@sol1[[1, -1]];
  Print[Definition[g1]];
  g1[33]
  ]

This gives the output

Attributes[g1$28580]={Temporary}

g1$28580[y$_]:=-Sqrt[-3+y$28580]
-Sqrt[-3 + y$28588]

As you can see this does not work. The y in the body of the function is y$28580, but not y$. Is there any way to make this work?

$\endgroup$
3
  • 1
    $\begingroup$ If you use Block instead of Module does that solve the problem? $\endgroup$
    – Jason B.
    Commented Nov 30, 2018 at 19:09
  • 1
    $\begingroup$ Use this: SetDelayed @@ {g1[y_], sol1[[1, -1]]} and read this: Enforcing correct variable bindings $\endgroup$
    – Kuba
    Commented Nov 30, 2018 at 21:31
  • $\begingroup$ Let me know if you disagree with marking it as a duplicate. $\endgroup$
    – Kuba
    Commented Nov 30, 2018 at 21:33

1 Answer 1

1
$\begingroup$

Just replace the third line by:

g1[Pattern[Evaluate[y], _]] = sol1[[1, -1]];
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.