4.7 Symbols
Symbols in The Racket Guide introduces symbols.
A symbol is like an immutable string, but symbols are normally interned, so that two symbols with the same character content are normally eq?. All symbols produced by the default reader (see Reading Symbols) are interned.
The two procedures string->uninterned-symbol and gensym generate uninterned symbols, i.e., symbols that are not eq?, eqv?, or equal? to any other symbol, although they may print the same as other symbols.
The procedure string->unreadable-symbol returns an unreadable symbol that is partially interned. The default reader (see Reading Symbols) never produces a unreadable symbol, but two calls to string->unreadable-symbol with equal? strings produce eq? results. An unreadable symbol can print the same as an interned or uninterned symbol. Unreadable symbols are useful in expansion and compilation to avoid collisions with symbols that appear in the source; they are usually not generated directly, but they can appear in the result of functions like identifier-binding.
Interned and unreadable symbols are only weakly held by the internal symbol table. This weakness can never affect the result of an eq?, eqv?, or equal? test, but a symbol may disappear when placed into a weak box (see Weak Boxes), used as the key in a weak hash table (see Hash Tables), or used as an ephemeron key (see Ephemerons).
See Reading Symbols for information on reading symbols and Printing Symbols for information on printing symbols.
procedure
(symbol-interned? sym) → boolean?
sym : symbol?
> (symbol-interned? 'Apple) #t
> (symbol-interned? (gensym)) #f
> (symbol-interned? (string->unreadable-symbol "Apple")) #f
procedure
(symbol-unreadable? sym) → boolean?
sym : symbol?
> (symbol-unreadable? 'Apple) #f
> (symbol-unreadable? (gensym)) #f
> (symbol-unreadable? (string->unreadable-symbol "Apple")) #t
procedure
(symbol->string sym) → string?
sym : symbol?
See also symbol->immutable-string from racket/symbol.
> (symbol->string 'Apple) "Apple"
procedure
(string->symbol str) → symbol?
str : string?
> (string->symbol "Apple") 'Apple
> (string->symbol "1") '|1|
procedure
(string->uninterned-symbol str) → symbol?
str : string?
> (string->uninterned-symbol "Apple") 'Apple
> (eq? 'a (string->uninterned-symbol "a")) #f
> (eq? (string->uninterned-symbol "a") (string->uninterned-symbol "a")) #f
procedure
(string->unreadable-symbol str) → symbol?
str : string?
> (string->unreadable-symbol "Apple") 'Apple
> (eq? 'a (string->unreadable-symbol "a")) #f
> (eq? (string->unreadable-symbol "a") (string->unreadable-symbol "a")) #t
> (gensym "apple") 'apple8305788
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
4.7.1 Additional Symbol Functions
(require racket/symbol) | package: base |
Added in version 7.6 of package base.
procedure
(symbol->immutable-string sym) → (and/c string? immutable?)
sym : symbol?
> (symbol->immutable-string 'Apple) "Apple"
> (immutable? (symbol->immutable-string 'Apple)) #t
Added in version 7.6 of package base.