/usr/share/doc/racket/teachpack/2htdpuniverse.html is in racket-doc 6.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><title>2.4 Worlds and the Universe: "universe.rkt"</title><link rel="stylesheet" type="text/css" href="../scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="../racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../doc-site.css" title="default"/><script type="text/javascript" src="../scribble-common.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../doc-site.js"></script><script type="text/javascript" src="../local-redirect/local-redirect.js"></script><script type="text/javascript" src="../local-redirect/local-user-redirect.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="doc-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_0");">►</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x"><span style="font-style: italic">How to Design Programs</span> Teachpacks</a></td></tr></table></div><div class="tocviewsublisttop" style="display: none;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1 </td><td><a href="htdp.html" class="tocviewlink" data-pltdoc="x">Ht<span class="mywbr"> </span>DP Teachpacks</a></td></tr><tr><td align="right">2 </td><td><a href="2htdp2htdp.html" class="tocviewselflink" data-pltdoc="x">Ht<span class="mywbr"> </span>DP/<span class="mywbr"> </span>2e Teachpacks</a></td></tr></table></div></div><div class="tocviewlist"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_1");">▼</a></td><td>2 </td><td><a href="2htdp2htdp.html" class="tocviewlink" data-pltdoc="x">Ht<span class="mywbr"> </span>DP/<span class="mywbr"> </span>2e Teachpacks</a></td></tr></table><div class="tocviewsublist" style="display: block;" id="tocview_1"><table cellspacing="0" cellpadding="0"><tr><td align="right">2.1 </td><td><a href="2htdpbatch-io.html" class="tocviewlink" data-pltdoc="x">Batch Input/<span class="mywbr"> </span>Output:<span class="mywbr"> </span> <span class="stt">"batch-<wbr></wbr>io.rkt"</span><a name="(idx._(gentag._20._(lib._teachpack/teachpack..scrbl)._2htdp))"></a></a></td></tr><tr><td align="right">2.2 </td><td><a href="2htdpimage-guide.html" class="tocviewlink" data-pltdoc="x">Image Guide</a></td></tr><tr><td align="right">2.3 </td><td><a href="2htdpimage.html" class="tocviewlink" data-pltdoc="x">Images:<span class="mywbr"> </span> <span class="stt">"image.rkt"</span><a name="(idx._(gentag._21._(lib._teachpack/teachpack..scrbl)._2htdp))"></a></a></td></tr><tr><td align="right">2.4 </td><td><a href="" class="tocviewselflink" data-pltdoc="x">Worlds and the Universe:<span class="mywbr"> </span> <span class="stt">"universe.rkt"</span><a name="(idx._(gentag._22._(lib._teachpack/teachpack..scrbl)._2htdp))"></a></a></td></tr><tr><td align="right">2.5 </td><td><a href="2htdpPlanet_Cute_Images.html" class="tocviewlink" data-pltdoc="x">Planet Cute Images</a></td></tr><tr><td align="right">2.6 </td><td><a href="2htdphtdp-port.html" class="tocviewlink" data-pltdoc="x">Porting World Programs to Universe</a></td></tr></table></div></div><div class="tocviewlist"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_2");">►</a></td><td>2.4 </td><td><a href="" class="tocviewselflink" data-pltdoc="x">Worlds and the Universe:<span class="mywbr"> </span> <span class="stt">"universe.rkt"</span><a name="(idx._(gentag._22._(lib._teachpack/teachpack..scrbl)._2htdp))"></a></a></td></tr></table><div class="tocviewsublistbottom" style="display: none;" id="tocview_2"><table cellspacing="0" cellpadding="0"><tr><td align="right">2.4.1 </td><td><a href="#%28part._scene%29" class="tocviewlink" data-pltdoc="x">Background</a></td></tr><tr><td align="right">2.4.2 </td><td><a href="#%28part._simulations%29" class="tocviewlink" data-pltdoc="x">Simple Simulations</a></td></tr><tr><td align="right">2.4.3 </td><td><a href="#%28part._world._interactive%29" class="tocviewlink" data-pltdoc="x">Interactions</a></td></tr><tr><td align="right">2.4.4 </td><td><a href="#%28part._world-example%29" class="tocviewlink" data-pltdoc="x">A First Sample World</a></td></tr><tr><td align="right">2.4.5 </td><td><a href="#%28part._universe._world2%29" class="tocviewlink" data-pltdoc="x">The World is not Enough</a></td></tr><tr><td align="right">2.4.6 </td><td><a href="#%28part._universe-server%29" class="tocviewlink" data-pltdoc="x">The Universe Server</a></td></tr><tr><td align="right">2.4.7 </td><td><a href="#%28part._universe-sample%29" class="tocviewlink" data-pltdoc="x">A First Sample Universe</a></td></tr></table></div></div></div><div class="tocsub"><div class="tocsubtitle">On this page:</div><table class="tocsublist" cellspacing="0"><tr><td><span class="tocsublinknumber">2.4.1<tt> </tt></span><a href="#%28part._scene%29" class="tocsubseclink" data-pltdoc="x">Background</a></td></tr><tr><td><span class="tocsublinknumber">2.4.2<tt> </tt></span><a href="#%28part._simulations%29" class="tocsubseclink" data-pltdoc="x">Simple Simulations</a></td></tr><tr><td><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">animate</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._run-simulation%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">run-<wbr></wbr>simulation</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._run-movie%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">run-<wbr></wbr>movie</span></span></a></td></tr><tr><td><span class="tocsublinknumber">2.4.3<tt> </tt></span><a href="#%28part._world._interactive%29" class="tocsubseclink" data-pltdoc="x">Interactions</a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">big-<wbr></wbr>bang</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">to-<wbr></wbr>draw</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-draw%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">on-<wbr></wbr>draw</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">on-<wbr></wbr>tick</span></span></a></td></tr><tr><td><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key-event~3f%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">key-<wbr></wbr>event?</span></span></a></td></tr><tr><td><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key~3d~3f%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">key=?</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-key%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">on-<wbr></wbr>key</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-release%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">on-<wbr></wbr>release</span></span></a></td></tr><tr><td><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad-event~3f%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">pad-<wbr></wbr>event?</span></span></a></td></tr><tr><td><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad~3d~3f%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">pad=?</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-pad%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">on-<wbr></wbr>pad</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad-handler%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">pad-<wbr></wbr>handler</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._up%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">up</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._down%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">down</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._left%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">left</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._right%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">right</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._space%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">space</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._shift%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">shift</span></span></a></td></tr><tr><td><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._mouse-event~3f%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">mouse-<wbr></wbr>event?</span></span></a></td></tr><tr><td><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._mouse~3d~3f%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">mouse=?</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-mouse%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">on-<wbr></wbr>mouse</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">stop-<wbr></wbr>when</span></span></a></td></tr><tr><td><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-with%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">stop-<wbr></wbr>with</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._check-with%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">check-<wbr></wbr>with</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._record~3f%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">record?</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._state%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">state</span></span></a></td></tr><tr><td><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._name%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">name</span></span></a></td></tr><tr><td><span class="tocsublinknumber">2.4.4<tt> </tt></span><a href="#%28part._world-example%29" class="tocsubseclink" data-pltdoc="x">A First Sample World</a></td></tr><tr><td><span class="tocsublinknumber">2.4.4.1<tt> </tt></span><a href="#%28part._.Understanding_a_.Door%29" class="tocsubseclink" data-pltdoc="x">Understanding a Door</a></td></tr><tr><td><span class="tocsublinknumber">2.4.4.2<tt> </tt></span><a href="#%28part._.Hints_on_.Designing_.Worlds%29" class="tocsubseclink" data-pltdoc="x">Hints on Designing Worlds</a></td></tr><tr><td><span class="tocsublinknumber">2.4.5<tt> </tt></span><a href="#%28part._universe._world2%29" class="tocsubseclink" data-pltdoc="x">The World is not Enough</a></td></tr><tr><td><span class="tocsublinknumber">2.4.5.1<tt> </tt></span><a href="#%28part._universe._.Messages%29" class="tocsubseclink" data-pltdoc="x">Messages</a></td></tr><tr><td><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._sexp~3f%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">sexp?</span></span></a></td></tr><tr><td><span class="tocsublinknumber">2.4.5.2<tt> </tt></span><a href="#%28part._universe._.Sending_.Messages%29" class="tocsubseclink" data-pltdoc="x">Sending Messages</a></td></tr><tr><td><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._package~3f%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">package?</span></span></a></td></tr><tr><td><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._make-package%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">make-<wbr></wbr>package</span></span></a></td></tr><tr><td><span class="tocsublinknumber">2.4.5.3<tt> </tt></span><a href="#%28part._universe._.Connecting_with_the_.Universe%29" class="tocsubseclink" data-pltdoc="x">Connecting with the Universe</a></td></tr><tr><td><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._.L.O.C.A.L.H.O.S.T%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">LOCALHOST</span></span></a></td></tr><tr><td><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._register%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">register</span></span></a></td></tr><tr><td><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._port%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">port</span></span></a></td></tr><tr><td><span class="tocsublinknumber">2.4.5.4<tt> </tt></span><a href="#%28part._universe._.Receiving_.Messages%29" class="tocsubseclink" data-pltdoc="x">Receiving Messages</a></td></tr><tr><td><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._on-receive%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">on-<wbr></wbr>receive</span></span></a></td></tr><tr><td><span class="tocsublinknumber">2.4.6<tt> </tt></span><a href="#%28part._universe-server%29" class="tocsubseclink" data-pltdoc="x">The Universe Server</a></td></tr><tr><td><span class="tocsublinknumber">2.4.6.1<tt> </tt></span><a href="#%28part._.Worlds_and_.Messages%29" class="tocsubseclink" data-pltdoc="x">Worlds and Messages</a></td></tr><tr><td><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3f%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">iworld?</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3d~3f%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">iworld=?</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld-name%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">iworld-<wbr></wbr>name</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld1%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">iworld1</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld2%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">iworld2</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld3%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">iworld3</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._bundle~3f%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">bundle?</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._make-bundle%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">make-<wbr></wbr>bundle</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._mail~3f%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">mail?</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._make-mail%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">make-<wbr></wbr>mail</span></span></a></td></tr><tr><td><span class="tocsublinknumber">2.4.6.2<tt> </tt></span><a href="#%28part._.Universe_.Descriptions%29" class="tocsubseclink" data-pltdoc="x">Universe Descriptions</a></td></tr><tr><td><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._universe%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">universe</span></span></a></td></tr><tr><td><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._on-new%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">on-<wbr></wbr>new</span></span></a></td></tr><tr><td><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._on-msg%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">on-<wbr></wbr>msg</span></span></a></td></tr><tr><td><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._on-disconnect%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">on-<wbr></wbr>disconnect</span></span></a></td></tr><tr><td><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._to-string%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">to-<wbr></wbr>string</span></span></a></td></tr><tr><td><span class="tocsublinknumber">2.4.6.3<tt> </tt></span><a href="#%28part._.Exploring_a_.Universe%29" class="tocsubseclink" data-pltdoc="x">Exploring a Universe</a></td></tr><tr><td><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._launch-many-worlds%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">launch-<wbr></wbr>many-<wbr></wbr>worlds</span></span></a></td></tr><tr><td><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._launch-many-worlds%2Fproc%29%29" class="tocsubnonseclink" data-pltdoc="x"><span class="RktSym"><span class="RktValLink">launch-<wbr></wbr>many-<wbr></wbr>worlds/<span class="mywbr"> </span>proc</span></span></a></td></tr><tr><td><span class="tocsublinknumber">2.4.7<tt> </tt></span><a href="#%28part._universe-sample%29" class="tocsubseclink" data-pltdoc="x">A First Sample Universe</a></td></tr><tr><td><span class="tocsublinknumber">2.4.7.1<tt> </tt></span><a href="#%28part._.Two_.Ball_.Tossing_.Worlds%29" class="tocsubseclink" data-pltdoc="x">Two Ball Tossing Worlds</a></td></tr><tr><td><span class="tocsublinknumber">2.4.7.2<tt> </tt></span><a href="#%28part._.Hints_on_.Designing_.Universes%29" class="tocsubseclink" data-pltdoc="x">Hints on Designing Universes</a></td></tr><tr><td><span class="tocsublinknumber">2.4.7.3<tt> </tt></span><a href="#%28part._.Designing_the_.Ball_.Universe%29" class="tocsubseclink" data-pltdoc="x">Designing the Ball Universe</a></td></tr><tr><td><span class="tocsublinknumber">2.4.7.4<tt> </tt></span><a href="#%28part._.Designing_the_.Ball_.Server%29" class="tocsubseclink" data-pltdoc="x">Designing the Ball Server</a></td></tr><tr><td><span class="tocsublinknumber">2.4.7.5<tt> </tt></span><a href="#%28part._.Designing_the_.Ball_.World%29" class="tocsubseclink" data-pltdoc="x">Designing the Ball World</a></td></tr></table></div></div><div class="maincolumn"><div class="main"><div class="navsettop"><span class="navleft"><form class="searchform"><input class="searchbox" style="color: #888;" type="text" value="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, "6.1", "../");" onfocus="this.style.color="black"; this.style.textAlign="left"; if (this.value == "...search manuals...") this.value="";" onblur="if (this.value.match(/^ *$/)) { this.style.color="#888"; this.style.textAlign="center"; this.value="...search manuals..."; }"/></form> <a href="../index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot("6.1");">top</a></span><span class="navright"> <a href="2htdpimage.html" title="backward to "2.3 Images: "image.rkt""" data-pltdoc="x">← prev</a> <a href="2htdp2htdp.html" title="up to "2 HtDP/2e Teachpacks"" data-pltdoc="x">up</a> <a href="2htdpPlanet_Cute_Images.html" title="forward to "2.5 Planet Cute Images"" data-pltdoc="x">next →</a></span> </div><h4 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""universe"">2.4<tt> </tt><a name="(part._universe)"></a><a name="(mod-path._2htdp/universe)"></a>Worlds and the Universe: <span class="stt">"universe.rkt"</span><a name="(idx._(gentag._22._(lib._teachpack/teachpack..scrbl)._2htdp))"></a></h4><div class="SAuthorListBox"><span class="SAuthorList"><p class="author">Matthias Felleisen</p></span></div><p><table cellspacing="0" cellpadding="0" class="defmodule"><tr><td align="left"><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._require%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">require</a></span><span class="stt"> </span><a href="" class="RktModLink" data-pltdoc="x"><span class="RktSym">2htdp/universe</span></a><span class="RktPn">)</span></td><td align="right"><span class="RpackageSpec"><span class="Smaller"> package:</span> <span class="stt">htdp-lib</span></span></td></tr></table></p><p>The <span class="stt">universe.rkt</span> teachpack implements and provides the functionality
for creating interactive, graphical programs that consist of plain
mathematical functions. We refer to such programs as <a name="(tech._world)"></a><span style="font-style: italic">world</span>
programs. In addition, world programs can also become a part of a
<a name="(tech._universe)"></a><span style="font-style: italic">universe</span>, a collection of worlds that can exchange messages.</p><p>The purpose of this documentation is to give experienced Racketeers and HtDP
teachers a concise overview for using the library. The first part of the
documentation focuses on <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> programs. Section
<a href="#%28part._world-example%29" data-pltdoc="x">A First Sample World</a> presents an illustration of how to design such
programs for a simple domain; it is suited for a novice who knows how to
design conditional functions for enumerations, intervals, and unions. The
second half of the documentation focuses on "universe" programs: how it is
managed via a server, how <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> programs register with the server,
etc. The last two sections show how to design a simple universe of two
communicating worlds.</p><p><span style="font-style: italic">Note</span>: For a quick and educational introduction to just worlds, see
<a href="http://www.ccs.neu.edu/home/matthias/HtDP2e/prologue.html">How
to Design Programs, Second Edition: Prologue</a>. As of August 2008, we also
have a series of projects available as a small booklet on
<a href="http://world.cs.brown.edu/">How to Design Worlds</a>.</p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""scene"">2.4.1<tt> </tt><a name="(part._scene)"></a>Background</h5><p><div class="SIntrapara">The universe teachpack assumes working knowledge of the basic image
manipulation operations, either <a href="image.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">htdp/image</span></a> or
<a href="2htdpimage.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">2htdp/image</span></a>. As far as this extended reference is
concerned, the major difference between the two image teachpacks is
the assumption that
</div><div class="SIntrapara"><blockquote><p><a href="image.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">htdp/image</span></a> programs render their state as <span style="font-style: italic">scenes</span>,
i.e., images that satisfy the <span class="RktSym">scene?</span> predicate.</p></blockquote></div><div class="SIntrapara">Recall that <a href="image.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">htdp/image</span></a> defines a scene to be an image whose
pinhole is at <span style="font-style: italic"></span>(<span style="font-style: italic"></span>0<span style="font-style: italic">,</span>0<span style="font-style: italic"></span>)<span style="font-style: italic"></span>. If your program uses the operations of
<a href="2htdpimage.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">2htdp/image</span></a>, all images are also scenes.</div></p><p>While the operations of this teachpack work with both image teachpacks, we
hope to eliminate <a href="image.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">htdp/image</span></a> in the not-too-distant future.
All example programs are already written using <a href="2htdpimage.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">2htdp/image</span></a>
operations. We urge programmers to use <a href="2htdpimage.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">2htdp/image</span></a> when
they design new “world” and “universe” programs and to rewrite their
existing <a href="image.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">htdp/image</span></a> programs to use
<a href="2htdpimage.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">2htdp/image</span></a>.</p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""simulations"">2.4.2<tt> </tt><a name="(part._simulations)"></a>Simple Simulations</h5><p>The simplest kind of animated <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program is a time-based
simulation, which is a series of images. The programmer’s task is to
supply a function that creates an image for each natural number. Handing
this function to the teachpack displays the simulation.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((lib._2htdp/universe..rkt)._animate))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValDef RktValLink" data-pltdoc="x">animate</a></span></span><span class="hspace"> </span><span class="RktVar">create-image</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._natural-number%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">natural-number/c</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">create-image</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._natural-number%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">natural-number/c</a></span><span class="hspace"> </span><span class="RktSym">scene?</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara"><span class="refelem"><span class="refcolumn"><span class="refcontent">See <a href="#%28part._scene%29" data-pltdoc="x">Background</a> for <span class="RktSym">scene?</span>.</span></span></span>
opens a canvas and starts a clock that ticks 28 times per second. Every
time the clock ticks, DrRacket applies <span class="RktVar">create-image</span> to the
number of ticks passed since this function call. The results of these
function calls are displayed in the canvas. The simulation runs until you
click the <span class="stt">Stop</span> button in DrRacket or close the window. At that
point, <span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span> returns the number of ticks that have
passed.</div></p><p><div class="SIntrapara">Example:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">create-UFO-scene</span><span class="hspace"> </span><span class="RktSym">height</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._underlay%2Fxy%29%29" class="RktValLink" data-pltdoc="x">underlay/xy</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._rectangle%29%29" class="RktValLink" data-pltdoc="x">rectangle</a></span><span class="hspace"> </span><span class="RktVal">100</span><span class="hspace"> </span><span class="RktVal">100</span><span class="hspace"> </span><span class="RktVal">"solid"</span><span class="hspace"> </span><span class="RktVal">"white"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">50</span><span class="hspace"> </span><span class="RktSym">height</span><span class="hspace"> </span><span class="RktSym">UFO</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">UFO</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._underlay%2Falign%29%29" class="RktValLink" data-pltdoc="x">underlay/align</a></span><span class="hspace"> </span><span class="RktVal">"center"</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"center"</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._circle%29%29" class="RktValLink" data-pltdoc="x">circle</a></span><span class="hspace"> </span><span class="RktVal">10</span><span class="hspace"> </span><span class="RktVal">"solid"</span><span class="hspace"> </span><span class="RktVal">"green"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._rectangle%29%29" class="RktValLink" data-pltdoc="x">rectangle</a></span><span class="hspace"> </span><span class="RktVal">40</span><span class="hspace"> </span><span class="RktVal">4</span><span class="hspace"> </span><span class="RktVal">"solid"</span><span class="hspace"> </span><span class="RktVal">"green"</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span><span class="hspace"> </span><span class="RktSym">create-UFO-scene</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((lib._2htdp/universe..rkt)._run-simulation))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._run-simulation%29%29" class="RktValDef RktValLink" data-pltdoc="x">run-simulation</a></span></span><span class="hspace"> </span><span class="RktVar">create-image</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528lib._racket%252Fbool..rkt%2529._true%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">true</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">create-image</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._natural-number%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">natural-number/c</a></span><span class="hspace"> </span><span class="RktSym">scene?</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara"><span class="refelem"><span class="refcolumn"><span class="refcontent">See <a href="#%28part._scene%29" data-pltdoc="x">Background</a> for <span class="RktSym">scene?</span>.</span></span></span>
<span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span> was originally called <span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._run-simulation%29%29" class="RktValLink" data-pltdoc="x">run-simulation</a></span>, and this
binding is retained for backwards compatibility</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((lib._2htdp/universe..rkt)._run-movie))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._run-movie%29%29" class="RktValDef RktValLink" data-pltdoc="x">run-movie</a></span></span><span class="hspace"> </span><span class="RktVar">r</span><span class="hspace"> </span><span class="RktVar">m</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528lib._racket%252Fbool..rkt%2529._true%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">true</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">r</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._and%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">and/c</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._real%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">real?</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._positive%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">positive?</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">m</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym">Listof</span><span class="hspace"> </span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image~3f%29%29" class="RktValLink" data-pltdoc="x">image?</a></span><span class="RktPn">]</span></td></tr></table></blockquote></div><div class="SIntrapara"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._run-movie%29%29" class="RktValLink" data-pltdoc="x">run-movie</a></span> displays the list of images <span class="RktVar">m</span>, spending
<span class="RktVar">r</span> seconds per image.</div></p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""interactive"">2.4.3<tt> </tt><a name="(part._world._interactive)"></a>Interactions</h5><p>The step from simulations to interactive programs is relatively
small. Roughly speaking, a simulation designates one function,
<span class="RktVar">create-image</span>, as a handler for one kind of event: clock ticks. In
addition to clock ticks, <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> programs can also deal with two
other kinds of events: keyboard events and mouse events. A keyboard event
is triggered when a computer user presses a key on the
keyboard. Similarly, a mouse event is the movement of the mouse, a click
on a mouse button, the crossing of a boundary by a mouse movement, etc.</p><p>Your program may deal with such events via the <span style="font-style: italic">designation</span> of
<span style="font-style: italic">handler</span> functions. Specifically, the teachpack provides for the
installation of four event handlers: <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span>, <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-key%29%29" class="RktStxLink" data-pltdoc="x">on-key</a></span>,
<span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-mouse%29%29" class="RktStxLink" data-pltdoc="x">on-mouse</a></span>, and <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-pad%29%29" class="RktStxLink" data-pltdoc="x">on-pad</a></span>. In addition, a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>
program must specify a
<span class="RktSym">draw</span> function, which is called every time your program should
visualize the current world, and a <span class="RktSym">done</span> predicate, which is used
to determine when the <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program should shut down.</p><p>Each handler function consumes the current state of the <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> and
optionally a data representation of the event. It produces a new state of
the <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>.</p><p>The following picture provides an intuitive overview of the workings of a
<a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program in the form of a state transition diagram.</p><p><img src="nuworld.png" alt="" width="722" height="400"/></p><p>The <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> form installs <span class="RktSym">World_0</span> as the initial <a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a>.
The handlers <span class="RktSym">tock</span>, <span class="RktSym">react</span>, and <span class="RktSym">click</span> transform
one world into another one; each time an event is handled, <span class="RktSym">done</span> is
used to check whether the world is final, in which case the program is
shut down; and finally, <span class="RktSym">draw</span> renders each world as an image, which
is then displayed on an external canvas.</p><p><a name="(tech._world._worldstate)"></a><span style="font-style: italic">WorldState</span> : <span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">any/c</a></span></p><p>The design of a world program demands that you come up with a data
definition of all possible states. We use <a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a> to refer to
this collection of data, using a capital W to distinguish it from the
program. In principle, there are no constraints on this data
definition though it mustn’t be an instance of the <a href="#%28tech._universe._package%29" class="techoutside" data-pltdoc="x"><span class="techinside">Package</span></a>
structure (see below). You can even keep it implicit, even if this
violates the Design Recipe.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._big-bang))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">big-bang</a></span></span><span class="hspace"> </span><span class="RktVar">state-expr</span><span class="hspace"> </span><span class="RktVar">clause</span><span class="hspace"> </span><span class="RktMeta">...</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="specgrammar"><tr><td align="right" valign="baseline"><span class="RktVar">clause</span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">=</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace"> </span><span class="RktVar">tick-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace"> </span><span class="RktVar">tick-expr</span><span class="hspace"> </span><span class="RktVar">rate-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace"> </span><span class="RktVar">tick-expr</span><span class="hspace"> </span><span class="RktVar">rate-expr</span><span class="hspace"> </span><span class="RktVar">limit-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-key%29%29" class="RktStxLink" data-pltdoc="x">on-key</a></span><span class="hspace"> </span><span class="RktVar">key-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-pad%29%29" class="RktStxLink" data-pltdoc="x">on-pad</a></span><span class="hspace"> </span><span class="RktVar">pad-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-release%29%29" class="RktStxLink" data-pltdoc="x">on-release</a></span><span class="hspace"> </span><span class="RktVar">release-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-mouse%29%29" class="RktStxLink" data-pltdoc="x">on-mouse</a></span><span class="hspace"> </span><span class="RktVar">mouse-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace"> </span><span class="RktVar">draw-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace"> </span><span class="RktVar">draw-expr</span><span class="hspace"> </span><span class="RktVar">width-expr</span><span class="hspace"> </span><span class="RktVar">height-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxLink" data-pltdoc="x">stop-when</a></span><span class="hspace"> </span><span class="RktVar">stop-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxLink" data-pltdoc="x">stop-when</a></span><span class="hspace"> </span><span class="RktVar">stop-expr</span><span class="hspace"> </span><span class="RktVar">last-scene-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._check-with%29%29" class="RktStxLink" data-pltdoc="x">check-with</a></span><span class="hspace"> </span><span class="RktVar">world?-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._record~3f%29%29" class="RktStxLink" data-pltdoc="x">record?</a></span><span class="hspace"> </span><span class="RktVar">r-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._state%29%29" class="RktStxLink" data-pltdoc="x">state</a></span><span class="hspace"> </span><span class="RktVar">expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._on-receive%29%29" class="RktStxLink" data-pltdoc="x">on-receive</a></span><span class="hspace"> </span><span class="RktVar">rec-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._register%29%29" class="RktStxLink" data-pltdoc="x">register</a></span><span class="hspace"> </span><span class="RktVar">IP-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktVar">port</span><span class="hspace"> </span><span class="RktVar">Port-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._name%29%29" class="RktStxLink" data-pltdoc="x">name</a></span><span class="hspace"> </span><span class="RktVar">name-expr</span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">starts a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program in the initial state specified with
<span class="RktVar">state-expr</span>, which must of course evaluate to an element of
<a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a>. Its behavior is specified via the handler functions
designated in the optional <span class="RktSym">spec</span> clauses, especially how the
<a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program deals with clock ticks, with key events, with mouse
events, and eventually with messages from the universe; how it renders
itself as an image; when the program must shut down; where to register the
world with a universe; and whether to record the stream of events. A world
specification may not contain more than one <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span>,
<span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span>, or <span class="RktSym"><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._register%29%29" class="RktStxLink" data-pltdoc="x">register</a></span> clause. A <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>
expression returns the last world when the stop condition is satisfied
(see below) or when the programmer clicks on the <span class="stt">Stop</span> button or
closes the canvas.</div></p><p><div class="SIntrapara">The only mandatory clause of a <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> description is
<span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span> (or <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-draw%29%29" class="RktStxLink" data-pltdoc="x">on-draw</a></span> for backwards compatibility):
</div><div class="SIntrapara"><ul><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._to-draw))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">to-draw</a></span></span><span class="hspace"> </span><span class="RktVar">render-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">render-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><span class="RktSym">scene?</span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara"><span class="refelem"><span class="refcolumn"><span class="refcontent">See <a href="#%28part._scene%29" data-pltdoc="x">Background</a> for <span class="RktSym">scene?</span>.</span></span></span>
tells DrRacket to call the function <span class="RktVar">render-expr</span> whenever the
canvas must be drawn. The external canvas is usually re-drawn after DrRacket has
dealt with an event. Its size is determined by the size of the first
generated image.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace"> </span><span class="RktVar">render-expr</span><span class="hspace"> </span><span class="RktVar">width-expr</span><span class="hspace"> </span><span class="RktVar">height-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><blockquote class="SubFlow"><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">render-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><span class="RktSym">scene?</span><span class="RktPn">)</span></td></tr></table><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">width-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._natural-number%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">natural-number/c</a></span></td></tr></table><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">height-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._natural-number%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">natural-number/c</a></span></td></tr></table></blockquote></td></tr></table></blockquote></div><div class="SIntrapara"><span class="refelem"><span class="refcolumn"><span class="refcontent">See <a href="#%28part._scene%29" data-pltdoc="x">Background</a> for <span class="RktSym">scene?</span>.</span></span></span>
tells DrRacket to use a <span class="RktVar">width-expr</span> by <span class="RktVar">height-expr</span>
canvas instead of one determine by the first generated image.</div></p><p>For compatibility reasons, the teachpack also supports the keyword
<a name="(form._world._((lib._2htdp/universe..rkt)._on-draw))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-draw%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">on-draw</a></span></span> in lieu of <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span> but the latter is preferred
now.</p></li></ul></div><div class="SIntrapara">All remaining clauses are optional. To introduce them, we need one more
data definition:</div></p><p><a name="(tech._world._handlerresult)"></a><span style="font-style: italic">HandlerResult</span> : is a synonym for <a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a> until <a href="#%28part._universe._world2%29" data-pltdoc="x">The World is not Enough</a></p><ul><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._on-tick))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">on-tick</a></span></span><span class="hspace"> </span><span class="RktVar">tick-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">tick-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to call the <span class="RktVar">tick-expr</span> function on the current
world every time the clock ticks. The result of the call becomes the
current world. The clock ticks at the rate of 28 times per second.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace"> </span><span class="RktVar">tick-expr</span><span class="hspace"> </span><span class="RktVar">rate-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><blockquote class="SubFlow"><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">tick-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a><span class="RktPn">)</span></td></tr></table><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">rate-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._and%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">and/c</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._real%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">real?</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._positive%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">positive?</a></span><span class="RktPn">)</span></td></tr></table></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to call the <span class="RktVar">tick-expr</span> function on the current
world every time the clock ticks. The result of the call becomes the
current world. The clock ticks every <span class="RktVar">rate-expr</span> seconds.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace"> </span><span class="RktVar">tick-expr</span><span class="hspace"> </span><span class="RktVar">rate-expr</span><span class="hspace"> </span><span class="RktVar">limit-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><blockquote class="SubFlow"><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">tick-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a><span class="RktPn">)</span></td></tr></table><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">rate-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._and%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">and/c</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._real%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">real?</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._positive%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">positive?</a></span><span class="RktPn">)</span></td></tr></table><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">limit-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._and%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">and/c</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._integer%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">integer?</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._positive%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">positive?</a></span><span class="RktPn">)</span></td></tr></table></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to call the <span class="RktVar">tick-expr</span> function on the current
world every time the clock ticks. The result of the call becomes the
current world. The clock ticks every <span class="RktVar">rate-expr</span> seconds.
The world ends when the clock has ticked more than <span class="RktVar">limit-expr</span> times.</div></p></li><li><p>A <a href="#%28tech._world._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a> represents key board events.</p><p><a name="(tech._world._keyevent)"></a><span style="font-style: italic">KeyEvent</span> : <span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">string?</a></span></p><p><div class="SIntrapara">For simplicity, we represent key events with strings, but not all strings
are key events. The representation of key events comes in distinct
classes. First, a single-character string is used to signal that the user
has hit a "regular" key, such as
</div><div class="SIntrapara"><ul><li><p><span class="RktVal">"q"</span> stands for the q key;</p></li><li><p><span class="RktVal">"w"</span> stands for the w key;</p></li><li><p><span class="RktVal">"e"</span> stands for the e key;</p></li><li><p><span class="RktVal">"r"</span> stands for the r key; and so on.</p></li></ul></div><div class="SIntrapara">Some of these one-character strings look somewhat unusual:
</div><div class="SIntrapara"><ul><li><p><span class="RktVal">" "</span> stands for the space bar (<span class="RktVal">#\space</span>);</p></li><li><p><span class="RktVal">"\r"</span> stands for the return and enter key (<span class="RktVal">#\return</span>);</p></li><li><p><span class="RktVal">"\t"</span> stands for the tab key (<span class="RktVal">#\tab</span>); and</p></li><li><p><span class="RktVal">"\b"</span> stands for the backspace key (<span class="RktVal">#\backspace</span>).</p></li></ul></div><div class="SIntrapara">Here is "proof" that these strings really have length 1:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-length%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">string-length</a></span><span class="hspace"> </span><span class="RktVal">"\t"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">1</span></p></td></tr></table></blockquote></div><div class="SIntrapara">On rare occasions your programs may also encounter <span class="RktVal">"\u007F"</span>,
which is the string representing the delete key (aka rubout).</div></p><p><div class="SIntrapara">Second, some keys have multiple-character string representations. Strings
with more than one character denote arrow keys or other special events,
starting with the four most important ones:
</div><div class="SIntrapara"><ul><li><p><span class="RktVal">"left"</span> is the left arrow;</p></li><li><p><span class="RktVal">"right"</span> is the right arrow;</p></li><li><p><span class="RktVal">"up"</span> is the up arrow;</p></li><li><p><span class="RktVal">"down"</span> is the down arrow;</p></li></ul></div><div class="SIntrapara">Here are some others that you may encounter:
</div><div class="SIntrapara"><ul><li><p><span class="RktVal">"start"</span></p></li><li><p><span class="RktVal">"cancel"</span></p></li><li><p><span class="RktVal">"clear"</span></p></li><li><p><span class="RktVal">"shift"</span></p></li><li><p><span class="RktVal">"rshift"</span></p></li><li><p><span class="RktVal">"control"</span></p></li><li><p><span class="RktVal">"rcontrol"</span></p></li><li><p><span class="RktVal">"menu"</span></p></li><li><p><span class="RktVal">"pause"</span></p></li><li><p><span class="RktVal">"capital"</span></p></li><li><p><span class="RktVal">"prior"</span></p></li><li><p><span class="RktVal">"next"</span></p></li><li><p><span class="RktVal">"end"</span></p></li><li><p><span class="RktVal">"home"</span></p></li><li><p><span class="RktVal">"escape"</span></p></li><li><p><span class="RktVal">"select"</span></p></li><li><p><span class="RktVal">"print"</span></p></li><li><p><span class="RktVal">"execute"</span></p></li><li><p><span class="RktVal">"snapshot"</span></p></li><li><p><span class="RktVal">"insert"</span></p></li><li><p><span class="RktVal">"help"</span></p></li><li><p>function keys:
<span class="RktVal">"f1"</span>,
<span class="RktVal">"f2"</span>,
<span class="RktVal">"f3"</span>,
<span class="RktVal">"f4"</span>,
<span class="RktVal">"f5"</span>,
<span class="RktVal">"f6"</span>,
<span class="RktVal">"f7"</span>,
<span class="RktVal">"f8"</span>,
<span class="RktVal">"f9"</span>,
<span class="RktVal">"f10"</span>,
<span class="RktVal">"f11"</span>,
<span class="RktVal">"f12"</span>,
<span class="RktVal">"f13"</span>,
<span class="RktVal">"f14"</span>,
<span class="RktVal">"f15"</span>,
<span class="RktVal">"f16"</span>,
<span class="RktVal">"f17"</span>,
<span class="RktVal">"f18"</span>,
<span class="RktVal">"f19"</span>,
<span class="RktVal">"f20"</span>,
<span class="RktVal">"f21"</span>,
<span class="RktVal">"f22"</span>,
<span class="RktVal">"f23"</span>,
<span class="RktVal">"f24"</span></p></li><li><p><span class="RktVal">"numlock"</span></p></li><li><p><span class="RktVal">"scroll"</span></p></li></ul></div></p><p><div class="SIntrapara">The following four count as keyevents even though they are triggered by
physical events on some form of mouse:
</div><div class="SIntrapara"><ul><li><p><span class="RktVal">"wheel-up"</span></p></li><li><p><span class="RktVal">"wheel-down"</span></p></li><li><p><span class="RktVal">"wheel-left"</span></p></li><li><p><span class="RktVal">"wheel-right"</span></p></li></ul></div><div class="SIntrapara">The preceding enumeration is neither complete in covering all the events
that this library deals with nor does it specify which events the library
ignores. If you wish to design a program that relies on specific keys on
your keyboard, you should first write a small test program to find out
whether the chosen keystrokes are caught by the library and, if so, which
string representations are used for these events.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._world._((lib._2htdp/universe..rkt)._key-event~3f))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key-event~3f%29%29" class="RktValDef RktValLink" data-pltdoc="x">key-event?</a></span></span><span class="hspace"> </span><span class="RktVar">x</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">x</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">any</a></span></td></tr></table></blockquote></div><div class="SIntrapara">determines whether <span class="RktVar">x</span> is a <a href="#%28tech._world._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a></div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._world._((lib._2htdp/universe..rkt)._key~3d~3f))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key~3d~3f%29%29" class="RktValDef RktValLink" data-pltdoc="x">key=?</a></span></span><span class="hspace"> </span><span class="RktVar">x</span><span class="hspace"> </span><span class="RktVar">y</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">x</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key-event~3f%29%29" class="RktValLink" data-pltdoc="x">key-event?</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">y</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key-event~3f%29%29" class="RktValLink" data-pltdoc="x">key-event?</a></span></td></tr></table></blockquote></div><div class="SIntrapara">compares two <a href="#%28tech._world._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a> for equality</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._on-key))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-key%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">on-key</a></span></span><span class="hspace"> </span><span class="RktVar">key-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">key-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key-event~3f%29%29" class="RktValLink" data-pltdoc="x">key-event?</a></span><span class="hspace"> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to call the <span class="RktVar">key-expr</span> function on the current world and a
<a href="#%28tech._world._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a> for every keystroke the user of the computer makes. The result
of the call becomes the current world.</div></p><p><div class="SIntrapara">Here is a typical key-event handler:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">change</span><span class="hspace"> </span><span class="RktSym">w</span><span class="hspace"> </span><span class="RktSym">a-key</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key~3d~3f%29%29" class="RktValLink" data-pltdoc="x">key=?</a></span><span class="hspace"> </span><span class="RktSym">a-key</span><span class="hspace"> </span><span class="RktVal">"left"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">world-go</span><span class="hspace"> </span><span class="RktSym">w</span><span class="hspace"> </span><span class="RktSym"><span class="nobreak">-D</span>ELTA</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key~3d~3f%29%29" class="RktValLink" data-pltdoc="x">key=?</a></span><span class="hspace"> </span><span class="RktSym">a-key</span><span class="hspace"> </span><span class="RktVal">"right"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">world-go</span><span class="hspace"> </span><span class="RktSym">w</span><span class="hspace"> </span><span class="RktSym">+DELTA</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%7E3d%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">=</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-length%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">string-length</a></span><span class="hspace"> </span><span class="RktSym">a-key</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym">w</span><span class="RktPn">]</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">order-free checking</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key~3d~3f%29%29" class="RktValLink" data-pltdoc="x">key=?</a></span><span class="hspace"> </span><span class="RktSym">a-key</span><span class="hspace"> </span><span class="RktVal">"up"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">world-go</span><span class="hspace"> </span><span class="RktSym">w</span><span class="hspace"> </span><span class="RktSym"><span class="nobreak">-D</span>ELTA</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key~3d~3f%29%29" class="RktValLink" data-pltdoc="x">key=?</a></span><span class="hspace"> </span><span class="RktSym">a-key</span><span class="hspace"> </span><span class="RktVal">"down"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">world-go</span><span class="hspace"> </span><span class="RktSym">w</span><span class="hspace"> </span><span class="RktSym">+DELTA</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._else%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">else</a></span><span class="hspace"> </span><span class="RktSym">w</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">The omitted, auxiliary function <span style="font-style: italic">world-go</span> is supposed to consume a
world and a number and produces a world.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._on-release))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-release%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">on-release</a></span></span><span class="hspace"> </span><span class="RktVar">release-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">release-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key-event~3f%29%29" class="RktValLink" data-pltdoc="x">key-event?</a></span><span class="hspace"> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to call the <span class="RktVar">release-expr</span> function on the current world and a
<a href="#%28tech._world._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a> for every release event on the keyboard. A release event
occurs when a user presses the key and then releases it. The second argument
indicates which key has been released. The result of the function call
becomes the current world.</div></p></li><li><p>A <a href="#%28tech._world._padevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">PadEvent</span></a> is a <a href="#%28tech._world._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a> for a game-pad simulation via
<span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>. The presence of an <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-pad%29%29" class="RktStxLink" data-pltdoc="x">on-pad</a></span> clause superimposes
the game-pad image onto the current image, suitably scaled to its size:</p><p><img src="gamepad.png" alt="" width="631" height="102"/></p><p><a name="(tech._world._padevent)"></a><span style="font-style: italic">PadEvent</span> : <span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key-event~3f%29%29" class="RktValLink" data-pltdoc="x">key-event?</a></span></p><p><div class="SIntrapara">It is one of the following:
</div><div class="SIntrapara"><ul><li><p><span class="RktVal">"left"</span> is the left arrow;</p></li><li><p><span class="RktVal">"right"</span> is the right arrow;</p></li><li><p><span class="RktVal">"up"</span> is the up arrow;</p></li><li><p><span class="RktVal">"down"</span> is the down arrow;</p></li><li><p><span class="RktVal">"w"</span> to be interpreted as up arrow;</p></li><li><p><span class="RktVal">"s"</span> to be interpreted as down arrow;</p></li><li><p><span class="RktVal">"a"</span> to be interpreted as left arrow;</p></li><li><p><span class="RktVal">"d"</span> to be interpreted as right arrow;</p></li><li><p><span class="RktVal">" "</span> is the space bar;</p></li><li><p><span class="RktVal">"shift"</span> is the left shift key;</p></li><li><p><span class="RktVal">"rshift"</span> is the right shift key;</p></li></ul></div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._world._((lib._2htdp/universe..rkt)._pad-event~3f))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad-event~3f%29%29" class="RktValDef RktValLink" data-pltdoc="x">pad-event?</a></span></span><span class="hspace"> </span><span class="RktVar">x</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">x</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">any</a></span></td></tr></table></blockquote></div><div class="SIntrapara">determines whether <span class="RktVar">x</span> is a <a href="#%28tech._world._padevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">PadEvent</span></a></div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._world._((lib._2htdp/universe..rkt)._pad~3d~3f))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad~3d~3f%29%29" class="RktValDef RktValLink" data-pltdoc="x">pad=?</a></span></span><span class="hspace"> </span><span class="RktVar">x</span><span class="hspace"> </span><span class="RktVar">y</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">x</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad-event~3f%29%29" class="RktValLink" data-pltdoc="x">pad-event?</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">y</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad-event~3f%29%29" class="RktValLink" data-pltdoc="x">pad-event?</a></span></td></tr></table></blockquote></div><div class="SIntrapara">compares two <a href="#%28tech._world._padevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">PadEvent</span></a> for equality</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._on-pad))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-pad%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">on-pad</a></span></span><span class="hspace"> </span><span class="RktVar">pad-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">pad-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad-event~3f%29%29" class="RktValLink" data-pltdoc="x">pad-event?</a></span><span class="hspace"> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to call the <span class="RktVar">pad-expr</span> function on the current world and the
<a href="#%28tech._world._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a> for every keystroke that is also a <a href="#%28tech._world._padevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">PadEvent</span></a>. The result
of the call becomes the current world.</div></p><p><div class="SIntrapara">Here is a typical <a href="#%28tech._world._padevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">PadEvent</span></a> handler:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">ComplexNumber PadEvent -> ComplexNumber</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">handle-pad-events</span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktSym">k</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=case.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._case%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">case</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=symbols.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-%7E3esymbol%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">string->symbol</a></span><span class="hspace"> </span><span class="RktSym">k</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._up%29%29" class="RktStxLink" data-pltdoc="x">up</a></span><span class="hspace"> </span><span class="RktSym">w</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._-%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktVal">0+10i</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._down%29%29" class="RktStxLink" data-pltdoc="x">down</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">+</a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktVal">0+10i</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._left%29%29" class="RktStxLink" data-pltdoc="x">left</a></span><span class="hspace"> </span><span class="RktSym">a</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._-%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktVal">10</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._right%29%29" class="RktStxLink" data-pltdoc="x">right</a></span><span class="hspace"> </span><span class="RktSym">d</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">+</a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktVal">10</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym">| |</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym">x0</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._shift%29%29" class="RktStxLink" data-pltdoc="x">shift</a></span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528lib._racket%252Fmath..rkt%2529._conjugate%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">conjugate</a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym">rshift</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-with%29%29" class="RktValLink" data-pltdoc="x">stop-with</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528lib._racket%252Fmath..rkt%2529._conjugate%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">conjugate</a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p>When a <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression specifies an <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-pad%29%29" class="RktStxLink" data-pltdoc="x">on-pad</a></span> clause,
all <a href="#%28tech._world._padevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">PadEvent</span></a>s are sent to the <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-pad%29%29" class="RktStxLink" data-pltdoc="x">on-pad</a></span> handler. All other
key events are discarded, unless an <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-key%29%29" class="RktStxLink" data-pltdoc="x">on-key</a></span> and/or an
<span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-release%29%29" class="RktStxLink" data-pltdoc="x">on-release</a></span> clause are specified, in which case all remaining
<a href="#%28tech._world._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a>s are sent there.</p><p>To facilitate the definition of <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-pad%29%29" class="RktStxLink" data-pltdoc="x">on-pad</a></span> handlers, the library
provides the <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad-handler%29%29" class="RktStxLink" data-pltdoc="x">pad-handler</a></span> form.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._pad-handler))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad-handler%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">pad-handler</a></span></span><span class="hspace"> </span><span class="RktVar">clause</span><span class="hspace"> </span><span class="RktMeta">...</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="specgrammar"><tr><td align="right" valign="baseline"><span class="RktVar">clause</span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">=</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._up%29%29" class="RktStxLink" data-pltdoc="x">up</a></span><span class="hspace"> </span><span class="RktVar">up-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._down%29%29" class="RktStxLink" data-pltdoc="x">down</a></span><span class="hspace"> </span><span class="RktVar">down-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._left%29%29" class="RktStxLink" data-pltdoc="x">left</a></span><span class="hspace"> </span><span class="RktVar">left-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._right%29%29" class="RktStxLink" data-pltdoc="x">right</a></span><span class="hspace"> </span><span class="RktVar">right-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._space%29%29" class="RktStxLink" data-pltdoc="x">space</a></span><span class="hspace"> </span><span class="RktVar">space-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._shift%29%29" class="RktStxLink" data-pltdoc="x">shift</a></span><span class="hspace"> </span><span class="RktVar">shift-expr</span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">Creates a function that deals with <a href="#%28tech._world._padevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">PadEvent</span></a>s. Each (optional) clause
contributes one function that consumes a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">World</span></a> and produces a
world. The name of the clause determines for which kind of <a href="#%28tech._world._padevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">PadEvent</span></a>
the function is called.</div></p><p>Using the form is entirely optional and not required to use
<span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-pad%29%29" class="RktStxLink" data-pltdoc="x">on-pad</a></span>. Indeed, <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad-handler%29%29" class="RktStxLink" data-pltdoc="x">pad-handler</a></span> could be used to define a
plain <a href="#%28tech._world._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a> handler—<wbr></wbr>if we could guarantee that players never
hit keys other than <a href="#%28tech._world._padevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">PadEvent</span></a> keys.</p><p><div class="SIntrapara">All clauses in a <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad-handler%29%29" class="RktStxLink" data-pltdoc="x">pad-handler</a></span> form are optional:
</div><div class="SIntrapara"><ul><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._up))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._up%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">up</a></span></span><span class="hspace"> </span><span class="RktVar">up-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktSym">tick-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">Creates a handler for <span class="RktVal">"up"</span> and <span class="RktVal">"w"</span> events.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._down))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._down%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">down</a></span></span><span class="hspace"> </span><span class="RktVar">down-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktSym">tick-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">Creates a handler for <span class="RktVal">"down"</span> and <span class="RktVal">"s"</span> events.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._left))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._left%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">left</a></span></span><span class="hspace"> </span><span class="RktVar">left-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktSym">tick-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">Creates a handler for <span class="RktVal">"left"</span> and <span class="RktVal">"a"</span> events.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._right))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._right%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">right</a></span></span><span class="hspace"> </span><span class="RktVar">right-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktSym">tick-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">Creates a handler for <span class="RktVal">"right"</span> and <span class="RktVal">"d"</span> events.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._space))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._space%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">space</a></span></span><span class="hspace"> </span><span class="RktVar">space-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktSym">tick-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">Creates a handler for space-bar events (<span class="RktVal">" "</span>).</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._shift))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._shift%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">shift</a></span></span><span class="hspace"> </span><span class="RktVar">shift-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktSym">tick-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">Creates a handler for <span class="RktVal">"shift"</span> and <span class="RktVal">"rshift"</span> events.</div></p></li></ul></div><div class="SIntrapara">If a clause is omitted, <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad-handler%29%29" class="RktStxLink" data-pltdoc="x">pad-handler</a></span> installs a default function
that maps the existing world to itself.</div></p><p><div class="SIntrapara">Here is a <a href="#%28tech._world._padevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">PadEvent</span></a> handler defined with <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad-handler%29%29" class="RktStxLink" data-pltdoc="x">pad-handler</a></span>:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">ComplexNumber -> ComplexNumber</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">i-sub1</span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._-%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktVal">0+1i</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">ComplexNumber -> ComplexNumber</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">i-add1</span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">+</a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktVal">0+1i</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">ComplexNumber -> ComplexNumber</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">deal with all </span><a href="#%28tech._world._padevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">PadEvent</span></a><span class="RktCmt">s</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">handler</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._pad-handler%29%29" class="RktStxLink" data-pltdoc="x">pad-handler</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._left%29%29" class="RktStxLink" data-pltdoc="x">left</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._sub1%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">sub1</a></span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._right%29%29" class="RktStxLink" data-pltdoc="x">right</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._add1%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">add1</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._up%29%29" class="RktStxLink" data-pltdoc="x">up</a></span><span class="hspace"> </span><span class="RktSym">i-sub1</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._down%29%29" class="RktStxLink" data-pltdoc="x">down</a></span><span class="hspace"> </span><span class="RktSym">i-add1</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._shift%29%29" class="RktStxLink" data-pltdoc="x">shift</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">w</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">0</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._space%29%29" class="RktStxLink" data-pltdoc="x">space</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-with%29%29" class="RktValLink" data-pltdoc="x">stop-with</a></span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">some tests:</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">handler</span><span class="hspace"> </span><span class="RktVal">9</span><span class="hspace"> </span><span class="RktVal">"left"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">8</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">handler</span><span class="hspace"> </span><span class="RktVal">8</span><span class="hspace"> </span><span class="RktVal">"up"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">8-1i</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p></li><li><p> A <a href="#%28tech._world._mouseevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">MouseEvent</span></a> represents mouse events, e.g., mouse movements
or mouse clicks, by the computer’s user.</p><p><a name="(tech._world._mouseevent)"></a><span style="font-style: italic">MouseEvent</span> : <span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._one-of%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">one-of/c</a></span><span class="stt"> </span><span class="RktVal">"button-down"</span><span class="stt"> </span><span class="RktVal">"button-up"</span><span class="stt"> </span><span class="RktVal">"drag"</span><span class="stt"> </span><span class="RktVal">"move"</span><span class="stt"> </span><span class="RktVal">"enter"</span><span class="stt"> </span><span class="RktVal">"leave"</span><span class="RktPn">)</span></p><p><div class="SIntrapara">All <a href="#%28tech._world._mouseevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">MouseEvent</span></a>s are represented via strings:
</div><div class="SIntrapara"><ul><li><p><span class="RktVal">"button-down"</span>
signals that the computer user has pushed a mouse button down;</p></li><li><p><span class="RktVal">"button-up"</span>
signals that the computer user has let go of a mouse button;</p></li><li><p><span class="RktVal">"drag"</span>
signals that the computer user is dragging the mouse. A dragging event
occurs when the mouse moves while a mouse button is pressed.</p></li><li><p><span class="RktVal">"move"</span>
signals that the computer user has moved the mouse;</p></li><li><p><span class="RktVal">"enter"</span>
signals that the computer user has moved the mouse into the canvas area; and</p></li><li><p><span class="RktVal">"leave"</span>
signals that the computer user has moved the mouse out of the canvas area.</p></li></ul></div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._world._((lib._2htdp/universe..rkt)._mouse-event~3f))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._mouse-event~3f%29%29" class="RktValDef RktValLink" data-pltdoc="x">mouse-event?</a></span></span><span class="hspace"> </span><span class="RktVar">x</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">x</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">any</a></span></td></tr></table></blockquote></div><div class="SIntrapara">determines whether <span class="RktVar">x</span> is a <a href="#%28tech._world._mouseevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">MouseEvent</span></a></div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._world._((lib._2htdp/universe..rkt)._mouse~3d~3f))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._mouse~3d~3f%29%29" class="RktValDef RktValLink" data-pltdoc="x">mouse=?</a></span></span><span class="hspace"> </span><span class="RktVar">x</span><span class="hspace"> </span><span class="RktVar">y</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">x</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._mouse-event~3f%29%29" class="RktValLink" data-pltdoc="x">mouse-event?</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">y</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._mouse-event~3f%29%29" class="RktValLink" data-pltdoc="x">mouse-event?</a></span></td></tr></table></blockquote></div><div class="SIntrapara">compares two <a href="#%28tech._world._mouseevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">MouseEvent</span></a>s for equality</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._on-mouse))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-mouse%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">on-mouse</a></span></span><span class="hspace"> </span><span class="RktVar">mouse-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">mouse-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a></td></tr><tr><td><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._integer%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">integer?</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._integer%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">integer?</a></span><span class="hspace"> </span><a href="#%28tech._world._mouseevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">MouseEvent</span></a></td></tr><tr><td><span class="hspace"> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a><span class="RktPn">)</span></td></tr></table></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to call <span class="RktVar">mouse-expr</span> on the current world, the current
<span class="RktSym">x</span> and <span class="RktSym">y</span> coordinates of the mouse, and a
<a href="#%28tech._world._mouseevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">MouseEvent</span></a> for every (noticeable) action of the mouse by the
computer user. The result of the call becomes the current world.</div></p><p>For <span class="RktVal">"leave"</span> and <span class="RktVal">"enter"</span> events, the coordinates of the
mouse click may be outside of the (implicit) rectangle. That is, the
coordinates may be negative or larger than the (implicitly) specified
width and height.</p><p><span style="font-weight: bold">Note 1</span>: the operating system doesn’t really notice every single movement
of the mouse (across the mouse pad). Instead it samples the movements and
signals most of them.</p><p><span style="font-weight: bold">Note 2</span>: while mouse events are usually reported in the expected
manner, the operating system doesn’t necessarily report them in the
expected order. For example, the Windows operating system insists on
signaling a <span class="RktVal">"move"</span> event immediately after a <span class="RktVal">"button-up"</span>
event is discovered. Programmers must design the <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-mouse%29%29" class="RktStxLink" data-pltdoc="x">on-mouse</a></span>
handler to handle any possible mouse event at any moment. </p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._stop-when))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">stop-when</a></span></span><span class="hspace"> </span><span class="RktVar">last-world?</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">last-world?</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to call the <span class="RktVar">last-world?</span> function whenever the canvas is
drawn. If this call produces <span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528lib._racket%252Fbool..rkt%2529._true%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">true</a></span>, the world program is shut
down. Specifically, the clock is stopped; no more
tick events, <a href="#%28tech._world._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a>s, or <a href="#%28tech._world._mouseevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">MouseEvent</span></a>s are forwarded to
the respective handlers. The <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression returns this
last world.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxLink" data-pltdoc="x">stop-when</a></span><span class="hspace"> </span><span class="RktVar">last-world?</span><span class="hspace"> </span><span class="RktVar">last-picture</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><blockquote class="SubFlow"><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">last-world?</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span><span class="RktPn">)</span></td></tr></table><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">last-picture</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><span class="RktSym">scene?</span><span class="RktPn">)</span></td></tr></table></blockquote></td></tr></table></blockquote></div><div class="SIntrapara"><span class="refelem"><span class="refcolumn"><span class="refcontent">See <a href="#%28part._scene%29" data-pltdoc="x">Background</a> for <span class="RktSym">scene?</span>.</span></span></span>
tells DrRacket to call the <span class="RktVar">last-world?</span> function whenever the canvas is
drawn. If this call produces <span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528lib._racket%252Fbool..rkt%2529._true%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">true</a></span>, the world program is shut
down after displaying the world one last time, this time using the image
rendered with <span class="RktVar">last-picture</span>. Specifically, the clock is stopped; no more
tick events, <a href="#%28tech._world._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a>s, or <a href="#%28tech._world._mouseevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">MouseEvent</span></a>s are forwarded to
the respective handlers. The <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression returns this
last world.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>struct</p></div></div><p class="RForeground"><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">struct</a></span><span class="stt"> </span><a name="(def._world._((lib._teachpack/2htdp/image..rkt)._stop-with-w))"></a><a name="(def._world._((lib._teachpack/2htdp/image..rkt)._stop-with~3f))"></a><a name="(def._world._((lib._teachpack/2htdp/image..rkt)._struct~3astop-with))"></a><a name="(def._world._((lib._2htdp/universe..rkt)._stop-with))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-with%29%29" class="RktValDef RktValLink" data-pltdoc="x">stop-with</a></span></span><span class="stt"> </span><span class="RktPn">(</span><span class="RktSym">w</span><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktSym">w</span><span class="hspace"> </span>:<span class="hspace"> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a></td></tr></table></blockquote></div><div class="SIntrapara">signals to
DrRacket that the world program should shut down. That is, any
handler may return <span class="RktPn">(</span><span class="RktSym"><a href="#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-with%29%29" class="RktValLink" data-pltdoc="x">stop-with</a></span><span class="stt"> </span><span class="RktSym">w</span><span class="RktPn">)</span> provided <span class="RktSym">w</span> is a
<a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a>. If it does, the state of the world becomes <span class="RktSym">w</span>
and <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> will close down all event handling.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._check-with))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._check-with%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">check-with</a></span></span><span class="hspace"> </span><span class="RktVar">world-expr?</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">world-expr?</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><span class="RktSym">Any</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to call the <span class="RktVar">world-expr?</span> function on the result of
every world handler call. If this call produces <span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528lib._racket%252Fbool..rkt%2529._true%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">true</a></span>, the result
is considered a world; otherwise the world program signals an error.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._record~3f))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._record~3f%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">record?</a></span></span><span class="hspace"> </span><span class="RktVar">r-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">r-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">any/c</a></span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to enable a visual replay of the interaction,
unless <span class="RktVal">#f</span>.
The replay action generates one png image per image and
an animated gif for the entire sequence in the directory of the user’s
choice. If <span class="RktVar">r-expr</span> evaluates to the name of an existing
directory/folder (in the local directory/folder), the directory is used to
deposit the images.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._state))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._state%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">state</a></span></span><span class="hspace"> </span><span class="RktVar">expr</span><span class="RktPn">)</span></p></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">if not <span class="RktVal">#f</span>, DrRacket opens a separate window in which the current
state is rendered each time it is updated. This is useful for beginners
who wish to see how their world evolves—<wbr></wbr>without having to design a
rendering function—<wbr></wbr>plus for the debugging of world programs.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._world._((lib._2htdp/universe..rkt)._name))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._name%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">name</a></span></span><span class="hspace"> </span><span class="RktVar">name-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">name-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._or%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">or/c</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=symbols.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._symbol%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">symbol?</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">string?</a></span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">provide a name (<span class="RktSym">namer-expr</span>) to this world, which is used as the
title of the canvas.</div></p></li></ul><p>The following example shows that <span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._run-simulation%29%29" class="RktValLink" data-pltdoc="x">run-simulation</a></span><span class="stt"> </span><span class="RktSym">create-UFO-scene</span><span class="RktPn">)</span> is
a short-hand for three lines of code:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">create-UFO-scene</span><span class="hspace"> </span><span class="RktSym">height</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._underlay%2Fxy%29%29" class="RktValLink" data-pltdoc="x">underlay/xy</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._rectangle%29%29" class="RktValLink" data-pltdoc="x">rectangle</a></span><span class="hspace"> </span><span class="RktVal">100</span><span class="hspace"> </span><span class="RktVal">100</span><span class="hspace"> </span><span class="RktVal">"solid"</span><span class="hspace"> </span><span class="RktVal">"white"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">50</span><span class="hspace"> </span><span class="RktSym">height</span><span class="hspace"> </span><span class="RktSym">UFO</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">UFO</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._underlay%2Falign%29%29" class="RktValLink" data-pltdoc="x">underlay/align</a></span><span class="hspace"> </span><span class="RktVal">"center"</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"center"</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._circle%29%29" class="RktValLink" data-pltdoc="x">circle</a></span><span class="hspace"> </span><span class="RktVal">10</span><span class="hspace"> </span><span class="RktVal">"solid"</span><span class="hspace"> </span><span class="RktVal">"green"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._rectangle%29%29" class="RktValLink" data-pltdoc="x">rectangle</a></span><span class="hspace"> </span><span class="RktVal">40</span><span class="hspace"> </span><span class="RktVal">4</span><span class="hspace"> </span><span class="RktVal">"solid"</span><span class="hspace"> </span><span class="RktVal">"green"</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace"> </span><span class="RktVal">0</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._add1%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">add1</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace"> </span><span class="RktSym">create-UFO-scene</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>Exercise: Add a condition for stopping the flight of the UFO when it
reaches the bottom.</p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""world-example"">2.4.4<tt> </tt><a name="(part._world-example)"></a>A First Sample World</h5><p>This section uses a simple example to explain the design of worlds. The
first subsection introduces the sample domain, a door that closes
automatically. The second subsection is about the design of <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>
programs in general, the remaining subsections implement a simulation of
the door.</p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""Understanding_a_Door"">2.4.4.1<tt> </tt><a name="(part._.Understanding_a_.Door)"></a>Understanding a Door</h5><p>Say we wish to design a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program that simulates the working of
a door with an automatic door closer. If this kind of door is locked, you
can unlock it with a key. While this doesn’t open the door per se, it is
now possible to do so. That is, an unlocked door is closed and pushing at
the door opens it. Once you have passed through the door and you let go,
the automatic door closer takes over and closes the door again. When a
door is closed, you can lock it again.</p><p>Here is a diagram that translates our words into a graphical
representation:</p><p><img src="door-real.png" alt="" width="130" height="196"/></p><p>Like the picture of the general workings of a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program, this
diagram displays a so-called “state machine.” The three circled words are
the states that our informal description of the door identified: locked,
closed (and unlocked), and open. The arrows specify how the door can go
from one state into another. For example, when the door is open, the
automatic door closer shuts the door as time passes. This transition is
indicated by the arrow labeled “time.” The other arrows represent
transitions in a similar manner:</p><ul><li><p>“push” means a person pushes the door open (and let’s go);</p></li><li><p>“lock” refers to the act of inserting a key into the lock and turning
it to the locked position; and</p></li><li><p>“unlock” is the opposite of “lock.”</p></li></ul><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""Hints_on_Designing_Worlds"">2.4.4.2<tt> </tt><a name="(part._.Hints_on_.Designing_.Worlds)"></a>Hints on Designing Worlds</h5><p>Simulating any dynamic behavior via a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program demands two
different activities. First, we must tease out those portions of our
domain that change over time or in reaction to actions, and we must
develop a data representation for this information. This is what we call
<a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a>. Keep in
mind that a good data definition makes it easy for readers to map data to
information in the real world and vice versa. For all others aspects of
the world, we use global constants, including graphical or visual
constants that are used in conjunction with the rendering operations.</p><p>Second, we must translate the actions in our domain—<wbr></wbr>the arrows in the
above diagram—<wbr></wbr>into interactions with the computer that the universe
teachpack can deal with. Once we have decided to use the passing of time
for one aspect, key presses for another, and mouse movements for a third,
we must develop functions that map the current state of the
world—<wbr></wbr>represented as data from <a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a>—<wbr></wbr>into the next state of the
world. Put differently, we have just created a wish list with three
handler functions that have the following general contract and purpose
statements:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">tick : WorldState -> HandlerResult</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">deal with the passing of time</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">tick</span><span class="hspace"> </span><span class="RktSym">w</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">click : WorldState </span><span style="font-style: italic">Number</span><span class="RktCmt"> </span><span style="font-style: italic">Number</span><span class="RktCmt"> </span><a href="world.html#%28tech._mouseevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">MouseEvent</span></a><span class="RktCmt"> -> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">deal with a mouse click at </span><span style="font-style: italic">(x,y)</span><span class="RktCmt"> of kind </span><span style="font-style: italic">me</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">in the current world </span><span style="font-style: italic">w</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">click</span><span class="hspace"> </span><span class="RktSym">w</span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktSym">y</span><span class="hspace"> </span><span class="RktSym">me</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">control : WorldState </span><a href="world.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a><span class="RktCmt"> -> </span><a href="#%28tech._world._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">deal with a key event </span><span style="font-style: italic">ke</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">in the current world </span><span style="font-style: italic">w</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">control</span><span class="hspace"> </span><span class="RktSym">w</span><span class="hspace"> </span><span class="RktSym">ke</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr></table></blockquote><p>That is, the contracts of the various handler designations dictate what the
contracts of our functions are, once we have defined how to represent the
domain with data in our chosen language.</p><p>A typical program does not use all three of these functions. Furthermore,
the design of these functions provides only the top-level, initial design
goal. It often demands the design of many auxiliary functions. The
collection of all these functions is your <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program.</p><blockquote class="SCentered"><p>An extended example is available in
<a href="http://www.ccs.neu.edu/home/matthias/HtDP2e/">How to Design Programs/2e</a>.</p></blockquote><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""world2"">2.4.5<tt> </tt><a name="(part._universe._world2)"></a>The World is not Enough</h5><p>The library facilities covered so far are about designing individual
programs with interactive graphical user interfaces (simulations,
animations, games, etc.). In this section, we introduce capabilities for
designing a distributed program, which is really a number of programs that
coordinate their actions in some fashion. Each of the individual programs
may run on any computer in the world (as in our planet and the spacecrafts
that we sent out), as long as it is on the internet and as long as the
computer allows the program to send and receive messages (via TCP). We
call this arrangement a <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a> and the program that coordinates
it all a <span style="font-style: italic">universe server</span> or just <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a>.</p><p>This section explains what messages are, how to send them from a
<a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program, how to receive them, and how to connect a
<a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program to a <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a>.</p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""Messages"">2.4.5.1<tt> </tt><a name="(part._universe._.Messages)"></a>Messages</h5><p>After a world program has become a part of a universe, it may send messages
and receive them. In terms of data, a message is just an
<a href="#%28tech._universe._s._expression%29" class="techoutside" data-pltdoc="x"><span class="techinside">S-expression</span></a>.</p><p><a name="(tech._universe._s._expression)"></a><span style="font-style: italic">S-expression</span> An S-expression is roughly a nested list of basic
data; to be precise, an S-expression is one of:</p><p><div class="SIntrapara"><ul><li><p>a string,</p></li><li><p>a symbol,</p></li><li><p>a number,</p></li><li><p>a boolean,</p></li><li><p>a char, or</p></li><li><p>a list of S-expressions, or</p></li><li><p>a prefab struct of S-expressions.</p></li></ul></div><div class="SIntrapara">Note the <span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._list%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">list</a></span> clause includes <span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._empty%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">empty</a></span> of course.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._universe._((lib._2htdp/universe..rkt)._sexp~3f))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._sexp~3f%29%29" class="RktValDef RktValLink" data-pltdoc="x">sexp?</a></span></span><span class="hspace"> </span><span class="RktVar">x</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">x</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">any/c</a></span></td></tr></table></blockquote></div><div class="SIntrapara">determines whether <span class="RktVar">x</span> is an <a href="#%28tech._universe._s._expression%29" class="techoutside" data-pltdoc="x"><span class="techinside">S-expression</span></a>.</div></p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""Sending_Messages"">2.4.5.2<tt> </tt><a name="(part._universe._.Sending_.Messages)"></a>Sending Messages</h5><p>Each world-producing callback in a world program—<wbr></wbr>those for handling clock
tick events, keyboard events, and mouse events—<wbr></wbr>may produce a
<a href="#%28tech._universe._package%29" class="techoutside" data-pltdoc="x"><span class="techinside">Package</span></a> in addition to just a <a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a>:</p><p><div class="SIntrapara"><a name="(tech._universe._handlerresult)"></a><span style="font-style: italic">HandlerResult</span> is one of the following:
</div><div class="SIntrapara"><ul><li><p><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a></p></li><li><p><a href="#%28tech._universe._package%29" class="techoutside" data-pltdoc="x"><span class="techinside">Package</span></a></p></li></ul></div><div class="SIntrapara">where <a name="(tech._universe._package)"></a><span style="font-style: italic">Package</span> represents a pair consisting of a <a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a>
and a message from a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program to the <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a>. Because
programs send messages via <a href="#%28tech._universe._package%29" class="techoutside" data-pltdoc="x"><span class="techinside">Package</span></a>, the teachpack does not
provide the selectors for the structure, only the constructor and a
predicate.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._universe._((lib._2htdp/universe..rkt)._package~3f))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._package~3f%29%29" class="RktValDef RktValLink" data-pltdoc="x">package?</a></span></span><span class="hspace"> </span><span class="RktVar">x</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">x</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">any/c</a></span></td></tr></table></blockquote></div><div class="SIntrapara">determine whether <span class="RktVar">x</span> is a <a href="#%28tech._universe._package%29" class="techoutside" data-pltdoc="x"><span class="techinside">Package</span></a>.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._universe._((lib._2htdp/universe..rkt)._make-package))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._make-package%29%29" class="RktValDef RktValLink" data-pltdoc="x">make-package</a></span></span><span class="hspace"> </span><span class="RktVar">w</span><span class="hspace"> </span><span class="RktVar">m</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._package~3f%29%29" class="RktValLink" data-pltdoc="x">package?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">w</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">any/c</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">m</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._sexp~3f%29%29" class="RktValLink" data-pltdoc="x">sexp?</a></span></td></tr></table></blockquote></div><div class="SIntrapara">create a <a href="#%28tech._universe._package%29" class="techoutside" data-pltdoc="x"><span class="techinside">Package</span></a> from a <a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a> and an <a href="#%28tech._universe._s._expression%29" class="techoutside" data-pltdoc="x"><span class="techinside">S-expression</span></a>.</div></p><p>Recall that event handlers return a <a href="#%28tech._universe._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a>,
and we have just refined this data definition. Hence, each handler may return either a
<a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a> or a <a href="#%28tech._universe._package%29" class="techoutside" data-pltdoc="x"><span class="techinside">Package</span></a>. If an event handler produces a <a href="#%28tech._universe._package%29" class="techoutside" data-pltdoc="x"><span class="techinside">Package</span></a>,
the content of the world field becomes the next world and the message field specifies
what the world sends to the universe. This distinction also explains why the data
definition for <a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a> may not include a <a href="#%28tech._universe._package%29" class="techoutside" data-pltdoc="x"><span class="techinside">Package</span></a>.</p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""Connecting_with_the_Universe"">2.4.5.3<tt> </tt><a name="(part._universe._.Connecting_with_the_.Universe)"></a>Connecting with the Universe</h5><p>Messages are sent to the universe program, which runs on some computer in
the world. The next section is about constructs for creating such a universe
server. For now, we just need to know that it exists and that it is the recipient
of messages.</p><p><a name="(tech._universe._ip)"></a><span style="font-style: italic">IP</span> <span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">string?</a></span></p><p>Before a world program can send messages, it must register with the
server. Registration must specify the internet address of the computer on which
the server runs, also known as an <a href="#%28tech._universe._ip%29" class="techoutside" data-pltdoc="x"><span class="techinside">IP</span></a> address or a host. Here a
<a href="#%28tech._universe._ip%29" class="techoutside" data-pltdoc="x"><span class="techinside">IP</span></a> address is a string of the right shape, e.g., <span class="RktVal">"192.168.1.1"</span>
or <span class="RktVal">"www.google.com"</span>.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>value</p></div></div><p class="RForeground"><a name="(def._universe._((lib._2htdp/universe..rkt)._.L.O.C.A.L.H.O.S.T))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._.L.O.C.A.L.H.O.S.T%29%29" class="RktValDef RktValLink" data-pltdoc="x">LOCALHOST</a></span></span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">string?</a></span></p></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">the <a href="#%28tech._universe._ip%29" class="techoutside" data-pltdoc="x"><span class="techinside">IP</span></a> of your computer. Use it while you
are developing a distributed program, especially while you are
investigating whether the participating world programs collaborate in an
appropriate manner. This is called <span style="font-style: italic">integration testing</span> and differs
from unit testing quite a bit.</div></p><p>A <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> description of a world program that wishes to communicate
with other programs must contain a <span class="RktSym"><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._register%29%29" class="RktStxLink" data-pltdoc="x">register</a></span> clause of one of the
following shapes:</p><ul><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._universe._((lib._2htdp/universe..rkt)._register))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._register%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">register</a></span></span><span class="hspace"> </span><span class="RktVar">ip-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">ip-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">string?</a></span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">connect this world to a universe server at the specified <span class="RktVar">ip-expr</span>
address and set up capabilities for sending and receiving messages.
If the world description includes a name specification of the form
<span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._name%29%29" class="RktStxLink" data-pltdoc="x">name</a></span><span class="stt"> </span><span class="RktSym">SomeString</span><span class="RktPn">)</span> or <span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._name%29%29" class="RktStxLink" data-pltdoc="x">name</a></span><span class="stt"> </span><span class="RktSym">SomeSymbol</span><span class="RktPn">)</span>, the name of the
world is sent along to the server.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._universe._((lib._2htdp/universe..rkt)._port))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._port%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">port</a></span></span><span class="hspace"> </span><span class="RktVar">port-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">port-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._natural-number%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">natural-number/c</a></span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">specifies port on which a world wishes to receive and send messages. A
port number is an integer between <span class="RktVal">0</span> and <span class="RktVal">65536</span>.</div></p></li></ul><p>When a world program registers with a universe program and the universe program
stops working, the world program stops working, too.</p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""Receiving_Messages"">2.4.5.4<tt> </tt><a name="(part._universe._.Receiving_.Messages)"></a>Receiving Messages</h5><p>Finally, the receipt of a message from the server is an event, just like
tick events, keyboard events, and mouse events. Dealing with the receipt of a
message works exactly like dealing with any other event. DrRacket
applies the event handler that the world program specifies; if there is no
clause, the message is discarded.</p><p>The <span class="RktSym"><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._on-receive%29%29" class="RktStxLink" data-pltdoc="x">on-receive</a></span> clause of a <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> specifies the event handler
for message receipts.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._universe._((lib._2htdp/universe..rkt)._on-receive))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._on-receive%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">on-receive</a></span></span><span class="hspace"> </span><span class="RktVar">receive-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">receive-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="hspace"> </span><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._sexp~3f%29%29" class="RktValLink" data-pltdoc="x">sexp?</a></span><span class="hspace"> </span><a href="#%28tech._universe._handlerresult%29" class="techoutside" data-pltdoc="x"><span class="techinside">HandlerResult</span></a><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to call <span class="RktVar">receive-expr</span> for every message receipt, on the current
<a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a> and the received message. The result of the call becomes the current
<a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a>.</div></p><p>Because <span class="RktVar">receive-expr</span> is (or evaluates to) a world-transforming
function, it too can produce a <a href="#%28tech._universe._package%29" class="techoutside" data-pltdoc="x"><span class="techinside">Package</span></a> instead of just a
<a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a>. If the result is a <a href="#%28tech._universe._package%29" class="techoutside" data-pltdoc="x"><span class="techinside">Package</span></a>, its message content is
sent to the <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a>.</p><p>The diagram below summarizes the extensions of this section in graphical form.</p><p><img src="universe.png" alt="" width="542" height="397"/></p><p>A registered world program may send a message to the universe server
at any time by returning a <a href="#%28tech._universe._package%29" class="techoutside" data-pltdoc="x"><span class="techinside">Package</span></a> from an event handler. The
message is transmitted to the server, which may forward it to some
other world program as given or in some massaged form. The arrival of a
message is just another event that a world program must deal with. Like
all other event handlers <span style="font-style: italic">receive</span> accepts a <a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a> and some
auxiliary arguments (a message in this case) and produces a
<a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a> or a <a href="#%28tech._universe._package%29" class="techoutside" data-pltdoc="x"><span class="techinside">Package</span></a>.</p><p>When messages are sent from any of the worlds to the universe or vice versa,
there is no need for the sender and receiver to synchronize. Indeed, a sender
may dispatch as many messages as needed without regard to whether the
receiver has processed them yet. The messages simply wait in queue until
the receiving <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a> or <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program takes care of them.</p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""universe-server"">2.4.6<tt> </tt><a name="(part._universe-server)"></a>The Universe Server</h5><p>A <a name="(tech._server)"></a><span style="font-style: italic">server</span> is the central control program of a <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a> and
deals with receiving and sending of messages between the world
programs that participate in the <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a>. Like a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>
program, a server is a program that reacts to events, though to different
events than <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>s. The two primary kinds of events are the
appearance of a new <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program in the <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a>
and the receipt of a message from a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program.</p><p>The teachpack provides a mechanism for designating event handlers for
servers that is quite similar to the mechanism for describing <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>
programs. Depending on the designated event handlers, the server takes on
distinct roles:</p><ul><li><p>A server may be a “pass through” channel between two worlds, in which case
it has no other function than to communicate whatever message it receives
from one world to the other, without any interference.</p></li><li><p>A server may enforce a “back and forth” protocol, i.e., it may force two
(or more) worlds to engage in a civilized tit-for-tat exchange. Each
world is given a chance to send a message and must then wait
to get a reply before it sends anything again.</p></li><li><p>A server may play the role of a special-purpose arbiter, e.g., the referee
or administrator of a game. It may check that each world “plays” by the rules,
and it administrates the resources of the game.</p></li></ul><p>As a matter of fact, a pass-through <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a> can become basically
invisible, making it appear as if all communication goes from peer
<a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> to peer in a <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a>.</p><p>This section first introduces some basic forms of data that the
<a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a> uses to represent <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>s and other matters. Second,
it explains how to describe a server program.</p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""Worlds_and_Messages"">2.4.6.1<tt> </tt><a name="(part._.Worlds_and_.Messages)"></a>Worlds and Messages</h5><p>Understanding the server’s event handling functions demands several data
representations: that of (a connection to) a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program and that
of a response of a handler to an event.</p><ul><li><p>The <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a> and its event handlers must agree on a
data representation of the <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>s that participate in the
universe.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((lib._2htdp/universe..rkt)._iworld~3f))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3f%29%29" class="RktValDef RktValLink" data-pltdoc="x">iworld?</a></span></span><span class="hspace"> </span><span class="RktVar">x</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">x</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">any/c</a></span></td></tr></table></blockquote></div><div class="SIntrapara">determines whether <span class="RktVar">x</span> is an <span style="font-style: italic">iworld</span>. Because the universe server
represents worlds via structures that collect essential information about
the connections, the teachpack does not export any constructor or selector
functions on worlds.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((lib._2htdp/universe..rkt)._iworld~3d~3f))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3d~3f%29%29" class="RktValDef RktValLink" data-pltdoc="x">iworld=?</a></span></span><span class="hspace"> </span><span class="RktVar">u</span><span class="hspace"> </span><span class="RktVar">v</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">u</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3f%29%29" class="RktValLink" data-pltdoc="x">iworld?</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">v</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3f%29%29" class="RktValLink" data-pltdoc="x">iworld?</a></span></td></tr></table></blockquote></div><div class="SIntrapara">compares two <span style="font-style: italic">iworld</span>s for equality.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((lib._2htdp/universe..rkt)._iworld-name))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld-name%29%29" class="RktValDef RktValLink" data-pltdoc="x">iworld-name</a></span></span><span class="hspace"> </span><span class="RktVar">w</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">string?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">w</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3f%29%29" class="RktValLink" data-pltdoc="x">iworld?</a></span></td></tr></table></blockquote></div><div class="SIntrapara">extracts the name from a <span style="font-style: italic">iworld</span> structure.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>value</p></div></div><p class="RForeground"><a name="(def._((lib._2htdp/universe..rkt)._iworld1))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld1%29%29" class="RktValDef RktValLink" data-pltdoc="x">iworld1</a></span></span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3f%29%29" class="RktValLink" data-pltdoc="x">iworld?</a></span></p></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">an <span style="font-style: italic">iworld</span> for testing your programs
</div><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>value</p></div></div><p class="RForeground"><a name="(def._((lib._2htdp/universe..rkt)._iworld2))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld2%29%29" class="RktValDef RktValLink" data-pltdoc="x">iworld2</a></span></span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3f%29%29" class="RktValLink" data-pltdoc="x">iworld?</a></span></p></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">another iworld for testing your programs
</div><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>value</p></div></div><p class="RForeground"><a name="(def._((lib._2htdp/universe..rkt)._iworld3))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld3%29%29" class="RktValDef RktValLink" data-pltdoc="x">iworld3</a></span></span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3f%29%29" class="RktValLink" data-pltdoc="x">iworld?</a></span></p></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">and a third one</div></p><p>The three sample iworlds are provided so that you can test your functions
for universe programs. For example:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3d~3f%29%29" class="RktValLink" data-pltdoc="x">iworld=?</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld1%29%29" class="RktValLink" data-pltdoc="x">iworld1</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld2%29%29" class="RktValLink" data-pltdoc="x">iworld2</a></span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528lib._racket%252Fbool..rkt%2529._false%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">false</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3d~3f%29%29" class="RktValLink" data-pltdoc="x">iworld=?</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld2%29%29" class="RktValLink" data-pltdoc="x">iworld2</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld2%29%29" class="RktValLink" data-pltdoc="x">iworld2</a></span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528lib._racket%252Fbool..rkt%2529._true%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">true</a></span><span class="RktPn">)</span></td></tr></table></blockquote></li><li><p>Each event handler produces a <span style="font-style: italic">bundle</span>, which is a structure
that contains the <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a>’s state, a list of mails to other worlds,
and the list of <span style="font-style: italic">iworld</span>s that are to be disconnected.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((lib._2htdp/universe..rkt)._bundle~3f))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._bundle~3f%29%29" class="RktValDef RktValLink" data-pltdoc="x">bundle?</a></span></span><span class="hspace"> </span><span class="RktVar">x</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">x</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">any/c</a></span></td></tr></table></blockquote></div><div class="SIntrapara">determines whether <span class="RktVar">x</span> is a <span style="font-style: italic">bundle</span>.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((lib._2htdp/universe..rkt)._make-bundle))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._make-bundle%29%29" class="RktValDef RktValLink" data-pltdoc="x">make-bundle</a></span></span><span class="hspace"> </span><span class="RktVar">state</span><span class="hspace"> </span><span class="RktVar">mails</span><span class="hspace"> </span><span class="RktVar">low</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._bundle~3f%29%29" class="RktValLink" data-pltdoc="x">bundle?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">state</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">any/c</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">mails</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._listof%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">listof</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._mail~3f%29%29" class="RktValLink" data-pltdoc="x">mail?</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">low</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._listof%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">listof</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3f%29%29" class="RktValLink" data-pltdoc="x">iworld?</a></span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">creates a <span style="font-style: italic">bundle</span> from a piece of data that
represents a server state, a list of mails, and a list of iworlds.</div></p><p>If disconnecting from these worlds results in an empty list of
participants, the universe server is restarted in the initial state.</p><p>A <span style="font-style: italic">mail</span> represents a message from an event handler to a world. The
teachpack provides only a predicate and a constructor for these structures:</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((lib._2htdp/universe..rkt)._mail~3f))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._mail~3f%29%29" class="RktValDef RktValLink" data-pltdoc="x">mail?</a></span></span><span class="hspace"> </span><span class="RktVar">x</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">x</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">any/c</a></span></td></tr></table></blockquote></div><div class="SIntrapara">determines whether <span class="RktVar">x</span> is a <span style="font-style: italic">mail</span>.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((lib._2htdp/universe..rkt)._make-mail))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._make-mail%29%29" class="RktValDef RktValLink" data-pltdoc="x">make-mail</a></span></span><span class="hspace"> </span><span class="RktVar">to</span><span class="hspace"> </span><span class="RktVar">content</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._mail~3f%29%29" class="RktValLink" data-pltdoc="x">mail?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">to</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3f%29%29" class="RktValLink" data-pltdoc="x">iworld?</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">content</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._sexp~3f%29%29" class="RktValLink" data-pltdoc="x">sexp?</a></span></td></tr></table></blockquote></div><div class="SIntrapara">creates a <span style="font-style: italic">mail</span> from a <span style="font-style: italic">iworld</span> and an <a href="#%28tech._universe._s._expression%29" class="techoutside" data-pltdoc="x"><span class="techinside">S-expression</span></a>.</div></p></li></ul><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""Universe_Descriptions"">2.4.6.2<tt> </tt><a name="(part._.Universe_.Descriptions)"></a>Universe Descriptions</h5><p>A <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a> keeps track of information about the <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a> that
it manages. One kind of tracked information is obviously the collection of
participating world programs, but in general the kind of information that
a server tracks and how the information is represented depends on the
situation and the programmer, just as with <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> programs.</p><p><a name="(tech._universestate)"></a><span style="font-style: italic">UniverseState</span> <span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">any/c</a></span> represents the server’s state. For running
<a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a>s, the teachpack demands that you come up with a data
definition for (your state of the) <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a>. Any piece of data can
represent the state. We just assume that you introduce a data definition
for the possible states and that your event handlers are designed
according to the design recipe for this data definition.</p><p>The <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a> itself is created with a description that includes the
first state and a number of clauses that specify functions for dealing
with <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a> events.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._((lib._2htdp/universe..rkt)._universe))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._universe%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">universe</a></span></span><span class="hspace"> </span><span class="RktVar">state-expr</span><span class="hspace"> </span><span class="RktVar">clause</span><span class="hspace"> </span><span class="RktMeta">...</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="specgrammar"><tr><td align="right" valign="baseline"><span class="RktVar">clause</span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">=</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._on-new%29%29" class="RktStxLink" data-pltdoc="x">on-new</a></span><span class="hspace"> </span><span class="RktVar">new-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._on-msg%29%29" class="RktStxLink" data-pltdoc="x">on-msg</a></span><span class="hspace"> </span><span class="RktVar">msg-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace"> </span><span class="RktVar">tick-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace"> </span><span class="RktVar">tick-expr</span><span class="hspace"> </span><span class="RktVar">rate-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace"> </span><span class="RktVar">tick-expr</span><span class="hspace"> </span><span class="RktVar">rate-expr</span><span class="hspace"> </span><span class="RktVar">limit-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._on-disconnect%29%29" class="RktStxLink" data-pltdoc="x">on-disconnect</a></span><span class="hspace"> </span><span class="RktVar">dis-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._state%29%29" class="RktStxLink" data-pltdoc="x">state</a></span><span class="hspace"> </span><span class="RktVar">expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._to-string%29%29" class="RktStxLink" data-pltdoc="x">to-string</a></span><span class="hspace"> </span><span class="RktVar">render-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._port%29%29" class="RktStxLink" data-pltdoc="x">port</a></span><span class="hspace"> </span><span class="RktVar">port-expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt"> </span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._check-with%29%29" class="RktStxLink" data-pltdoc="x">check-with</a></span><span class="hspace"> </span><span class="RktVar">universe?-expr</span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">creates a server with a given state, <span class="RktVar">state-expr</span>. The
behavior is specified via handler functions through mandatory and optional
<span style="font-style: italic">clause</span>s. These functions govern how the server deals with the
registration of new worlds, how it disconnects worlds, how it sends
messages from one world to the rest of the registered worlds, and how it
renders its current state as a string.</div></p><p>Evaluating a <span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._universe%29%29" class="RktStxLink" data-pltdoc="x">universe</a></span> expression starts a server. Visually it opens
a console window on which you can see that worlds join, which messages are
received from which world, and which messages are sent to which world. For
convenience, the console also has two buttons: one for shutting down a
universe and another one for re-starting it. The latter functionality is
especially useful during the integration of the various pieces of a
distributed program.</p><p>The mandatory clauses of a <span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._universe%29%29" class="RktStxLink" data-pltdoc="x">universe</a></span> server description are
<span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._on-new%29%29" class="RktStxLink" data-pltdoc="x">on-new</a></span> and <span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._on-msg%29%29" class="RktStxLink" data-pltdoc="x">on-msg</a></span>:</p><p><div class="SIntrapara"><ul><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._((lib._2htdp/universe..rkt)._on-new))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._on-new%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">on-new</a></span></span><span class="hspace"> </span><span class="RktVar">new-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">new-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._universestate%29" class="techoutside" data-pltdoc="x"><span class="techinside">UniverseState</span></a><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3f%29%29" class="RktValLink" data-pltdoc="x">iworld?</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._bundle~3f%29%29" class="RktValLink" data-pltdoc="x">bundle?</a></span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to call the function <span class="RktVar">new-expr</span> every time another world joins the
universe. The event handler is called with the current state and the
joining iworld, which isn’t on the list yet. In particular, the handler may
reject a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program from participating in a <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a>,
by simply including it in the resulting <span class="RktSym">bundle</span> structure (third field).</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._((lib._2htdp/universe..rkt)._on-msg))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._on-msg%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">on-msg</a></span></span><span class="hspace"> </span><span class="RktVar">msg-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">msg-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._universestate%29" class="techoutside" data-pltdoc="x"><span class="techinside">UniverseState</span></a><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3f%29%29" class="RktValLink" data-pltdoc="x">iworld?</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._sexp~3f%29%29" class="RktValLink" data-pltdoc="x">sexp?</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._bundle~3f%29%29" class="RktValLink" data-pltdoc="x">bundle?</a></span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to apply <span class="RktVar">msg-expr</span> to the current state of the
universe, the world
<span class="RktSym">w</span> that sent the message, and the message itself.</div></p></li></ul></div><div class="SIntrapara">All proper event handlers produce a <span style="font-style: italic">bundle</span>. The state in the
bundle is safe-guarded by the server until the next event, and the mails
are broadcast as specified. The list of iworlds in the third field of the
bundle is removed from the list of participants from which to expect
messages.</div></p><p>The following picture provides a graphical overview of the server’s workings.</p><p><img src="server.png" alt="" width="571" height="397"/></p><p>In addition to the mandatory handlers, a program may wish to add some
optional handlers:</p><ul><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace"> </span><span class="RktVar">tick-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">tick-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._universestate%29" class="techoutside" data-pltdoc="x"><span class="techinside">UniverseState</span></a><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._bundle~3f%29%29" class="RktValLink" data-pltdoc="x">bundle?</a></span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to apply <span class="RktVar">tick-expr</span> to the current state of the universe.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace"> </span><span class="RktVar">tick-expr</span><span class="hspace"> </span><span class="RktVar">rate-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><blockquote class="SubFlow"><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">tick-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._universestate%29" class="techoutside" data-pltdoc="x"><span class="techinside">UniverseState</span></a><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._bundle~3f%29%29" class="RktValLink" data-pltdoc="x">bundle?</a></span><span class="RktPn">)</span></td></tr></table><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">rate-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._and%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">and/c</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._real%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">real?</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._positive%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">positive?</a></span><span class="RktPn">)</span></td></tr></table></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to apply <span class="RktVar">tick-expr</span> as above; the clock ticks
every <span class="RktVar">rate-expr</span> seconds.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace"> </span><span class="RktVar">tick-expr</span><span class="hspace"> </span><span class="RktVar">rate-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><blockquote class="SubFlow"><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">tick-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._universestate%29" class="techoutside" data-pltdoc="x"><span class="techinside">UniverseState</span></a><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._bundle~3f%29%29" class="RktValLink" data-pltdoc="x">bundle?</a></span><span class="RktPn">)</span></td></tr></table><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">rate-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._and%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">and/c</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._real%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">real?</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._positive%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">positive?</a></span><span class="RktPn">)</span></td></tr></table><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktSym">limit-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._and%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">and/c</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._integer%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">integer?</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._positive%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">positive?</a></span><span class="RktPn">)</span></td></tr></table></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to apply <span class="RktVar">tick-expr</span> as above; the clock ticks
every <span class="RktVar">rate-expr</span> seconds. The universe stops when the clock has
ticked more than <span class="RktSym">limit-expr</span> times.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._((lib._2htdp/universe..rkt)._on-disconnect))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._on-disconnect%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">on-disconnect</a></span></span><span class="hspace"> </span><span class="RktVar">dis-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">dis-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._universestate%29" class="techoutside" data-pltdoc="x"><span class="techinside">UniverseState</span></a><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._iworld~3f%29%29" class="RktValLink" data-pltdoc="x">iworld?</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._bundle~3f%29%29" class="RktValLink" data-pltdoc="x">bundle?</a></span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to invoke <span class="RktVar">dis-expr</span> every time a participating
<a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> drops its connection to the server. The first argument
is the current state of the universe server, while the second argument is
the (representation of the) world that got disconnected. The resulting
bundle usually includes this second argument in the third field, telling
DrRacket not to wait for messages from this world anymore.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._port%29%29" class="RktStxLink" data-pltdoc="x">port</a></span><span class="hspace"> </span><span class="RktVar">port-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">port-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._natural-number%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">natural-number/c</a></span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">specifies port on which a universe wishes to receive and send messages. A
port number is an integer between <span class="RktVal">0</span> and <span class="RktVal">65536</span>.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._((lib._2htdp/universe..rkt)._to-string))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._to-string%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">to-string</a></span></span><span class="hspace"> </span><span class="RktVar">render-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">render-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><a href="#%28tech._universestate%29" class="techoutside" data-pltdoc="x"><span class="techinside">UniverseState</span></a><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">string?</a></span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to render the state of the universe after each event and to
display this string in the universe console.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._check-with%29%29" class="RktStxLink" data-pltdoc="x">check-with</a></span><span class="hspace"> </span><span class="RktVar">universe?-expr</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt"> </span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="argcontract"><tr><td><span class="hspace"> </span></td><td><span class="RktVar">universe?-expr</span></td><td><span class="hspace"> </span></td><td>:</td><td><span class="hspace"> </span></td><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><span class="RktSym">Any</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">boolean?</a></span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">ensure that what the event handlers produce is really an element of
<a href="#%28tech._universestate%29" class="techoutside" data-pltdoc="x"><span class="techinside">UniverseState</span></a>.</div></p></li><li><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed" style="display: inline-table; vertical-align: text-top;"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._state%29%29" class="RktStxLink" data-pltdoc="x">state</a></span><span class="hspace"> </span><span class="RktVar">expr</span><span class="RktPn">)</span></p></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">tells DrRacket to display a separate window in which the current
state is rendered each time it is updated. This is mostly useful for
debugging server programs.</div></p></li></ul><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""Exploring_a_Universe"">2.4.6.3<tt> </tt><a name="(part._.Exploring_a_.Universe)"></a>Exploring a Universe</h5><p>In order to explore the workings of a universe, it is necessary to launch a
server and several world programs on one and the same computer. We
recommend launching one server out of one DrRacket tab and as many worlds
as necessary out of a second tab. For the latter, the teachpack provides a
special form.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._((lib._2htdp/universe..rkt)._launch-many-worlds))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._launch-many-worlds%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">launch-many-worlds</a></span></span><span class="hspace"> </span><span class="RktVar">expression</span><span class="hspace"> </span><span class="RktMeta">...</span><span class="RktPn">)</span></p></blockquote></td></tr></table></blockquote></div><div class="SIntrapara">evaluates all sub-expressions in parallel. Typically each sub-expression
is an application of a function that evaluates a <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>
expression. When all worlds have stopped, the expression returns all final
worlds in order.</div></p><p><div class="SIntrapara">Once you have designed a world program, add a function definition
concerning <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> to the end of the tab:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">String -> World</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">main</span><span class="hspace"> </span><span class="RktSym">n</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._name%29%29" class="RktStxLink" data-pltdoc="x">name</a></span><span class="hspace"> </span><span class="RktSym">n</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Then in DrRacket’s Interactions area, use <span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._launch-many-worlds%29%29" class="RktStxLink" data-pltdoc="x">launch-many-worlds</a></span>
to create several distinctively named worlds:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%7E3e%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">></a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._launch-many-worlds%29%29" class="RktStxLink" data-pltdoc="x">launch-many-worlds</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">main</span><span class="hspace"> </span><span class="RktVal">"matthew"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">main</span><span class="hspace"> </span><span class="RktVal">"kathi"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">main</span><span class="hspace"> </span><span class="RktVal">"h3"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktVal">10</span></td></tr><tr><td><span class="RktVal">25</span></td></tr><tr><td><span class="RktVal">33</span></td></tr></table></blockquote></div><div class="SIntrapara">The three worlds can then interact via a server. When all of them have
stopped, they produce the final states, here <span class="RktVal">10</span>, <span class="RktVal">25</span>, and
<span class="RktVal">33</span>.</div></p><p>For advanced programmers, the library also provides a programmatic
interface for launching many worlds in parallel.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><table cellspacing="0" cellpadding="0" class="prototype RForeground"><tr><td><span class="RktPn">(</span><a name="(def._((lib._2htdp/universe..rkt)._launch-many-worlds/proc))"></a><span title="Provided from: 2htdp/universe | Package: htdp-lib"><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._launch-many-worlds%2Fproc%29%29" class="RktValDef RktValLink" data-pltdoc="x">launch-many-worlds/proc</a></span></span></td><td><span class="hspace"> </span></td><td><span class="RktVar">thunk-that-runs-a-world</span></td><td><span class="hspace"> </span></td><td><span class="hspace"> </span></td><td><span class="hspace"> </span></td><td><span class="hspace"> </span></td></tr><tr><td><span class="hspace"> </span></td><td><span class="hspace"> </span></td><td><span class="RktMeta">...</span><span class="RktPn">)</span></td><td><span class="hspace"> </span></td><td>→</td><td><span class="hspace"> </span></td><td><table cellspacing="0" cellpadding="0"><tr><td><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">any</a></span></td><td><span class="hspace"> </span></td><td><span class="stt">...</span></td></tr></table></td></tr></table></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">thunk-that-runs-a-world</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=function-contracts.html%23%2528form._%2528%2528lib._racket%252Fcontract%252Fbase..rkt%2529._-%7E3e%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x"><span class="nobreak">-></span></a></span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=data-structure-contracts.html%23%2528def._%2528%2528lib._racket%252Fcontract%252Fprivate%252Fmisc..rkt%2529._any%252Fc%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">any/c</a></span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">invokes all given <span class="RktVar">thunk-that-runs-a-world</span> in parallel. Typically
each argument is a function of no argument that evaluates a <span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>
expression. When all worlds have stopped, the function expression returns
all final worlds in order.</div></p><p><div class="SIntrapara">It is thus possible to decide at run time how many and which worlds to run
in parallel:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%7E3e%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">></a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=procedures.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._apply%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">apply</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._launch-many-worlds%2Fproc%29%29" class="RktValLink" data-pltdoc="x">launch-many-worlds/proc</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Flist..rkt%2529._build-list%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">build-list</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._random%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">random</a></span><span class="hspace"> </span><span class="RktVal">10</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">i</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">main</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._number-%7E3estring%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">number->string</a></span><span class="hspace"> </span><span class="RktSym">i</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktVal">0</span></td></tr><tr><td><span class="RktVal">9</span></td></tr><tr><td><span class="RktVal">1</span></td></tr><tr><td><span class="RktVal">2</span></td></tr><tr><td><span class="RktVal">3</span></td></tr><tr><td><span class="RktVal">6</span></td></tr><tr><td><span class="RktVal">5</span></td></tr><tr><td><span class="RktVal">4</span></td></tr><tr><td><span class="RktVal">8</span></td></tr><tr><td><span class="RktVal">7</span></td></tr></table></blockquote></div></p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""universe-sample"">2.4.7<tt> </tt><a name="(part._universe-sample)"></a>A First Sample Universe</h5><p>This section uses a simple example to explain the design of a universe,
especially its server and some participating worlds. The first subsection
explains the example, the second introduces the general design plan for
such universes. The remaining sections present the full-fledged solution.</p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""Two_Ball_Tossing_Worlds"">2.4.7.1<tt> </tt><a name="(part._.Two_.Ball_.Tossing_.Worlds)"></a>Two Ball Tossing Worlds</h5><p>Say we want to represent a universe that consists of a number of worlds and
that gives each world a “turn” in a round-robin fashion. If a world is
given its turn, it displays a ball that ascends from the bottom of a
canvas to the top. It relinquishes its turn at that point and the server
gives the next world a turn.</p><p>Here is an image that illustrates how this universe would work if two
worlds participated:</p><p><img src="balls.png" alt="" width="180" height="50"/></p><p>The two <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> programs could be located on two distinct computers
or on just one. A <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a> mediates between the two worlds, including
the initial start-up.</p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""Hints_on_Designing_Universes"">2.4.7.2<tt> </tt><a name="(part._.Hints_on_.Designing_.Universes)"></a>Hints on Designing Universes</h5><p>The first step in designing a <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a> is to understand the
coordination of the <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>s from a global perspective. To some
extent, it is all about knowledge and the distribution of knowledge
throughout a system. We know that the <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a> doesn’t exist until
the server starts and the <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>s are joining. Because of the nature
of computers and networks, however, we may assume little else. Our network
connections ensure that if some <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> or the <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a> sends
two messages to the <span style="font-style: italic">same</span> place in some order, they arrive in the
same order (if they arrive at all). In contrast, if two distinct
<a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> programs send one message each, the network does not
guarantee the order of arrival at the server; similarly, if the
<a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a> is asked to send some messages to several distinct
<a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> programs, they may arrive at those worlds in the order sent
or in the some other order. In the same vein, it is impossible to ensure
that one world joins before another. Worst, when someone removes the
connection (cable, wireless) between a computer that runs a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>
program and the rest of the network or if some network cable is cut,
messages don’t go anywhere. Due to this vagaries, it is therefore the
designer’s task to establish a protocol that enforces a certain order onto
a universe and this activity is called <span style="font-style: italic">protocol design</span>.</p><p>From the perspective of the <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a>, the design of a protocol is
about the design of data representations for tracking universe information
in the server and the participating worlds and the design of a data
representation for messages. As for the latter, we know that they must be
<a href="#%28tech._universe._s._expression%29" class="techoutside" data-pltdoc="x"><span class="techinside">S-expression</span></a>s, but usually <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> programs don’t send all
kinds of <a href="#%28tech._universe._s._expression%29" class="techoutside" data-pltdoc="x"><span class="techinside">S-expression</span></a>s. The data definitions for messages must
therefore select a subset of suitable <a href="#%28tech._universe._s._expression%29" class="techoutside" data-pltdoc="x"><span class="techinside">S-expression</span></a>s. As for the
state of the server and the worlds, they must reflect how they currently
relate to the universe. Later, when we design their “local” behavior, we
may add more components to their state space.</p><p>In summary, the first step of a protocol design is to introduce:</p><ul><li><p>a data definition for the information about the universe that the
server tracks, call it <a href="#%28tech._universestate%29" class="techoutside" data-pltdoc="x"><span class="techinside">UniverseState</span></a>;</p></li><li><p>a data definition for the world(s) about their current relationship
to the universe;</p></li><li><p>data definitions for the messages that are sent from the server to
the worlds and vice versa. Let’s call them <a name="(tech._s2w)"></a><span style="font-style: italic">S2W</span> for messages
from the server to the worlds and <a name="(tech._w2)"></a><span style="font-style: italic">W2S</span> for the other direction;
in the most general case you may need one pair per world.</p></li></ul><p>If all the worlds exhibit the same behavior over time, a single data
definition suffices for step 2. If they play different roles, we may need
one data definition per world.</p><p>Of course, as you define these collections of data always keep in mind what
the pieces of data mean, what they represent from the universe’s
perspective.</p><p>The second step of a protocol design is to figure out which major
events—<wbr></wbr>the addition of a world to the universe, the arrival of a message
at the server or at a world—<wbr></wbr>to deal with and what they imply for the
exchange of messages. Conversely, when a server sends a message to a
world, this may have implications for both the state of the server and the
state of the world. A good tool for writing down these agreements is an
interaction diagram.</p><p><table cellspacing="0" cellpadding="0"><tr><td><p><span class="stt"></span><span class="hspace"> </span></p></td></tr><tr><td><p><span class="stt">Server</span><span class="hspace"> </span><span class="stt">World1</span><span class="hspace"> </span><span class="stt">World2</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">'go</span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|<------------------|</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">'go</span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|------------------------------------------>|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr></table></p><p>Each vertical line is the life line of a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program or the
<a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a>. Each horizontal arrow denotes a message sent from one
<a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a> participant to another.</p><p>The design of the protocol, especially the data definitions, have direct
implications for the design of event handling functions. For example, in
the server we may wish to deal with two kinds of events: the joining of a
new world and the receipt of a message from one of the worlds. This
translates into the design of two functions with the following headers,</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">Bundle is</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="hspace"> </span><span class="RktCmt">(make-bundle UniverseState [Listof mail?] [Listof iworld?])</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">UniverseState iworld? -> Bundle</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">next list of worlds when world </span><span class="RktSym">iw</span><span class="RktCmt"> is joining</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">the universe in state </span><span class="RktSym">s</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">add-world</span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktSym">iw</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">UniverseState iworld? W2U -> Bundle</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">next list of worlds when world </span><span class="RktSym">iw</span><span class="RktCmt"> is sending message </span><span class="RktSym">m</span><span class="RktCmt"> to</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">the universe in state </span><span class="RktSym">s</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">process</span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktSym">iw</span><span class="hspace"> </span><span class="RktSym">m</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr></table></blockquote><p>Finally, we must also decide how the messages affect the states of the
worlds; which of their callback may send messages and when; and what to do
with the messages a world receives. Because this step is difficult to
explain in the abstract, we move on to the protocol design for the
universe of ball worlds.</p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""Designing_the_Ball_Universe"">2.4.7.3<tt> </tt><a name="(part._.Designing_the_.Ball_.Universe)"></a>Designing the Ball Universe</h5><p>Running the ball <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a> has a simple overall goal: to ensure that at any
point in time, one <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> is active and all others are passive. The active
<a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> displays a moving ball, and the passive <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>s should display
something, anything that indicates that it is some other <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>’s turn.</p><p>As for the server’s state, it must obviously keep track of all <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>s that
joined the <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a>, and it must know which one is active and which ones
are passive. Of course, initially the <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a> is empty, i.e., there are
no <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>s and, at that point, the server has nothing to track.</p><p>While there are many different useful ways of representing such a
<a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a>, we just use the list of <span style="font-style: italic">iworlds</span> that is handed to
each handler and that handlers return via their bundles. The
<a href="#%28tech._universestate%29" class="techoutside" data-pltdoc="x"><span class="techinside">UniverseState</span></a> itself is useless for this trivial example. We
interpret non-empty lists as those where the first <span style="font-style: italic">iworld</span> is active
and the remainder are the passive <span style="font-style: italic">iworld</span>s. As for the two possible
events,</p><ul><li><p>it is natural to add new <span style="font-style: italic">iworld</span>s to the end of the list; and</p></li><li><p>it is natural to move an active <span style="font-style: italic">iworld</span> that relinquishes its turn to
the end of the list, too.</p></li></ul><p><div class="SIntrapara">The server should send messages to the first <span style="font-style: italic">iworld</span> of its list as
long as it wishes this <span style="font-style: italic">iworld</span> to remain active. In turn, it should
expect to receive messages only from this one active <span style="font-style: italic">iworld</span> and no
other <span style="font-style: italic">iworld</span>. The content of these two messages is nearly irrelevant
because a message from the server to an <span style="font-style: italic">iworld</span> means that it is the
<span style="font-style: italic">iworld</span>’s turn and a message from the <span style="font-style: italic">iworld</span> to the server
means that the turn is over. Just so that we don’t confuse ourselves, we
use two distinct symbols for these two messages:
</div><div class="SIntrapara"><ul><li><p>A <span style="font-style: italic">GoMessage</span> is <span class="RktVal">'</span><span class="RktVal">it-is-your-turn</span>.</p></li><li><p>A <span style="font-style: italic">StopMessage</span> is <span class="RktVal">'</span><span class="RktVal">done</span>.</p></li></ul></div></p><p><div class="SIntrapara">From the <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a>’s perspective, each <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> is in one of two states:
</div><div class="SIntrapara"><ul><li><p>A passive <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> is <span style="font-style: italic">resting</span>. We use <span class="RktVal">'</span><span class="RktVal">resting</span> for this state.</p></li><li><p>An active <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> is not resting. We delay choosing a representation
for this part of a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>’s state until we design its “local” behavior.</p></li></ul></div><div class="SIntrapara">It is also clear that an active <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> may receive additional messages,
which it may ignore. When it is done with its turn, it will send a
message.</div></p><p><table cellspacing="0" cellpadding="0"><tr><td><p><span class="stt">Server</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">World1</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|<==================|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">'it-is-your-turn |</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|------------------>|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">World2</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|<==========================================|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">'done</span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|<------------------|</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">'it-is-your-turn |</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|------------------------------------------>|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">'done</span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|<------------------------------------------|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">'it-is-your-turn |</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|------------------>|</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr><tr><td><p><span class="stt"></span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span><span class="hspace"> </span><span class="stt">|</span></p></td></tr></table></p><p>Here the double-lines (horizontal) denote the registration step, the others
are message exchanges. The diagram thus shows how the <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a>
decides to make the first registered world the active one and to enlist
all others as they join.</p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""Designing_the_Ball_Server"">2.4.7.4<tt> </tt><a name="(part._.Designing_the_.Ball_.Server)"></a>Designing the Ball Server</h5><p>The preceding subsection dictates that our server program starts like this:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">teachpack: universe.rkt</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">UniverseState is </span><span class="RktCmt">'</span><span class="RktCmt">*</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">StopMessage is </span><span class="RktCmt">'</span><span class="RktCmt">done.</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">GoMessage is </span><span class="RktCmt">'</span><span class="RktCmt">it-is-your-turn.</span></td></tr></table></blockquote><p>The design of a protocol has immediate implications for the design of the
event handling functions of the server. Here we wish to deal with two
events: the appearance of a new world and the receipt of a message. Based
on our data definitions and based on the general contracts of the event
handling functions spelled out in this documentation, we get two functions
for our wish list:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">Result is</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="hspace"> </span><span class="RktCmt">(make-bundle [Listof iworld?]</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="hspace"> </span><span class="RktCmt">(list (make-mail iworld? GoMessage))</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="hspace"> </span><span class="RktCmt">'</span><span class="RktCmt">())</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">[Listof iworld?] iworld? -> Result</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">add world </span><span class="RktSym">iw</span><span class="RktCmt"> to the universe, when server is in state </span><span class="RktSym">u</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">add-world</span><span class="hspace"> </span><span class="RktSym">u</span><span class="hspace"> </span><span class="RktSym">iw</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">[Listof iworld?] iworld? StopMessage -> Result</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">world </span><span class="RktSym">iw</span><span class="RktCmt"> sent message </span><span class="RktSym">m</span><span class="RktCmt"> when server is in state </span><span class="RktSym">u</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">switch</span><span class="hspace"> </span><span class="RktSym">u</span><span class="hspace"> </span><span class="RktSym">iw</span><span class="hspace"> </span><span class="RktSym">m</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr></table></blockquote><p>Although we could have re-used the generic contracts from this
documentation, we also know from our protocol that our server sends a
message to exactly one world. Note how these contracts are just refinements
of the generic ones. (A type-oriented programmer would say that the
contracts here are subtypes of the generic ones.)</p><p>The second step of the design recipe calls for functional examples:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">an obvious example for adding a world:</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">add-world</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">(</span><span class="RktVal">)</span><span class="hspace"> </span><span class="RktSym">world1</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._make-bundle%29%29" class="RktValLink" data-pltdoc="x">make-bundle</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._list%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">list</a></span><span class="hspace"> </span><span class="RktSym">world1</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._list%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">list</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._make-mail%29%29" class="RktValLink" data-pltdoc="x">make-mail</a></span><span class="hspace"> </span><span class="RktSym">world1</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">it-is-your-turn</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">(</span><span class="RktVal">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">an example for receiving a message from the active world:</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">switch</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._list%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">list</a></span><span class="hspace"> </span><span class="RktSym">world1</span><span class="hspace"> </span><span class="RktSym">world2</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym">world1</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">done</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._make-bundle%29%29" class="RktValLink" data-pltdoc="x">make-bundle</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._list%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">list</a></span><span class="hspace"> </span><span class="RktSym">world2</span><span class="hspace"> </span><span class="RktSym">world1</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._list%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">list</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._make-mail%29%29" class="RktValLink" data-pltdoc="x">make-mail</a></span><span class="hspace"> </span><span class="RktSym">world2</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">it-is-your-turn</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">(</span><span class="RktVal">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>Note that our protocol analysis dictates this behavior for the two
functions. Also note how we use <span class="RktSym">world1</span>, <span class="RktSym">world2</span>, and
<span class="RktSym">world3</span> because the teachpack applies these event handlers to real
worlds.</p><p>Exercise: Create additional examples for the two functions based on our
protocol.</p><p>The protocol tells us that <span style="font-style: italic">add-world</span> just adds the given
<span style="font-style: italic">world</span> structure—<wbr></wbr>recall that this a data representation of the
actual <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program—<wbr></wbr>to the given list of worlds. It then sends a
message to the first world on this list to get things going:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">add-world</span><span class="hspace"> </span><span class="RktSym">univ</span><span class="hspace"> </span><span class="RktSym">wrld</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=local.html%23%2528form._%2528%2528lib._racket%252Flocal..rkt%2529._local%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">local</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">univ*</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._append%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">append</a></span><span class="hspace"> </span><span class="RktSym">univ</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._list%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">list</a></span><span class="hspace"> </span><span class="RktSym">wrld</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._make-bundle%29%29" class="RktValLink" data-pltdoc="x">make-bundle</a></span><span class="hspace"> </span><span class="RktSym">univ*</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._list%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">list</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._make-mail%29%29" class="RktValLink" data-pltdoc="x">make-mail</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._first%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">first</a></span><span class="hspace"> </span><span class="RktSym">univ*</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">it-is-your-turn</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">(</span><span class="RktVal">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>Because <span style="font-style: italic">univ*</span> contains at least <span style="font-style: italic">wrld</span>, it is acceptable to
create a mail to <span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._first%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">first</a></span><span class="stt"> </span><span class="RktSym">univ*</span><span class="RktPn">)</span>. Of course, this same reasoning
also implies that if <span style="font-style: italic">univ</span> isn’t empty, its first element is an
active world and is about to receive a second <span class="RktVal">'</span><span class="RktVal">it-is-your-turn</span> message.</p><p>Similarly, the protocol says that when <span style="font-style: italic">switch</span> is invoked because a
<a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> program sends a message, the data representation of the
corresponding world is moved to the end of the list and the next world on
the (resulting) list is sent a message:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">switch</span><span class="hspace"> </span><span class="RktSym">univ</span><span class="hspace"> </span><span class="RktSym">wrld</span><span class="hspace"> </span><span class="RktSym">m</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=local.html%23%2528form._%2528%2528lib._racket%252Flocal..rkt%2529._local%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">local</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">univ*</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._append%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">append</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._rest%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">rest</a></span><span class="hspace"> </span><span class="RktSym">univ</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._list%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">list</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._first%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">first</a></span><span class="hspace"> </span><span class="RktSym">univ</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._make-bundle%29%29" class="RktValLink" data-pltdoc="x">make-bundle</a></span><span class="hspace"> </span><span class="RktSym">univ*</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._list%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">list</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._make-mail%29%29" class="RktValLink" data-pltdoc="x">make-mail</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._first%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">first</a></span><span class="hspace"> </span><span class="RktSym">univ*</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">it-is-your-turn</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">(</span><span class="RktVal">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>As before, appending the first world to the end of the list guarantees
that there is at least this one world on this list. It is therefore
acceptable to create a mail for this world.</p><p>Start the server now.</p><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._universe%29%29" class="RktStxLink" data-pltdoc="x">universe</a></span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">(</span><span class="RktVal">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._on-new%29%29" class="RktStxLink" data-pltdoc="x">on-new</a></span><span class="hspace"> </span><span class="RktSym">add-world</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._%28%28lib._2htdp%2Funiverse..rkt%29._on-msg%29%29" class="RktStxLink" data-pltdoc="x">on-msg</a></span><span class="hspace"> </span><span class="RktSym">switch</span><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote><p>Exercise: The function definition simply assumes that <span style="font-style: italic">wrld</span> is
<span class="RktSym">world=?</span> to <span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._first%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">first</a></span><span class="stt"> </span><span class="RktSym">univ</span><span class="RktPn">)</span> and that the received message
<span style="font-style: italic">m</span> is <span class="RktVal">'</span><span class="RktVal">done</span>. Modify the function definition so that it
checks these assumptions and raises an error signal if either of them is
wrong. Start with functional examples. If stuck, re-read the section on
checked functions from HtDP. (Note: in a <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a> it is quite
possible that a program registers with a <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a> but fails to stick
to the agreed-upon protocol. How to deal with such situations properly
depends on the context. For now, stop the <a href="#%28tech._universe%29" class="techoutside" data-pltdoc="x"><span class="techinside">universe</span></a> at this point by
returning an empty list of worlds. Consider alternative solutions, too.)</p><p>Exercise: An alternative state representation would equate
<a href="#%28tech._universestate%29" class="techoutside" data-pltdoc="x"><span class="techinside">UniverseState</span></a> with <span style="font-style: italic">world</span> structures, keeping track of the
active world. The list of world in the server would track the passive
worlds only. Design appropriate <span class="RktSym">add-world</span> and <span class="RktSym">switch</span>
functions.</p><h5 x-source-module="(lib "teachpack/teachpack.scrbl")" x-part-tag=""Designing_the_Ball_World"">2.4.7.5<tt> </tt><a name="(part._.Designing_the_.Ball_.World)"></a>Designing the Ball World</h5><p>The final step is to design the ball <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>. Recall that each world
is in one of two possible states: active or passive. The second kind of
<a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> moves a ball upwards, decreasing the ball’s <span style="font-style: italic">y</span>
coordinate; the first kind of <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> displays something that says
it’s someone else’s turn. Assuming the ball always moves along a vertical
line and that the vertical line is fixed, the state of the world is an
enumeration of two cases:</p><p><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">teachpack: universe.rkt</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">WorldState is one of:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">–</span><span class="RktCmt"> Number</span><span class="hspace"> </span><span class="RktCmt">%% representing the </span><span style="font-style: italic">y</span><span class="RktCmt"> coordinate</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">–</span><span class="RktCmt"> </span><span class="RktVal">'</span><span class="RktVal">resting</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">WORLD0</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">resting</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">A WorldResult is one of:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">–</span><span class="RktCmt"> WorldState</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">–</span><span class="RktCmt"> (make-package WorldState StopMessage)</span></td></tr></table></blockquote></div><div class="SIntrapara">The definition says that initially a <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> is passive.</div></p><p>The communication protocol and the refined data definition of <a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a>
imply a number of contract and purpose statements:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">WorldState GoMessage -> WorldResult</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">make sure the ball is moving</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">receive</span><span class="hspace"> </span><span class="RktSym">w</span><span class="hspace"> </span><span class="RktSym">n</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">WorldState -> WorldResult</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">move this ball upwards for each clock tick</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">or stay </span><span class="RktVal">'</span><span class="RktVal">resting</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">move</span><span class="hspace"> </span><span class="RktSym">w</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">WorldState -> Image</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">render the world as an image</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">render</span><span class="hspace"> </span><span class="RktSym">w</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr></table></blockquote><p>Let’s design one function at a time, starting with <span style="font-style: italic">receive</span>. Since
the protocol doesn’t spell out what <span style="font-style: italic">receive</span> is to compute, let’s
create a good set of functional examples, exploiting the structure of the
data organization of <a href="#%28tech._world._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a>:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">receive</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">resting</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">it-is-your-turn</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym">HEIGHT</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">receive</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._-%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace"> </span><span class="RktSym">HEIGHT</span><span class="hspace"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">it-is-your-turn</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr></table></blockquote><p>Since there are two kinds of states, we make up at least two kinds of
examples: one for a <span class="RktVal">'</span><span class="RktVal">resting</span> state and another one for a numeric
state. The dots in the result part of the second unit test reveal the
first ambiguity; specifically it isn’t clear what the result should be
when an active <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> receives another message to activate itself. The
second ambiguity shows up when we study additional examples, which are
suggested by our approach to designing functions on numeric intervals
(HtDP, section 3). That is we should consider the following three inputs
to <span style="font-style: italic">receive</span>:</p><ul><li><p><span class="RktSym">HEIGHT</span> when the ball is at the bottom of the image;</p></li><li><p><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._-%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="stt"> </span><span class="RktSym">HEIGHT</span><span class="stt"> </span><span class="RktVal">1</span><span class="RktPn">)</span> when the ball is properly inside the image; and</p></li><li><p><span class="RktVal">0</span> when the ball has hit the top of the image.</p></li></ul><p>In the third case the function could produce three distinct results:
<span class="RktVal">0</span>, <span class="RktVal">'</span><span class="RktVal">resting</span>, or <span class="RktPn">(</span><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._make-package%29%29" class="RktValLink" data-pltdoc="x">make-package</a></span><span class="stt"> </span><span class="RktVal">'</span><span class="RktVal">resting</span><span class="stt"> </span><span class="RktVal">'</span><span class="RktVal">done</span><span class="RktPn">)</span>. The first leaves things alone; the second turns the active <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>
into a resting one; the third does so, too, and tells the universe about
this switch.</p><p>We choose to design <span style="font-style: italic">receive</span> so that it ignores the message and
returns the current state of an active <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>. This ensures that the ball
moves in a continuous fashion and that the <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> remains active.</p><p>Exercise: One alternative design is to move the ball back to the bottom of
the image every time <span class="RktVal">'</span><span class="RktVal">it-is-your-turn</span> is received. Design this function, too.</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">receive</span><span class="hspace"> </span><span class="RktSym">w</span><span class="hspace"> </span><span class="RktSym">m</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=symbols.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._symbol%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">symbol?</a></span><span class="hspace"> </span><span class="RktSym">w</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym">HEIGHT</span><span class="RktPn">]</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">meaning: </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528lib._racket%252Fbool..rkt%2529._symbol%7E3d%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">symbol=?</a></span><span class="stt"> </span><span class="RktSym">w</span><span class="stt"> </span><span class="RktVal">'</span><span class="RktVal">resting</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._else%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">else</a></span><span class="hspace"> </span><span class="RktSym">w</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>Our second function to design is <span style="font-style: italic">move</span>, the function that computes
the ball movement. We have the contract and the second step in the design
recipe calls for examples:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">WorldState -> WorldState or </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._make-package%29%29" class="RktValLink" data-pltdoc="x">make-package</a></span><span class="stt"> </span><span class="RktVal">'</span><span class="RktVal">resting</span><span class="stt"> </span><span class="RktVal">'</span><span class="RktVal">done</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">move the ball if it is flying</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">move</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">resting</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">resting</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">move</span><span class="hspace"> </span><span class="RktSym">HEIGHT</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._-%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace"> </span><span class="RktSym">HEIGHT</span><span class="hspace"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">move</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._-%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace"> </span><span class="RktSym">HEIGHT</span><span class="hspace"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._-%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace"> </span><span class="RktSym">HEIGHT</span><span class="hspace"> </span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">move</span><span class="hspace"> </span><span class="RktVal">0</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._make-package%29%29" class="RktValLink" data-pltdoc="x">make-package</a></span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">resting</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">done</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">move</span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr></table></blockquote><p>Following HtDP again, the examples cover four typical situations:
<span class="RktVal">'</span><span class="RktVal">resting</span>, two end points of the specified numeric interval, and
one interior point. They tell us that <span style="font-style: italic">move</span> leaves a passive <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>
alone and that it otherwise moves the ball until the <span style="font-style: italic">y</span> coordinate
becomes <span class="RktVal">0</span>. In the latter case, the result is a package that
renders the <a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a> passive and tells the server about it.</p><p>Turning these thoughts into a complete definition is straightforward now:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">move</span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=symbols.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._symbol%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">symbol?</a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._number%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">number?</a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">if</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%7E3c%7E3d%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x"><=</a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktVal">0</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._make-package%29%29" class="RktValLink" data-pltdoc="x">make-package</a></span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">resting</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">done</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._sub1%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">sub1</a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>Exercise: what could happen if we had designed <span style="font-style: italic">receive</span> so that it
produces <span class="RktVal">'</span><span class="RktVal">resting</span> when the state of the world is <span class="RktVal">0</span>? Use
your answer to explain why you think it is better to leave this kind of
state change to the tick event handler instead of the message receipt
handler?</p><p>Finally, here is the third function, which renders the state as an image:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">WorldState -> Image</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">render the state of the world as an image</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">render</span><span class="hspace"> </span><span class="RktSym">HEIGHT</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._underlay%2Fxy%29%29" class="RktValLink" data-pltdoc="x">underlay/xy</a></span><span class="hspace"> </span><span class="RktSym">MT</span><span class="hspace"> </span><span class="RktVal">50</span><span class="hspace"> </span><span class="RktSym">HEIGHT</span><span class="hspace"> </span><span class="RktSym">BALL</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">render</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">resting</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._underlay%2Fxy%29%29" class="RktValLink" data-pltdoc="x">underlay/xy</a></span><span class="hspace"> </span><span class="RktSym">MT</span><span class="hspace"> </span><span class="RktVal">10</span><span class="hspace"> </span><span class="RktVal">10</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._text%29%29" class="RktValLink" data-pltdoc="x">text</a></span><span class="hspace"> </span><span class="RktVal">"resting"</span><span class="hspace"> </span><span class="RktVal">11</span><span class="hspace"> </span><span class="RktVal">"red"</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">render</span><span class="hspace"> </span><span class="RktSym">w</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._underlay%2Fxy%29%29" class="RktValLink" data-pltdoc="x">underlay/xy</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=symbols.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._symbol%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">symbol?</a></span><span class="hspace"> </span><span class="RktSym">w</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._underlay%2Fxy%29%29" class="RktValLink" data-pltdoc="x">underlay/xy</a></span><span class="hspace"> </span><span class="RktSym">MT</span><span class="hspace"> </span><span class="RktVal">10</span><span class="hspace"> </span><span class="RktVal">10</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._text%29%29" class="RktValLink" data-pltdoc="x">text</a></span><span class="hspace"> </span><span class="RktVal">"resting"</span><span class="hspace"> </span><span class="RktVal">11</span><span class="hspace"> </span><span class="RktVal">"red"</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._number%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">number?</a></span><span class="hspace"> </span><span class="RktSym">w</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._underlay%2Fxy%29%29" class="RktValLink" data-pltdoc="x">underlay/xy</a></span><span class="hspace"> </span><span class="RktSym">MT</span><span class="hspace"> </span><span class="RktVal">50</span><span class="hspace"> </span><span class="RktSym">w</span><span class="hspace"> </span><span class="RktSym">BALL</span><span class="RktPn">)</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">5</span><span class="hspace"> </span><span class="RktVal">85</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._text%29%29" class="RktValLink" data-pltdoc="x">text</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._name%29%29" class="RktStxLink" data-pltdoc="x">name</a></span><span class="hspace"> </span><span class="RktVal">11</span><span class="hspace"> </span><span class="RktVal">"black"</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>Here is an improvement that adds a name to the image and abstracts over
the name at the same time:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">String -> (WorldState -> Image)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">render the state of the world as an image</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=htdp-langs&rel=beginner.html%23%2528form._%2528%2528lib._lang%252Fhtdp-beginner..rkt%2529._check-expect%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">check-expect</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktPn">(</span><span class="RktSym">draw</span><span class="hspace"> </span><span class="RktVal">"Carl"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">100</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._underlay%2Fxy%29%29" class="RktValLink" data-pltdoc="x">underlay/xy</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._underlay%2Fxy%29%29" class="RktValLink" data-pltdoc="x">underlay/xy</a></span><span class="hspace"> </span><span class="RktSym">MT</span><span class="hspace"> </span><span class="RktVal">50</span><span class="hspace"> </span><span class="RktVal">100</span><span class="hspace"> </span><span class="RktSym">BALL</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">5</span><span class="hspace"> </span><span class="RktVal">85</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._text%29%29" class="RktValLink" data-pltdoc="x">text</a></span><span class="hspace"> </span><span class="RktVal">"Carl"</span><span class="hspace"> </span><span class="RktVal">11</span><span class="hspace"> </span><span class="RktVal">"black"</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">draw</span><span class="hspace"> </span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._name%29%29" class="RktStxLink" data-pltdoc="x">name</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">w</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._overlay%2Fxy%29%29" class="RktValLink" data-pltdoc="x">overlay/xy</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=symbols.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._symbol%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">symbol?</a></span><span class="hspace"> </span><span class="RktSym">w</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._underlay%2Fxy%29%29" class="RktValLink" data-pltdoc="x">underlay/xy</a></span><span class="hspace"> </span><span class="RktSym">MT</span><span class="hspace"> </span><span class="RktVal">10</span><span class="hspace"> </span><span class="RktVal">10</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._text%29%29" class="RktValLink" data-pltdoc="x">text</a></span><span class="hspace"> </span><span class="RktVal">"resting"</span><span class="hspace"> </span><span class="RktVal">11</span><span class="hspace"> </span><span class="RktVal">"red"</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._number%7E3f%2529%2529&version=6.1" class="RktValLink Sq" data-pltdoc="x">number?</a></span><span class="hspace"> </span><span class="RktSym">w</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._underlay%2Fxy%29%29" class="RktValLink" data-pltdoc="x">underlay/xy</a></span><span class="hspace"> </span><span class="RktSym">MT</span><span class="hspace"> </span><span class="RktVal">50</span><span class="hspace"> </span><span class="RktSym">w</span><span class="hspace"> </span><span class="RktSym">BALL</span><span class="RktPn">)</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">5</span><span class="hspace"> </span><span class="RktVal">85</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._text%29%29" class="RktValLink" data-pltdoc="x">text</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._name%29%29" class="RktStxLink" data-pltdoc="x">name</a></span><span class="hspace"> </span><span class="RktVal">11</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">black</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p><div class="SIntrapara">By doing so, we can use the same program to create many different
<a href="#%28tech._world%29" class="techoutside" data-pltdoc="x"><span class="techinside">world</span></a>s that register with a <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a> on your computer:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">String -> WorldState</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">create and hook up a world with the </span><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._.L.O.C.A.L.H.O.S.T%29%29" class="RktValLink" data-pltdoc="x">LOCALHOST</a></span><span class="RktCmt"> server</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://download.racket-lang.org/docs/6.1/html/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=6.1" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">create-world</span><span class="hspace"> </span><span class="RktSym">n</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace"> </span><span class="RktSym">WORLD0</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._on-receive%29%29" class="RktStxLink" data-pltdoc="x">on-receive</a></span><span class="hspace"> </span><span class="RktSym">receive</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">draw</span><span class="hspace"> </span><span class="RktSym">n</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace"> </span><span class="RktSym">move</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._name%29%29" class="RktStxLink" data-pltdoc="x">name</a></span><span class="hspace"> </span><span class="RktSym">n</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28form._universe._%28%28lib._2htdp%2Funiverse..rkt%29._register%29%29" class="RktStxLink" data-pltdoc="x">register</a></span><span class="hspace"> </span><span class="RktSym"><a href="#%28def._universe._%28%28lib._2htdp%2Funiverse..rkt%29._.L.O.C.A.L.H.O.S.T%29%29" class="RktValLink" data-pltdoc="x">LOCALHOST</a></span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p>Now you can use <span class="RktPn">(</span><span class="RktSym">create-world</span><span class="stt"> </span><span class="RktVal">'</span><span class="RktVal">carl</span><span class="RktPn">)</span> and <span class="RktPn">(</span><span class="RktSym">create-world</span><span class="stt"> </span><span class="RktVal">'</span><span class="RktVal">sam</span><span class="RktPn">)</span>,
respectively, to run two different worlds, after launching a <a href="#%28tech._server%29" class="techoutside" data-pltdoc="x"><span class="techinside">server</span></a>
first.</p><p>Exercise: Design a function that takes care of a world to which the
universe has lost its connection. Is <span style="font-style: italic">Result</span> the proper contract for
the result of this function?</p><div class="navsetbottom"><span class="navleft"><form class="searchform"><input class="searchbox" style="color: #888;" type="text" value="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, "6.1", "../");" onfocus="this.style.color="black"; this.style.textAlign="left"; if (this.value == "...search manuals...") this.value="";" onblur="if (this.value.match(/^ *$/)) { this.style.color="#888"; this.style.textAlign="center"; this.value="...search manuals..."; }"/></form> <a href="../index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot("6.1");">top</a></span><span class="navright"> <a href="2htdpimage.html" title="backward to "2.3 Images: "image.rkt""" data-pltdoc="x">← prev</a> <a href="2htdp2htdp.html" title="up to "2 HtDP/2e Teachpacks"" data-pltdoc="x">up</a> <a href="2htdpPlanet_Cute_Images.html" title="forward to "2.5 Planet Cute Images"" data-pltdoc="x">next →</a></span> </div></div></div><div id="contextindicator"> </div></body></html>
|