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

▼ | 3 | Syntactic Forms

3.1 | Modules: module, module*, ...
3.2 | Importing and Exporting: require and provide
3.3 | Literals: quote and #%datum
3.4 | Expression Wrapper: #%expression
3.5 | Variable References and #%top
3.6 | Locations: #%variable-reference
3.7 | Procedure Applications and #%app
3.8 | Procedure Expressions: lambda and case-lambda
3.9 | Local Binding: let, let*, letrec, ...
3.10 | Local Definitions: local
3.11 | Constructing Graphs: shared
3.12 | Conditionals: if, cond, and, and or
3.13 | Dispatch: case
3.14 | Definitions: define, define-syntax, ...
3.15 | Sequencing: begin, begin0, and begin-for-syntax
3.16 | Guarded Evaluation: when and unless
3.17 | Assignment: set! and set!-values
3.18 | Iterations and Comprehensions: for, for/ list, ...
3.19 | Continuation Marks: with-continuation-mark
3.20 | Quasiquoting: quasiquote, unquote, and unquote-splicing
3.21 | Syntax Quoting: quote-syntax
3.22 | Interaction Wrapper: #%top-interaction
3.23 | Blocks: block
3.24 | Internal-Definition Limiting: #%stratified-body
3.25 | Performance Hints: begin-encourage-inline
3.26 | Importing Modules Lazily: lazy-require

On this page:

case

top ← prev up next →

#### 3.13`` Dispatch: case

> > syntax

> > (case val-expr case-clause ...)

> case-clause |  | = |  | [(datum ...) then-body ...+]
> |  | | |  | [else then-body ...+]

Evaluates val-expr and uses the result to select a case-clause. The selected clause is the first one with a datum whose quoted form is equal? to the result of val-expr. If no such datum is present, the else case-clause is selected; if no else case-clause is present, either, then the result of the case form is #<void>.

> > > The case form of racket differs from that of R6RS: Scheme or R5RS: Legacy Scheme by being based equal? instead of eqv? (in addition to allowing internal definitions).

For the selected case-clause, the results of the last then-body, which is in tail position with respect to the case form, are the results for the whole case form.

A case-clause that starts with else must be the last case-clause.

The case form can dispatch to a matching case-clause in O(log N) time for N datums.

Examples:

> > (case (+ 7 5)
> [(1 2 3) 'small]
> [(10 11 12) 'big])

> 'big

> > (case (- 7 5)
> [(1 2 3) 'small]
> [(10 11 12) 'big])

> 'small

> > (case (string-append "do" "g")
> [("cat" "dog" "mouse") "animal"]
> [else "mineral or vegetable"])

> "animal"

> > (case (list 'y 'x)
> [((a b) (x y)) 'forwards]
> [((b a) (y x)) 'backwards])

> 'backwards

> > (case 'x
> [(x) "ex"]
> [('x) "quoted ex"])

> "ex"

> > (case (list 'quote 'x)
> [(x) "ex"]
> [('x) "quoted ex"])

> "quoted ex"

> (define (classify c)
> (case (char-general-category c)
> [(ll lu lt ln lo) "letter"]
> [(nd nl no) "number"]
> [else "other"]))

> > (classify #\A)
> "letter"
> > (classify #\1)
> "number"
> > (classify #\!)
> "other"

top ← prev up next →