Message Boards Message Boards

use of the $ symbol

Posted 11 years ago
as I have been looking for information on how to solve a problem in mathematica, I have given the task of looking into the network related to my problem, but a question arose when reading a piece of code, because they use the $ symbol, the find something of use of this symbol in the program's help I could not find anything about it, someone who I have information about it?, here i put a piece of code

POSTED BY: Luis Ledesma
4 Replies
The author of your code sample didn't seem to follow this (unwritten) convention too strictly as he seems to use $Board as a variable (and not a constant like $Moves). I have the impression that he just marked global variables with the $ signs.  Somthething's wrong with the Edit option so I couldn't edit my previous post to add this.
POSTED BY: Szabolcs Horvát
As Jari said, $ can be part of identifier names, just like any letter of the alphabet.  However, it is used in the names of generated variables, in particular for Mathematica's variable localization mechanism:

Example 1:
In[1]:= Module[{x}, x]
Out[1]= x$22099

These generated names are either of the form name$number or simply name$.  They're never of the form $name.  So it is really a good idea not to use $ in the middle of a symbol name, but using it at the beginning is quite common.  Many people will use names starting with a $ to (informally) indicate that they are constants.  So if you read somebody's code and see a symbol name starting with $, it is likely that the author of the code meant to use that symbol as a constant.

Many built-in symbols follow this convention too, such as $MachinePrecision or $Path.  ($Path is a constant in the sense that programs won't use it as a variable that is continually changing.)

Built in symbols will start with a capital letter after the $, so it's safe to use name such as $myConstant, but better not use $MyConstant to make sure you don't conflict with built-ins.
POSTED BY: Szabolcs Horvát
Posted 11 years ago
Dollar-prefix as a convention to mark constants is a new thing to me; good to know.
POSTED BY: Jari Kirma
Posted 11 years ago
In Mathematica (or to be pedantic, in Wolfram language), dollar sign has no special semantic meaning, and it's a valid character in a variable name. In this sense, use of it in your example has no effect. You could change all dollar-prefixed variables to non-prefixed alternatives, and there would be no change in functionality, assuming there is no clash with predefined names.

On the other hand, dollar sign is used internally by Mathematica, and its use in user-generated variable names is strongly discouraged. This is most visible on system variables such as $MachinePrecision (which is not the same as symbol MachinePrecision!), but dollar signs are also used in generation of temporary variable names. So, avoid using it. Only "benefit" of using it is an increased chance of shooting yourself on your own foot.

Some languages require using symbols such as dollar signs to identify variables or a type of a variable. In your example, programmer might have used such a convention in Mathematica, although there is no need for it. On a side note, he has also chosen variables and function names to start in upper case. This is valid in Mathematica, but the convention is that only system-predefined names start in upper case, and user variables in lower case. This guarantees definitions not to clash.
POSTED BY: Jari Kirma
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract