I'm trying to understand the difference between HoldAll
and Unevaluated
. The documentation for HoldAll
, under "Application", lists the following example:
(* Use HoldAll and Unevaluated to suppress evaluation of symbols wherever it would occur: *)
In[1]:= SetAttributes[symbolLength, HoldAll];
symbolLength[s_Symbol] := StringLength[SymbolName[Unevaluated[s]]]
(* Find the length of a symbol's name even if it has a value: *)
In[2]:= xyzzy = 1;
In[3]:= symbolLength[xyzzy]
Out[3]= 5
The problem is that if you define symbolLength
without Unevaluated
, i.e.
symbolLength[s_Symbol] := StringLength[SymbolName[s]]
then the function still returns 5. So what's the point of Unevaluated
? Also, more generally, shouldn't HoldAll
already keep s
unevaluated? And if that's the case, then is there any reason to combine HoldAll
and Unevaluated
in the same function?
5
, in the second version: i.sstatic.net/U6zUR.png $\endgroup$