► |  | Guide: PLT Scheme

1 | Welcome to PLT Scheme
2 | Scheme 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 | Performance
18 | Running and Creating Executables
19 | Compilation and Configuration
20 | More Libraries
21 | Dialects of Scheme
| 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.10`` Hash Tables

A hash table implements a mapping from keys to values, where both keys and values can be arbitrary Scheme values, and access and update to the table are normally constant-time operations. Keys are compared using equal? or eq?, depending on whether the hash table is created with make-hash or make-hasheq.

Examples:
> (define ht (make-hash))
> (hash-set! ht "apple" '(red round))
> (hash-set! ht "banana" '(yellow long))
> (hash-ref ht "apple")
(red round)
> (hash-ref ht "coconut")
hash-ref: no value found for key: "coconut"
> (hash-ref ht "coconut" "not there")
"not there"

A literal hash table can be written as an expression by using #hash (for an equal?-based table), #hasheq (for an eq?-based table), or #hasheqv (for an eqv?-based table). A parenthesized sequence must immediately follow #hash, #hasheq, or #hasheqv, where each element is a sequence is a dotted key–value pair. Literal hash tables are immutable, but they can be extended functionally (producing a new hash table without changing the old one) using hash-set.

Examples:
> (define ht #hash(("banana". yellow) ("apple". red)))
> (hash-ref ht "apple")
red
> (define ht2 (hash-set ht "coconut" 'brown))
> (hash-ref ht "coconut")
hash-ref: no value found for key: "coconut"
> (hash-ref ht2 "coconut")
brown
> ht2
#hash(("banana". yellow) ("coconut". brown) ("apple". red))

> > > Reading Hash Tables in Reference: PLT Scheme documents the fine points of the syntax of hash table literals.

A non-literal hash table can optionally retain its keys weakly, so each mapping is retained only so long as the key is retained elsewhere.

Examples:
> (define ht (make-weak-hasheq))
> (hash-set! ht (gensym) "can you see me?")
> (collect-garbage)
> (hash-count ht)
0

Beware that even a weak hash table retains its values strongly, as long as the corresponding key is accessible. This creates a catch-22 dependency when a value refers back to its key, so that the mapping is retained permanently. To break the cycle, map the key to an ephemeron that pairs the value with its key (in addition to the implicit pairing of the hash table).

Examples:
> (define ht (make-weak-hasheq))

> (let ([g (gensym)])
(hash-set! ht g (list g)))

> (collect-garbage)
> (hash-count ht)
1

> (define ht (make-weak-hasheq))

> (let ([g (gensym)])
(hash-set! ht g (make-ephemeron g (list g))))

> (collect-garbage)
> (hash-count ht)
0

> > > Hash Tables in Reference: PLT Scheme provides more on hash tables and hash-table procedures.

top← prev up next →