2
$\begingroup$

Abstract: I have noticed something really tricky with Mathematica and the documentation for symbols is short on clearing the confusion. It likely reveals what symbols are but it's not very prominent. I know unique symbols are created inside Modules but are they also generated every time we define regular functions and variables?

About The Code: I did a simple test with abc = 123. The output for ?abc gives a grayed out Symbol and SymbolName produces an error message. Does this mean it is not a symbol?

abc = 123
?abc
SymbolName[abc]

Four Questions:

  1. Are system functions symbols?
  2. Are user defined functions symbols?
  3. Are user defined variables symbols?
  4. What are symbols and are they automatically generated?
$\endgroup$
4
$\begingroup$

Towards your abc example which seems to be the source of confusion: At all times after evaluating abc = 123, abc is a symbol (with full name probably Global`abc, but that depends on the Context[] in which you evaluate it). The problem with SymbolName[abc] is that abc evaluates immediately to 123. The latter is not a symbol, hence SymbolName[123] throws an error. One can obtain the behavior that you probably expected as follows:

SymbolName[Unevaluated[abc]]

"abc"

$\endgroup$
3
  • 2
    $\begingroup$ Addendum: if you want to use that construct as a pure function, do it this way: ff = Function[Null, SymbolName[Unevaluated[#]], {HoldFirst}]; ff[abc]. $\endgroup$ May 31 '20 at 12:45
  • $\begingroup$ @J.M. could it be used in this form as well: ff@abc? Does this form omit the need to set attributes to HoldFirst when used in other contexts like Blocks or user defined functions? Question on the immediate assignment ff = . Shouldn't it be delayed assignment ff := ? $\endgroup$ Jun 1 '20 at 5:34
  • 1
    $\begingroup$ @Jules: 1. yes; the attributes are already associated with the pure function ff. 2. := would force repeated evaluation of the RHS, so that isn't what you want in that situation; = is the correct choice here. $\endgroup$ Jun 1 '20 at 5:37

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.