1 Language Model
2 Syntactic Forms
3 Datatypes
4 Structures
5 Classes and Objects
6 Units
7 Contracts
8 Pattern Matching
9 Control Flow
10 Concurrency
11 Macros
12 Input and Output
13 Reflection and Security
14 Operating System
15 Memory Management
16 Running PLT Scheme
Bibliography
Index
On this page:
case
Version: 4.0.2

 

2.13 Dispatch: case

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

 

case-clause

 

=

 

[(datum ...) then-expr ...+]

 

 

|

 

[else then-expr ...+]

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 eqv? 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>.

For the selected case-clause, the results of the last then-expr, 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.

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

  (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"