0
$\begingroup$

Consider the following code

g[x_]:=x^2
f[x_]:=2*g[x]
f[x] (* returns 2 x^2*)
test[x_]:=Module[{g},f[x]]
test[x] (* returns 2 x^2 as well*)

What I would like to have and the reason why I thought module would have been fine to use is to locally remove the definition of g so that test[x] returns me 2*g[x].

I want to do it by only modifying what appears inside the definition of my function test[x], I don't have the right to modify the definitions of f or g.

How is it possible to do it ?

$\endgroup$
2
  • 1
    $\begingroup$ You could try using Block[{g = Inactive[g]}, f[x]] instead of Module[{g},f[x]]. The Module variable g has no relationship to the function g. $\endgroup$ – Carl Woll Oct 7 '20 at 21:26
  • $\begingroup$ I see a problem here. Even if you mange to keep g unevaluated inside test, as soon as test returns g, g will be evaluated if you have a definition like:"g[x_]:=x^2" $\endgroup$ – Daniel Huber Oct 8 '20 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.