1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
<!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>
|