4.7 Symbols (original) (raw)

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 andgensym 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 anunreadable symbol that is partially interned. The default reader (see Reading Symbols) never produces a unreadable symbol, but two calls to string->unreadable-symbol withequal? 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 aneq?, 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.

Returns #t if v is a symbol, #f otherwise.

Examples:

> (symbol? 'Apple)
#t
> (symbol? 10)
#f

Returns #t if sym isinterned, #f otherwise.

Examples:

Returns #t if sym is an unreadable symbol, #f otherwise.

Examples:

Returns a freshly allocated mutable string whose characters are the same as insym.

See also symbol->immutable-string fromracket/symbol.

Example:

> (symbol->string 'Apple)
"Apple"

Returns aninterned symbol whose characters are the same as instr.

Examples:

> (string->symbol "Apple")
'Apple
> (string->symbol "1")
'|1

Like(string->symbol str), but the resulting symbol is a newuninterned symbol. Calling string->uninterned-symbol twice with the same str returns two distinct symbols.

Examples:

Like(string->symbol str), but the resulting symbol is a newunreadable symbol. Calling string->unreadable-symbol twice with equivalent strs returns the same symbol, butread never produces the symbol.

Examples:

Returns a new uninterned symbol with an automatically-generated name. The optional base argument is a prefix symbol or string.

Example:

> (gensym "apple")
'apple8151951

Returns #t if the arguments are sorted, where the comparison for each pair of symbols is the same as usingsymbol->string with string->bytes/utf-8 andbytes<?.

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🔗

The bindings documented in this section are provided by the racket/symbol library, not racket/base or racket.

Added in version 7.6 of package base.

Like symbol->string, but the result is an immutable string, not necessarily freshly allocated.

Examples:

Added in version 7.6 of package base.