Rougly my question is about constructing a function that maps Hold[var] to "var"
even when var has an assigned value. In details :
In[1] var=22; A=Hold[var];
Can we construct a function F such that F[A] becomes the string, "var" ?
In fact, I can do it like :
In[2] F[x_]:=StringDrop[StringDrop[ToString[x],5],-1];
In[3] F[A]//InputForm
Out[3] "var"
But above method seems somewhat unnatural.
One natural,ideal way I think possible is :
- uncover Hold in Hold[var], with its content(=var) untouched, and apply ToString to the content.
Another natural,ideal way I think possible is :
- Change Hold in Hold[var] to ToString, with its content(=var) untouched.
Can you suggest natural ways to do it?
F[Hold[x_]] := ToString@Unevaluated@x
? $\endgroup$SetAttributes[F, HoldFirst]
,F[x_] := ToString@Unevaluated@x
lets you dispense with theHold
and useF[var]
directly, in case that's useful to you!) $\endgroup$SymbolName
and pure function syntaxFunction[Null, SymbolName[Unevaluated[#]], HoldFirst]
$\endgroup$&
is short forFunction
, so it's redundant here (even though the syntax highlighter doesn't get that#
is bound). try simplyFunction[Null, SymbolName[Unevaluated[#]], HoldFirst][A]
. Equivalently,Function[v, SymbolName[Unevaluated[v]], HoldFirst][A]
$\endgroup$