Next: Cells, Previous: ASCII character encoding, Up: System features
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.
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.
Enum
expands to the integer value represented symbolically by enumerand-name in the enumeration enumeration-name as defined bydefine-enumeration
.Components
expands to a literal vector of the components in enumeration-name as defined bydefine-enumeration
. In both cases, enumerand-name must be written literally as the name of the enumerand; seename->enumerand
for extracting an enumerand's integer given a run-time symbol naming an 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 bydefine-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.
(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)