323 lines
16 KiB
HTML
323 lines
16 KiB
HTML
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<html>
|
|
<!--
|
|
|
|
Generated from r6rs-lib.tex by tex2page, v 20070803
|
|
(running on MzScheme 371, unix),
|
|
(c) Dorai Sitaram,
|
|
http://www.ccs.neu.edu/~dorai/tex2page/tex2page-doc.html
|
|
|
|
-->
|
|
<head>
|
|
<title>
|
|
r6rs-lib
|
|
</title>
|
|
<link rel="stylesheet" type="text/css" href="r6rs-lib-Z-S.css" title=default>
|
|
<meta name=robots content="index,follow">
|
|
</head>
|
|
<body>
|
|
<div id=slidecontent>
|
|
<div align=right class=navigation>[Go to <span><a href="r6rs-lib.html">first</a>, <a href="r6rs-lib-Z-H-14.html">previous</a></span><span>, <a href="r6rs-lib-Z-H-16.html">next</a></span> page<span>; </span><span><a href="r6rs-lib-Z-H-1.html#node_toc_start">contents</a></span><span><span>; </span><a href="r6rs-lib-Z-H-21.html#node_index_start">index</a></span>]</div>
|
|
<p></p>
|
|
<a name="node_chap_14"></a>
|
|
<h1 class=chapter>
|
|
<div class=chapterheading><a href="r6rs-lib-Z-H-1.html#node_toc_node_chap_14">Chapter 14</a></div><br>
|
|
<a href="r6rs-lib-Z-H-1.html#node_toc_node_chap_14">Enumerations</a></h1>
|
|
<p></p>
|
|
<p>
|
|
This chapter describes the <tt>(rnrs enums (6))</tt><a name="node_idx_1226"></a>library for dealing with enumerated values
|
|
<a name="node_idx_1228"></a>and sets of enumerated values. Enumerated
|
|
values are represented by ordinary symbols, while finite sets of
|
|
enumerated values form a separate type, known as the
|
|
<a name="node_idx_1230"></a><em>enumeration sets</em>.
|
|
The enumeration sets are further partitioned into sets that
|
|
share the same <a name="node_idx_1232"></a><em>universe</em> and <a name="node_idx_1234"></a><em>enumeration type</em>.
|
|
These universes and enumeration types are created by the
|
|
<tt>make-enumeration</tt> procedure. Each call to that procedure
|
|
creates a new enumeration type.</p>
|
|
<p>
|
|
This library interprets each enumeration set with respect to
|
|
its specific universe of symbols and enumeration type.
|
|
This facilitates efficient implementation of enumeration sets
|
|
and enables the complement operation.</p>
|
|
<p>
|
|
In the descriptions of the following procedures, <i>enum-set</i>
|
|
ranges over the enumeration sets, which are defined as the subsets
|
|
of the universes that can be defined using <tt>make-enumeration</tt>.</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_1236"></a>make-enumeration<i> symbol-list</i>)</tt> procedure </div>
|
|
<p>
|
|
<i>Symbol-list</i> must be a list of symbols.
|
|
The <tt>make-enumeration</tt> procedure
|
|
creates a new enumeration type whose universe consists of
|
|
those symbols (in canonical order of their first appearance
|
|
in the list) and returns that universe as an enumeration
|
|
set whose universe is itself and whose enumeration type is
|
|
the newly created enumeration type.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_1238"></a>enum-set-universe<i> enum-set</i>)</tt> procedure </div>
|
|
<p>
|
|
Returns the set of all symbols that comprise
|
|
the universe of its argument, as an enumeration set.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_1240"></a>enum-set-indexer<i> enum-set</i>)</tt> procedure </div>
|
|
<p>
|
|
Returns a unary procedure that, given a symbol
|
|
that is in the universe of <i>enum-set</i>, returns its 0-origin index
|
|
within the canonical ordering of the symbols in the universe; given a
|
|
value not in the universe, the unary procedure returns <tt>#f</tt>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let* ((e (make-enumeration ’(red green blue)))<br>
|
|
(i (enum-set-indexer e)))<br>
|
|
(list (i ’red) (i ’green) (i ’blue) (i ’yellow))) <br> ⇒ (0 1 2 <tt>#f</tt>)<p></tt></p>
|
|
<p>
|
|
The <tt>enum-set-indexer</tt> procedure could be defined as follows using the
|
|
<tt>memq</tt> procedure from the <tt>(rnrs lists (6))</tt> library:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(define (enum-set-indexer set)<br>
|
|
(let* ((symbols (enum-set->list<br>
|
|
(enum-set-universe set)))<br>
|
|
(cardinality (length symbols)))<br>
|
|
(lambda (x)<br>
|
|
(cond<br>
|
|
((memq x symbols)<br>
|
|
=> (lambda (probe)<br>
|
|
(- cardinality (length probe))))<br>
|
|
(else <tt>#f</tt>)))))<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_1242"></a>enum-set-constructor<i> enum-set</i>)</tt> procedure </div>
|
|
<p>
|
|
Returns a unary procedure that, given a
|
|
list of symbols that belong to the universe of <i>enum-set</i>, returns
|
|
a subset of that universe that contains exactly the symbols in the
|
|
list. The values in the list must all belong to the universe.
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_1244"></a>enum-set->list<i> enum-set</i>)</tt> procedure </div>
|
|
<p>
|
|
Returns a list of the symbols that belong to its
|
|
argument, in the canonical order of the universe of <i>enum-set</i>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let* ((e (make-enumeration ’(red green blue)))<br>
|
|
(c (enum-set-constructor e)))<br>
|
|
(enum-set->list (c ’(blue red)))) <br> ⇒ (red blue)<br>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_1246"></a>enum-set-member?<i> symbol enum-set</i>)</tt> procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_1248"></a>enum-set-subset?<i> <i>enum-set<sub>1</sub></i> <i>enum-set<sub>2</sub></i></i>)</tt> procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_1250"></a>enum-set=?<i> <i>enum-set<sub>1</sub></i> <i>enum-set<sub>2</sub></i></i>)</tt> procedure </div>
|
|
<p>
|
|
The <tt>enum-set-member?</tt> procedure returns <tt>#t</tt> if its first argument is an
|
|
element of its second argument, <tt>#f</tt> otherwise.</p>
|
|
<p>
|
|
The <tt>enum-set-subset?</tt> procedure returns <tt>#t</tt> if the universe of
|
|
<i>enum-set<sub>1</sub></i> is a subset of the universe of <i>enum-set<sub>2</sub></i>
|
|
(considered as sets of symbols) and every element of <i>enum-set<sub>1</sub></i>
|
|
is a member of <i>enum-set<sub>2</sub></i>. It returns <tt>#f</tt> otherwise.</p>
|
|
<p>
|
|
The <tt>enum-set=?</tt> procedure returns <tt>#t</tt> if <i>enum-set<sub>1</sub></i> is a
|
|
subset of <i>enum-set<sub>2</sub></i> and vice versa, as determined by the
|
|
<tt>enum-set-subset?</tt> procedure. This implies that the universes of
|
|
the two sets are equal as sets of symbols, but does not imply
|
|
that they are equal as enumeration types. Otherwise, <tt>#f</tt> is
|
|
returned.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let* ((e (make-enumeration ’(red green blue)))<br>
|
|
(c (enum-set-constructor e)))<br>
|
|
(list<br>
|
|
(enum-set-member? ’blue (c ’(red blue)))<br>
|
|
(enum-set-member? ’green (c ’(red blue)))<br>
|
|
(enum-set-subset? (c ’(red blue)) e)<br>
|
|
(enum-set-subset? (c ’(red blue)) (c ’(blue red)))<br>
|
|
(enum-set-subset? (c ’(red blue)) (c ’(red)))<br>
|
|
(enum-set=? (c ’(red blue)) (c ’(blue red)))))<br>
|
|
⇒ (<tt>#t</tt> <tt>#f</tt> <tt>#t</tt> <tt>#t</tt> <tt>#f</tt> <tt>#t</tt>)<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_1252"></a>enum-set-union<i> <i>enum-set<sub>1</sub></i> <i>enum-set<sub>2</sub></i></i>)</tt> procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_1254"></a>enum-set-intersection<i> <i>enum-set<sub>1</sub></i> <i>enum-set<sub>2</sub></i></i>)</tt> procedure </div>
|
|
|
|
<div align=left><tt>(<a name="node_idx_1256"></a>enum-set-difference<i> <i>enum-set<sub>1</sub></i> <i>enum-set<sub>2</sub></i></i>)</tt> procedure </div>
|
|
<p>
|
|
<i>Enum-set<sub>1</sub></i> and <i>enum-set<sub>2</sub></i> must be enumeration sets
|
|
that have the same enumeration type.</p>
|
|
<p>
|
|
The <tt>enum-set-union</tt> procedure returns the union of <i>enum-set<sub>1</sub></i> and <i>enum-set<sub>2</sub></i>.
|
|
The <tt>enum-set-intersection</tt> procedure returns the intersection of <i>enum-set<sub>1</sub></i> and <i>enum-set<sub>2</sub></i>.
|
|
The <tt>enum-set-difference</tt> procedure returns the difference of <i>enum-set<sub>1</sub></i>
|
|
and <i>enum-set<sub>2</sub></i>.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let* ((e (make-enumeration ’(red green blue)))<br>
|
|
(c (enum-set-constructor e)))<br>
|
|
(list (enum-set->list<br>
|
|
(enum-set-union (c ’(blue)) (c ’(red))))<br>
|
|
(enum-set->list<br>
|
|
(enum-set-intersection (c ’(red green))<br>
|
|
(c ’(red blue))))<br>
|
|
(enum-set->list<br>
|
|
(enum-set-difference (c ’(red green))<br>
|
|
(c ’(red blue))))))<br>
|
|
<br> ⇒ ((red blue) (red) (green))<br>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_1258"></a>enum-set-complement<i> enum-set</i>)</tt> procedure </div>
|
|
<p>
|
|
Returns <i>enum-set</i>’s
|
|
complement with respect to its universe.</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let* ((e (make-enumeration ’(red green blue)))<br>
|
|
(c (enum-set-constructor e)))<br>
|
|
(enum-set->list<br>
|
|
(enum-set-complement (c ’(red)))))<br>
|
|
⇒ (green blue)<br>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div align=left><tt>(<a name="node_idx_1260"></a>enum-set-projection<i> <i>enum-set<sub>1</sub></i> <i>enum-set<sub>2</sub></i></i>)</tt> procedure </div>
|
|
<p>
|
|
Projects <i>enum-set<sub>1</sub></i> into the universe
|
|
of <i>enum-set<sub>2</sub></i>, dropping any elements of <i>enum-set<sub>1</sub></i> that do
|
|
not belong to the universe of <i>enum-set<sub>2</sub></i>. (If <i>enum-set<sub>1</sub></i>
|
|
is a subset of the universe of its second, no elements are
|
|
dropped, and the injection is returned.)</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(let ((e1 (make-enumeration<br>
|
|
’(red green blue black)))<br>
|
|
(e2 (make-enumeration<br>
|
|
’(red black white))))<br>
|
|
(enum-set->list<br>
|
|
(enum-set-projection e1 e2))))<br>
|
|
⇒ (red black)<br>
|
|
<p></tt>
|
|
</p>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p>
|
|
</p>
|
|
<div align=left><tt>(define-enumeration <type-name></tt> syntax </div>
|
|
|
|
<a name="node_idx_1262"></a><tt>(<symbol> <tt>...</tt>)<br>
|
|
|
|
<constructor-syntax>)</tt><p>
|
|
The <tt>define-enumeration</tt> form defines an enumeration type and
|
|
provides two macros for constructing its members and sets of its
|
|
members.</p>
|
|
<p>
|
|
A <tt>define-enumeration</tt> form is a definition and can appear
|
|
anywhere any other <definition> can appear.</p>
|
|
<p>
|
|
<Type-name> is an identifier that is bound as a syntactic keyword;
|
|
<symbol> <tt>...</tt> are the symbols that comprise the
|
|
universe of the enumeration (in order).</p>
|
|
<p>
|
|
<tt>(<type-name> <symbol>)</tt> checks at macro-expansion
|
|
time whether the name of <symbol> is in the universe associated with
|
|
<type-name>. If it is, <tt>(<type-name>
|
|
<symbol>)</tt> is equivalent to <tt><symbol></tt>.
|
|
It is a syntax violation if it is not.</p>
|
|
<p>
|
|
<Constructor-syntax> is an identifier that is bound to a
|
|
macro that, given any finite sequence of the symbols in the universe,
|
|
possibly with duplicates, expands into an expression that evaluates
|
|
to the enumeration set of those symbols.</p>
|
|
<p>
|
|
<tt>(<constructor-syntax> <symbol> <tt>...</tt>)</tt> checks at
|
|
macro-expansion time whether every <symbol> <tt>...</tt> is in the
|
|
universe associated with <type-name>. It is a syntax violation
|
|
if one or more is not.
|
|
Otherwise
|
|
</p>
|
|
|
|
<tt>(<constructor-syntax> <symbol> <tt>...</tt>)<br>
|
|
<p></tt>
|
|
is equivalent to
|
|
</p>
|
|
|
|
<tt>((enum-set-constructor (<constructor-syntax>))<br>
|
|
’(<symbol> <tt>...</tt>)).<br>
|
|
<p></tt></p>
|
|
<p>
|
|
Example:</p>
|
|
<p>
|
|
</p>
|
|
|
|
<tt>(define-enumeration color<br>
|
|
(black white purple maroon)<br>
|
|
color-set)<br>
|
|
<br>
|
|
(color black) ⇒ black<br>
|
|
(color purpel) ⇒ <tt> &syntax</tt> <i>exception</i><br>
|
|
(enum-set->list (color-set)) ⇒ ()<br>
|
|
(enum-set->list<br>
|
|
(color-set maroon white)) ⇒ (white maroon)<br>
|
|
<p></tt></p>
|
|
<p>
|
|
</p>
|
|
<blockquote><em>Note: </em>
|
|
In <tt>(<type-name> <symbol>)</tt> and <tt>(<constructor-syntax> <symbol> <tt>...</tt>)</tt> forms,
|
|
only the names of the <symbol>s are significant.
|
|
</blockquote>
|
|
<p></p>
|
|
<p>
|
|
</p>
|
|
<p>
|
|
</p>
|
|
<p></p>
|
|
<div class=smallskip></div>
|
|
<p style="margin-top: 0pt; margin-bottom: 0pt">
|
|
<div align=right class=navigation>[Go to <span><a href="r6rs-lib.html">first</a>, <a href="r6rs-lib-Z-H-14.html">previous</a></span><span>, <a href="r6rs-lib-Z-H-16.html">next</a></span> page<span>; </span><span><a href="r6rs-lib-Z-H-1.html#node_toc_start">contents</a></span><span><span>; </span><a href="r6rs-lib-Z-H-21.html#node_index_start">index</a></span>]</div>
|
|
</p>
|
|
<p></p>
|
|
</div>
|
|
</body>
|
|
</html>
|