You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/quad/qtest/mds/case.md

49 lines
1.1 KiB
Markdown

5 years ago
# Simple Dispatch: `case`
The `case` form dispatches to a clause by matching the result of an
expression to the values for the clause:
```racket
(case expr
[(datum ...+) body ...+]
...)
```
Each `datum` will be compared to the result of `expr` using `equal?`,
and then the corresponding `body`s are evaluated. The `case` form can
dispatch to the correct clause in _O_\(_log N_\)__ time for _N_
`datum`s.
Multiple `datum`s can be supplied for each clause, and the corresponding
`body`s are evaluated if any of the `datum`s match.
Example:
```racket
> (let ([v (random 6)])
(printf "~a\n" v)
(case v
[(0) 'zero]
[(1) 'one]
[(2) 'two]
[(3 4 5) 'many]))
0
'zero
```
The last clause of a `case` form can use `else`, just like `cond`:
Example:
```racket
> (case (random 6)
[(0) 'zero]
[(1) 'one]
[(2) 'two]
[else 'many])
'many
```
For more general pattern matching \(but without the dispatch-time
guarantee\), use `match`, which is introduced in \[missing\].