Symbol (Lisp)
Encyclopedia
A symbol in computer programming
is a primitive datatype whose instances have a unique human-readable form. Symbols can be used as identifiers. In some programming languages, they are called atoms.
In the most trivial implementation
, they are essentially named integer
s (e.g. the enumerated type
in C).
s provide support for symbols:
(called package in Common Lisp
). Symbols can be tested for equality with the function EQ. Lisp programs can generate new symbols at runtime. When Lisp reads data that contains textual represented symbols, existing symbols are referenced. If a symbol is unknown, the Lisp reader creates a new symbol.
In Common Lisp
symbols have the following attributes: a name, a value, a function, a list of properties and a package.
In Common Lisp
symbols may use any characters, including whitespace, such as spaces and newlines. If a symbol contains a whitespace character it needs to be written as |this is a symbol|. Symbols can be used as identifiers for any kind of named programming constructs: variables, functions, macros, classes, types, goto tags and more.
Symbols can be interned in a package. Keyword symbols are self-evaluating and interned in the package named KEYWORD.
symbol:
Symbols can contain whitespace (and all other characters):
In Common Lisp
symbols with a leading colon in their printed representations are keyword symbols
. These are interned in the keyword package.
A printed representation of a symbol may include a package name. Two colons are written between the name of the package and the name of the symbol.
Packages can export symbols. Then only one colon is written between the name of the package and the name of the symbol.
, symbols can be created with a literal form, or by converting a string.
They can be used as an identifier or an interned string. Two symbols with the same contents will always refer to the same object.
It is considered a best practice
to use symbols as keys to an associative array
in Ruby.
Strings can be coerced into symbols, vice versa:
Symbols are objects of the
Symbols are commonly used to dynamically send messages to (call methods on) objects:
Symbols as keys of an associative array:
, symbols can be created with a literal form, or by converting a string.
They can be used as an identifier or an interned string. Two symbols with the same contents will always refer to the same object. In most Smalltalk implementations, selectors (method names) are implemented as symbols.
Strings can be coerced into symbols, vice versa:
Symbols conform to the
Symbols are commonly used to dynamically send messages to (call methods on) objects:
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
is a primitive datatype whose instances have a unique human-readable form. Symbols can be used as identifiers. In some programming languages, they are called atoms.
In the most trivial implementation
Implementation
Implementation is the realization of an application, or execution of a plan, idea, model, design, specification, standard, algorithm, or policy.-Computer Science:...
, they are essentially named integer
Integer
The integers are formed by the natural numbers together with the negatives of the non-zero natural numbers .They are known as Positive and Negative Integers respectively...
s (e.g. the enumerated type
Enumerated type
In computer programming, an enumerated type is a data type consisting of a set of named values called elements, members or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language...
in C).
Support
The following programming languageProgramming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
s provide support for symbols:
language | type name(s) | example literal(s) |
Clojure Clojure Clojure |closure]]") is a recent dialect of the Lisp programming language created by Rich Hickey. It is a general-purpose language supporting interactive development that encourages a functional programming style, and simplifies multithreaded programming.... |
symbol, keyword | symbol, :keyword |
ANSI Ansi Ansi is a village in Kaarma Parish, Saare County, on the island of Saaremaa, Estonia.... Common Lisp Common Lisp Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers... |
symbol, keyword | symbol, :keyword |
R6RS Scheme | symbol | sym |
Ruby Ruby (programming language) Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto... |
Symbol | :sym or :'sym' |
Smalltalk Smalltalk Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist... |
Symbol | #sym or #'sym' |
Ericsson Ericsson Ericsson , one of Sweden's largest companies, is a provider of telecommunication and data communication systems, and related services, covering a range of technologies, including especially mobile networks... Erlang |
atom | sym |
Lisp
A symbol in Lisp is unique in a namespaceNamespace
In general, a namespace is a container that provides context for the identifiers it holds, and allows the disambiguation of homonym identifiers residing in different namespaces....
(called package in Common Lisp
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...
). Symbols can be tested for equality with the function EQ. Lisp programs can generate new symbols at runtime. When Lisp reads data that contains textual represented symbols, existing symbols are referenced. If a symbol is unknown, the Lisp reader creates a new symbol.
In Common Lisp
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...
symbols have the following attributes: a name, a value, a function, a list of properties and a package.
In Common Lisp
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...
symbols may use any characters, including whitespace, such as spaces and newlines. If a symbol contains a whitespace character it needs to be written as |this is a symbol|. Symbols can be used as identifiers for any kind of named programming constructs: variables, functions, macros, classes, types, goto tags and more.
Symbols can be interned in a package. Keyword symbols are self-evaluating and interned in the package named KEYWORD.
Examples
The following is a simple external representation of Common LispCommon Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...
symbol:
Symbols can contain whitespace (and all other characters):
In Common Lisp
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...
symbols with a leading colon in their printed representations are keyword symbols
Keyword (computer programming)
In computer programming, a keyword is a word or identifier that has a particular meaning to the programming language. The meaning of keywords — and, indeed, the meaning of the notion of keyword — differs widely from language to language....
. These are interned in the keyword package.
A printed representation of a symbol may include a package name. Two colons are written between the name of the package and the name of the symbol.
Packages can export symbols. Then only one colon is written between the name of the package and the name of the symbol.
Ruby
In RubyRuby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...
, symbols can be created with a literal form, or by converting a string.
They can be used as an identifier or an interned string. Two symbols with the same contents will always refer to the same object.
It is considered a best practice
Best practice
A best practice is a method or technique that has consistently shown results superior to those achieved with other means, and that is used as a benchmark...
to use symbols as keys to an associative array
Associative array
In computer science, an associative array is an abstract data type composed of a collection of pairs, such that each possible key appears at most once in the collection....
in Ruby.
Examples
The following is a simple example of a symbol literal in Ruby:Strings can be coerced into symbols, vice versa:
Symbols are objects of the
Symbol
class in Ruby:Symbols are commonly used to dynamically send messages to (call methods on) objects:
Symbols as keys of an associative array:
Smalltalk
In SmalltalkSmalltalk
Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...
, symbols can be created with a literal form, or by converting a string.
They can be used as an identifier or an interned string. Two symbols with the same contents will always refer to the same object. In most Smalltalk implementations, selectors (method names) are implemented as symbols.
Examples
The following is a simple example of a symbol literal in Smalltalk:Strings can be coerced into symbols, vice versa:
Symbols conform to the
symbol
protocol, and their class is called Symbol
in most implementations:Symbols are commonly used to dynamically send messages to (call methods on) objects: