4.12 Simple Dispatch: case

► |  | 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
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

▼ | 4 | Expressions and Definitions

4.1 | Notation
4.2 | Identifiers and Binding
4.3 | Function Calls
4.4 | Functions: lambda
4.5 | Definitions: define
4.6 | Local Binding
4.7 | Conditionals
4.8 | Sequencing
4.9 | Assignment: set!
4.10 | Quoting: quote and ’
4.11 | Quasiquoting: quasiquote and ‘
4.12 | Simple Dispatch: case
4.13 | Dynamic Binding: parameterize

top contents ← prev up next →

#### 4.12`` Simple Dispatch: case🔗ℹ

The case form dispatches to a clause by matching the result of an expression to the values for the clause:

> > (case expr
> > [(datum ...+) body ...+]
> > ...)

Each datum will be compared to the result of expr using equal?, and then the corresponding bodys are evaluated. The case form can dispatch to the correct clause in O(log N) time for N datums.

Multiple datums can be supplied for each clause, and the corresponding bodys are evaluated if any of the datums match.

Example:

> > (let ([v (random 6)])
> (printf "~a\n" v)
> (case v
> [(0) 'zero]
> [(1) 'one]
> [(2) 'two]
> [(3 4 5) 'many]))
> 2
> 'two

The last clause of a case form can use else, just like cond:

Example:

> > (case (random 6)
> [(0) 'zero]
> [(1) 'one]
> [(2) 'two]
> [else 'many])
> 'zero

For more general pattern matching (but without the dispatch-time guarantee), use match, which is introduced in Pattern Matching.

top contents ← prev up next →