Reading Ted Ersek's trickpage I stumbled upon the following problem:

http://www.verbeia.com/mathematica/tips/HTMLLinks/Tricks_P-Z_19.html (last example)

g = h;
h[2] = 9;
{g[2], h[2]}
(* {9,9} *)

my question: is it possible to prevent g[2] to evaluate to a different value rather than h[2] and eventually 9. Furthermore can we assign g[2] different values?

My own tries led me to come up with the use of TagSet:

g/: HoldForm[g[2]] = 0;

wrapper[x_]:= HoldForm@x;

Now with these definitions i can in a round-about way display different value for wrapper[g[2]]

{wrapper[g[2]], g[2]}
(* {0,9} *)

To change value I still need to use TagSet as shown below:

g/: HoldForm[g[2]] = 1;
{wrapper[g[2]], g[2]}
(* {1,9} *)

Reiterating, this is indeed a circular way to overcome the problem. Is there a better way to prevent g[2] to evaluate to h[2]?

  • $\begingroup$ Please do include the appropriate link to Ted's page, for reference. $\endgroup$ – J. M.'s technical difficulties Aug 30 '17 at 14:53
  • $\begingroup$ @J.M. thanks will do ! $\endgroup$ – Ali Hashmi Aug 30 '17 at 14:53
  • $\begingroup$ @CarlWoll thanks i think i understood what you mean $\endgroup$ – Ali Hashmi Aug 30 '17 at 15:36