4.7 Symbols

► |  | The Racket Reference

1 | Language Model
2 | Notation for Documentation
3 | Syntactic Forms
4 | Datatypes
5 | Structures
6 | Classes and Objects
7 | Units
8 | Contracts
9 | Pattern Matching
10 | Control Flow
11 | Concurrency and Parallelism
12 | Macros
13 | Input and Output
14 | Reflection and Security
15 | Operating System
16 | Memory Management
17 | Unsafe Operations
18 | Running Racket
| Bibliography
| Index

▼ | 4 | Datatypes

4.1 | Equality
4.2 | Booleans
4.3 | Numbers
4.4 | Strings
4.5 | Byte Strings
4.6 | Characters
4.7 | Symbols
4.8 | Regular Expressions
4.9 | Keywords
4.10 | Pairs and Lists
4.11 | Mutable Pairs and Lists
4.12 | Vectors
4.13 | Stencil Vectors
4.14 | Boxes
4.15 | Hash Tables
4.16 | Treelists
4.17 | Sequences and Streams
4.18 | Dictionaries
4.19 | Sets
4.20 | Procedures
4.21 | Void
4.22 | Undefined

► | 4.7 | Symbols

4.7.1 | Additional Symbol Functions

On this page:

symbol?
symbol-interned?
symbol-unreadable?
symbol->string
string->symbol
string->uninterned-symbol
string->unreadable-symbol
gensym
symbol<?
4.7.1`` Additional Symbol Functions
symbol->immutable-string

top contents ← prev up next →

#### 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 an 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? v) → boolean?
> v : any/c

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

Examples:

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

> procedure (symbol-interned? sym) → boolean?
> sym : symbol?

Returns #t if sym is interned, #f otherwise.

Examples:

> > (symbol-interned? 'Apple)
> #t
> > (symbol-interned? (gensym))
> #f
> > (symbol-interned? (string->unreadable-symbol "Apple"))
> #f

> procedure (symbol-unreadable? sym) → boolean?
> sym : symbol?

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

Examples:

> > (symbol-unreadable? 'Apple)
> #f
> > (symbol-unreadable? (gensym))
> #f
> > (symbol-unreadable? (string->unreadable-symbol "Apple"))
> #t

> procedure (symbol->string sym) → string?
> sym : symbol?

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

See also symbol->immutable-string from racket/symbol.

Example:

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

> procedure (string->symbol str) → symbol?
> str : string?

Returns an interned symbol whose characters are the same as in str.

Examples:

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

> procedure (string->uninterned-symbol str) → symbol?
> str : string?

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

Examples:

> > (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?

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

Examples:

> > (string->unreadable-symbol "Apple")
> 'Apple
> > (eq? 'a (string->unreadable-symbol "a"))
> #f
> > (eq? (string->unreadable-symbol "a")
> (string->unreadable-symbol "a"))
> #t

> procedure (gensym [base]) → symbol?
> base : (or/c string? symbol?) = "g"

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

Example:

> > (gensym "apple")
> 'apple7140519

> procedure (symbol<? a-sym b-sym ...) → boolean?
> a-sym : symbol?
> b-sym : symbol?

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

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

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.

> procedure (symbol->immutable-string sym) → (and/c string? immutable?)
> sym : symbol?

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

Examples:

> > (symbol->immutable-string 'Apple)
> "Apple"
> > (immutable? (symbol->immutable-string 'Apple))
> #t

Added in version 7.6 of package base.

top contents ← prev up next →