racket/collects/scribblings/guide/case.scrbl
Eli Barzilay 5a9d954f2a Several typos from Jussi Salmela. Fixes PR 11790 -- all typos.
(Two issues remain that will be resolved later.)
2011-03-11 06:47:36 -05:00

48 lines
1.2 KiB
Racket

#lang scribble/doc
@(require scribble/manual
scribble/eval
"guide-utils.ss"
(for-label racket/match))
@title[#:tag "case"]{Simple Dispatch: @racket[case]}
The @racket[case] form dispatches to a clause by matching the result
of an expression to the values for the clause:
@specform[(case expr
[(datum ...+) expr ...+]
...)]
Each @racket[_datum] will be compared to the result of the first
@racket[_expr] using @racket[eqv?]. Since @racket[eqv?] doesn't work on
many kinds of values, notably strings and lists, each @racket[_datum]
is typically a number, symbol, or boolean.
Multiple @racket[_datum]s can be supplied for each clause, and the
corresponding @racket[_expr] is evaluated if any of the
@racket[_datum]s match.
@examples[
(let ([v (random 6)])
(printf "~a\n" v)
(case v
[(0) 'zero]
[(1) 'one]
[(2) 'two]
[(3 4 5) 'many]))
]
The last clause of a @racket[case] form can use @racket[else], just
like @racket[cond]:
@examples[
(case (random 6)
[(0) 'zero]
[(1) 'one]
[(2) 'two]
[else 'many])
]
For more general pattern matching, use @racket[match], which is
introduced in @secref["match"].