Wolfram: Symbol
What is a Symbol
A symbol in WolframLang is similar to identifiers (e.g. variable names and function names) in other programing languages.
Any sequence of letters are symbols.
xx = {3, 4, b + 1} (* {3, 4, 1 + b} *) (* xx and b are symbols. b has no value. *) (* set a value for b *) b = 99 (* 99 *) xx (* {3, 4, 100} *) (* b now has value. *)
WolframLang code is essentially a sequence of symbols and operators, such as
, [] () + - * / # &
etc.
〔see Wolfram: Syntax Shortcuts and Operators〕
Builtin Symbols and User-Defined Symbols
Some symbols are builtin. e.g. all function names, such as
Length
,
StringSplit
,
List
,
Print
, etc.
user-defined symbols are any letter-sequence you typed.
e.g. {a,b,3,4}
creates symbols a
, b
.
Symbol May Have a Value
Symbol may have a value.
Most builtin symbols, have value.
E.g. Symbol Length
has a value that is a function definition.
Symbol $UserBaseDirectory
has a value that is string of directory path.
- When a symbol has a value, the value replaces the symbol during evaluation. This is the heart of WolframLang computational model, a term-rewriting system.
- When a symbol has no value, it return the symbol itself.
Allowed Characters in Symbol
- Cannot begin with a digit.
- Cannot contain HYPHEN - and LOW LINE _.
- In WolframScript, symbol names should just be letters of the English alphabet, lowercase or uppercase, and digits.
- In Wolfram notebook, you can use other letter such as Greek or letter-like forms.
Technically, allowed characters are more complex. see Wolfram: Source Code Encoding and Unicode
(* demo complexity of unicode char in symbol, in WolframScript *) Symbol["α"] (* Symbol::symname: The string "α" cannot be used for a symbol name. A symbol name must start with a letter followed by letters and numbers. *) (* this works *) Symbol["\[Alpha]"] (* \[Alpha] *) (* chinese char works *) Symbol["中"] (* 中 *) (* emoji works *) Symbol["🤡"] (* 🤡 *) Symbol["⭐"] (* ⭐ *) (* Basically, Wolfram language allow non-english letters or letter-like characters in unicode. When they occur, Wolfram map some of them to its own named characters of the form \[Alpha]. this happen in notebook only. Those unicode chars that Wolfram language does not have a named char, can be represented as is. *)
By convention, names starting with uppercase are builtin functions.
Names containing DOLLAR SIGN $ are builtin or generated symbols.
e.g. $UserBaseDirectory
(* example of internally generated symbol, containing dollar sign *) Module[{x}, Print [x]] (* x$1539 *) (* example of machine generated symbol *) Unique[x] (* x$1539 *)
Create Symbol Programatically (Convert String to Symbol)
Symbol[nameStr]
-
Create a symbol and return it, if not already exist.
Symbol["x"] (* x *) Symbol["3"] (* Symbol::symname: The string "3" cannot be used for a symbol name. A symbol name must start with a letter followed by letters and numbers. *)
Unique
-
returns a unique symbol.
Unique[x] (* x$1539 *)
Convert Symbol to String
SymbolName[symbol]
-
return the symbol name, as string.
x = 3; y = SymbolName[Unevaluated[x]] (* x *) StringQ[y] (* True *)
List Existing Symbols
Names[globPatternStr]
-
List existing symbols. The pattern string can include a asterisk to stand for any char or none.
it can also be Wolfram: Regular Expression or Wolfram: String Expression
(* list all symbols whose name contains String *) Names["*String*"]
(* list all symbols starting with x *) Names["x*"]
(* list all symbols in current context. this is most useful for listing all user-created symbols in current session. *) Names["`*"]
Clear a Symbol's Value
Clear
xx = 3 (* 3 *) yy = 4 (* 4 *) {xx, yy} (* {3, 4} *) Clear[xx] {xx, yy} (* {xx, 4} *)
(* clear all symbols in current context *) (* this is most useful for clearing all user-defined values in current session. *) Names["`*"]
Clear Everything of a Symbol
ClearAll
clears all values, definitions, attributes, defaults, options and messages.
Remove a symbol
💡 TIP: you basically never need to remove a symbol.
Check If a Symbol Has Value
ValueQ[expr]
return True if a value has been defined for expr
ValueQ[expr, Method → m]
see doc.
Get symbol's Definition
Definition
prints the definitions given for a symbol.
Definition[List] (* Attributes[List] = {Locked, Protected} *) x = 3; Definition[x] (* x = 3 *)
Get a symbol's Attributes
A symbol can have attributes. Attributes basically defines some properties of the function.
💡 TIP: this is advanced, you can ignore it.
Attributes
Attributes[List] (* {Locked, Protected} *) Attributes[Set] (* {HoldFirst, Protected, SequenceHold} *) Attributes[Table] (* {HoldAll, Protected} *) Attributes[Plus] (* {Flat, Listable, NumericFunction, OneIdentity, Orderless, Protected} *)