1
$\begingroup$

When I insert the following code in Mathematica,

f[x__] := Map[HoldForm, {x}];
g[x_] := x^2;
f[g[t], Cos[t], Tan[t]]

I get {t^2, Cos[t], Tan[t]}, but I am trying to get {g[t], Cos[t], Tan[t]}. Why is HoldForm evaluating the user defined function g[t]? Any fix to this? Thank you.

$\endgroup$
3
$\begingroup$

The sample code has two evaluation leaks that need to be plugged.

The first is when the arguments are passed to f. f[x] evaluates x, and then f does its thing. The fix for this is to give f the HoldAll attribute.

The second is when the arguments are passed to Map. It's probably best to not change the attributes of Map, so instead we'll use Unevaluated to temporarily prevent evaluation until things get into f.

In[14]:= SetAttributes[f, HoldAll]
In[15]:= f[x__] := HoldForm /@ Unevaluated[{x}]; 
In[16]:= g[x_] := x^2; 
In[17]:= InputForm[f[g[t], Cos[t], Tan[t]]]

Out[17]//InputForm= {HoldForm[g[t]], HoldForm[Cos[t]], HoldForm[Tan[t]]}
$\endgroup$
1
  • $\begingroup$ Although this solves the problem in the example that I have provided, it doesn't fix the issue of the program that I am working with. The piecewise function that I had defined in replacement of g[t] always self-evaluates and I am not sure why. $\endgroup$
    – Wilson
    Aug 31 '16 at 5:50

Your Answer

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

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