On this page:
top
up

3.13Dispatch: case πŸ”— i

syntax

( case val-exprcase-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 quote d 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 or R5RS by being based on 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 (+ 75)
[(123)'small]
[(101112)'big])

'big

> (case (- 75)
[(123)'small]
[(101112)'big])

'small

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

"animal"

> (case (list 'y'x)
[((ab)(xy))'forwards]
[((ba)(yx))'backwards])

'backwards

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

"ex"

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

"quoted ex"

(define (classifyc)
[(lllultlnlo)"letter"]
[(ndnlno)"number"]
[else "other"]))
> (classify#\A)

"letter"

> (classify#1円)

"number"

> (classify#\!)

"other"

3.13.1 Variants of case πŸ”— i

(require racket/case ) package: base
The bindings documented in this section are provided by the racket/case library, not racket/base or racket.

Added in version 8.11.1.8 of package base.

syntax

( case/equal val-exprcase-clause...)

syntax

( case/equal-always val-exprcase-clause...)

syntax

( case/eq val-exprcase-clause...)

syntax

( case/eqv val-exprcase-clause...)

Like case , but using equal? , equal-always? , eq? , or eqv? for comparing the result of val-expr to the literals in the case-clauses. The case/equal form is equivalent to case .

top
up

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /