Skip to main content

symbol-name

symbol-name Function

Syntax:

symbol-name symbol → name

Arguments and Values:

symbol—a symbol.

name—a string.

Description:

symbol-name returns the name of symbol. The consequences are undefined if name is ever modified.

Examples:

(symbol-name ’temp)"TEMP" 
(symbol-name :start)"START"
(symbol-name (gensym))"G1234" ;for example

Exceptional Situations:

Should signal an error of type type-error if symbol is not a symbol.

symbol-package

Expanded Reference: symbol-name

Basic usage

symbol-name returns the name of a symbol as a string. By default, the Lisp reader upcases symbol names.

(symbol-name 'hello)
=> "HELLO"
(symbol-name 'car)
=> "CAR"
(symbol-name 'my-variable)
=> "MY-VARIABLE"

Keywords

Keywords also have string names. The leading colon is not part of the name.

(symbol-name :test)
=> "TEST"
(symbol-name :key-name)
=> "KEY-NAME"

Special symbols: NIL and T

(symbol-name nil)
=> "NIL"
(symbol-name t)
=> "T"

Uninterned and generated symbols

symbol-name works on uninterned symbols and gensyms as well.

(symbol-name (make-symbol "my-sym"))
=> "my-sym"
(symbol-name '#:foo)
=> "FOO"

Comparing symbol names

Since symbol-name returns a string, you can use string comparison functions on symbol names.

(string= (symbol-name 'hello) "HELLO")
=> T
(string-equal (symbol-name 'hello) "hello")
=> T

(sort '(banana apple cherry) #'string< :key #'symbol-name)
=> (APPLE BANANA CHERRY)

The returned string should not be modified

The string returned by symbol-name may share structure with the symbol's internal representation. Modifying it has undefined consequences. Use copy-seq if you need a mutable copy.

(let* ((sym 'example)
(name (symbol-name sym))
(copy (copy-seq name)))
(setf (char copy 0) #\e)
(values name copy))
=> "EXAMPLE"
=> "eXAMPLE"