Next: , Previous: ASCII character encoding, Up: System features


4.1.6 Integer enumerations

Scheme48 provides a facility for integer enumerations, somewhat akin to C enums. The names described in this section are exported by the enumerated structure.

Note: These enumerations are not compatible with the enumerated/finite type facility.

— syntax: define-enumeration enumeration-name (enumerand-name ...)

Defines enumeration-name to be a static enumeration. (Note that it is not a regular variable. It is actually a macro, though its exact syntax is not exposed; it must be exported with the :syntax type.) Enumeration-name thereafter may be used with the enumeration operators described below.

— syntax: enum enumeration-name enumerand-name –> enumerand-integer
— syntax: components enumeration-name –> component-vector

Enum expands to the integer value represented symbolically by enumerand-name in the enumeration enumeration-name as defined by define-enumeration. Components expands to a literal vector of the components in enumeration-name as defined by define-enumeration. In both cases, enumerand-name must be written literally as the name of the enumerand; see name->enumerand for extracting an enumerand's integer given a run-time symbol naming an enumerand.

— syntax: enumerand->name enumerand-integer enumeration-name –> symbol
— syntax: name->enumerand enumerand-name enumeration-name –> integer-enumerand

Enumerand->name expands to a form that evaluates to the symbolic name that the integer value of the expression enumerand-integer is mapped to by enumeration-name as defined by define-enumeration. Name->enumerand expands to a form that evaluates to the integer value of the enumerand in enumeration-name that is represented symbolically by the value of the expression enumerand-name.

The enum-case structure provides a handy utility of the same name for dispatching on enumerands.

— syntax: enum-case
          (enum-case enumeration-name key
            ((enumerand-name ...) body)
            ...
            [(else else-body)])

Matches key with the clause one of whose names maps in enumeration-name to the integer value of key. Key must be an exact, non-negative integer. If no matching clause is found, and else-body is present, enum-case will evaluate else-body; if else-body is not present, enum-case will return an unspecific value.

Examples:

     (define-enumeration foo
       (bar
        baz))
     
     (enum foo bar)                          => 0
     (enum foo baz)                          => 1
     
     (enum-case foo (enum foo bar)
       ((baz) 'x)
       (else  'y))
         => y
     
     (enum-case foo (enum foo baz)
       ((bar) 'a)
       ((baz) 'b))
         => b
     
     (enumerand->name 1 foo)                 => baz
     (name->enumerand 'bar foo)              => 0
     (components foo)                        => #(bar baz)