► |  | The Racket Guide

1 | Welcome to Racket
2 | Racket Essentials
3 | Built-In Datatypes
4 | Expressions and Definitions
5 | Programmer-Defined Datatypes
6 | Modules
7 | Contracts
8 | Input and Output
9 | Regular Expressions
10 | Exceptions and Control
11 | Iterations and Comprehensions
12 | Pattern Matching
13 | Classes and Objects
14 | Units (Components)
15 | Reflection and Dynamic Evaluation
16 | Macros
17 | Creating Languages
18 | Concurrency and Synchronization
19 | Performance
20 | Parallelism
21 | Running and Creating Executables
22 | More Libraries
23 | Dialects of Racket and Scheme
24 | Command-Line Tools and Your Editor of Choice
| Bibliography
| Index

▼ | 3 | Built-In Datatypes

3.1 | Booleans
3.2 | Numbers
3.3 | Characters
3.4 | Strings (Unicode)
3.5 | Bytes and Byte Strings
3.6 | Symbols
3.7 | Keywords
3.8 | Pairs and Lists
3.9 | Vectors
3.10 | Hash Tables
3.11 | Boxes
3.12 | Void and Undefined

top ← prev up next →

#### 3.7`` Keywords

A keyword value is similar to a symbol (see Symbols), but its printed form is prefixed with #:.

> > > Reading Keywords in The Racket Reference documents the fine points of the syntax of keywords.

Examples:
> > (string->keyword "apple")
> '#:apple
> > '#:apple
> '#:apple
> > (eq? '#:apple (string->keyword "apple"))
> #t

More precisely, a keyword is analogous to an identifier; in the same way that an identifier can be quoted to produce a symbol, a keyword can be quoted to produce a value. The same term “keyword” is used in both cases, but we sometimes use keyword value to refer more specifically to the result of a quote-keyword expression or of string->keyword. An unquoted keyword is not an expression, just as an unquoted identifier does not produce a symbol:

Examples:
> > not-a-symbol-expression
> not-a-symbol-expression: undefined;
> cannot reference undefined identifier
> > #:not-a-keyword-expression
> eval:2:0: #%datum: keyword used as an expression
> in: #:not-a-keyword-expression

Despite their similarities, keywords are used in a different way than identifiers or symbols. Keywords are intended for use (unquoted) as special markers in argument lists and in certain syntactic forms. For run-time flags and enumerations, use symbols instead of keywords. The example below illustrates the distinct roles of keywords and symbols.

Examples:
> > (define dir (find-system-path 'temp-dir)) ; not '#:temp-dir
> > (with-output-to-file (build-path dir "stuff.txt")
> (lambda () (printf "example\n"))
> ; optional #:mode argument can be 'text or 'binary
> #:mode 'text
> ; optional #:exists argument can be 'replace, 'truncate, ...
> #:exists 'replace)

top ← prev up next →