-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.html
832 lines (589 loc) · 31.3 KB
/
control.html
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"><!-- DO NOT EDIT THIS FILE-->
<!-- Edit the .tex version instead-->
<html>
<head>
<title>Control Structures</title>
<link href="csug.css" rel="stylesheet" type="text/css">
</head>
<body>
<a name="g42"></a>
<a name="./control:h0"></a>
<h1>Chapter 6. Control Structures<a name="CHPTCONTROL"></a></h1>
<p>
This chapter describes <i>Chez Scheme</i> extensions to the set of standard
control structures.
See Chapter <a href="http://scheme.com/tspl4/./control.html#g96">5</a> of <i>The Scheme Programming Language, 4th Edition</i> or the Revised<sup>6</sup> Report
on Scheme for a description of standard control structures.
<p>
<h3><a name="g43"></a><a name="./control:h1"></a>Section 6.1. Conditionals</h3>
<p>
<a name="./control:s0"></a><span class=formdef><b>syntax</b>: <tt>(exclusive-cond <i>clause<sub>1</sub></i> <i>clause<sub>2</sub></i> ...)</tt></span>
<br>
<b>returns: </b>see below
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt>exclusive-cond</tt> is a version of <tt>cond</tt>
(Section <a href="http://scheme.com/tspl4/./control.html#g99">5.3</a> of TSPLFOUR) that differs
from <tt>cond</tt> in that the tests embedded within the clauses
are assumed to be exclusive in the sense that if one of the tests
is true, the others are not.
This allows the implementation to reorder clauses when profiling
information is available at expansion time (Section <a href="./system.html#g114">12.7</a>).
<p>
The <tt>(<i>test</i>)</tt> form of clause is not supported.
The order chosen when profiling information is available is based
on the relative numbers of times the RHS of each clause is executed,
and <tt>(<i>test</i>)</tt> has no RHS.
<tt>(<i>test</i> => values)</tt> is equivalent, abeit less concise.
<p>
<a name="./control:s1"></a><span class=formdef><b>syntax</b>: <tt>(case <i>expr<sub>0</sub></i> <i>clause<sub>1</sub></i> <i>clause<sub>2</sub></i> ...)</tt></span>
<br>
<b>returns: </b>see below
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>Each clause but the last must take one of the forms:
<p>
<p><tt>((<i>key</i> ...) <i>expr<sub>1</sub></i> <i>expr<sub>2</sub></i> ...)<br>
(<i>key</i> <i>expr<sub>1</sub></i> <i>expr<sub>2</sub></i> ...)</tt>
<p>where each <tt><i>key</i></tt> is a datum distinct from the other keys.
The last clause may be in the above form or it may be an
<a name="./control:s2"></a><tt>else</tt> clause of the form
<p>
<p><tt>(else <i>expr<sub>1</sub></i> <i>expr<sub>2</sub></i> ...)</tt>
<p><tt><i>expr<sub>0</sub></i></tt> is evaluated and the result is compared
(using <tt>equal?</tt>) against the keys of each clause in order.
If a clause containing a matching key is found, the
expressions <tt><i>expr<sub>1</sub></i> <i>expr<sub>2</sub></i> ...</tt> are evaluated in sequence
and the values of the last expression are returned.
<p>
If none of the clauses contains a matching key and an <tt>else</tt> clause
is present, the expressions <tt><i>expr<sub>1</sub></i> <i>expr<sub>2</sub></i> ...</tt> of the
<tt>else</tt> clause are evaluated in sequence and the values of the last
expression are returned.
<p>
If none of the clauses contains a matching key and no <tt>else</tt> clause
is present, the value or values are unspecified.
<p>
The Revised<sup>6</sup> Report version of <tt>case</tt> does not support singleton
keys (the second of the first two clause forms above) and uses
<tt>eqv?</tt> rather than <tt>equal?</tt> as the comparison procedure.
Both versions are defined in terms of <tt>exclusive-cond</tt> so that
if profiling information is available at expansion time, the clauses will
be reordered to put those that are most frequently executed first.
<p>
<p><tt>(let ([ls '(ii iv)])<br>
(case (car ls)<br>
[i 1]<br>
[ii 2]<br>
[iii 3]<br>
[(iiii iv) 3]<br>
[else 'out-of-range])) <img src="math/csug/0.gif" alt="<graphic>"> 2
<br>
<br>
(define p<br>
(lambda (x)<br>
(case x<br>
[("abc" "def") 'one]<br>
[((a b c)) 'two]<br>
[else #f])))
<br>
<br>
(p (string #\d #\e #\f)) <img src="math/csug/0.gif" alt="<graphic>"> one<br>
(p '(a b c)) <img src="math/csug/0.gif" alt="<graphic>"> two</tt>
<p><a name="./control:s3"></a><span class=formdef><b>syntax</b>: <tt>(record-case <i>expr</i> <i>clause<sub>1</sub></i> <i>clause<sub>2</sub></i> ...)</tt></span>
<br>
<b>returns: </b>see explanation
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt>record-case</tt> is a restricted form of <tt>case</tt> that supports the
destructuring of <a name="./control:s4"></a><i>records</i>, or <a name="./control:s5"></a><i>tagged lists</i>.
A record has as its first element a tag that determines what "type"
of record it is; the remaining elements are the fields of the record.
<p>
Each clause but the last must take the form
<p>
<p><tt>((<i>key</i> ...) <i>formals</i> <i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...)</tt>
<p>where each <tt><i>key</i></tt> is a datum distinct from the other keys.
The last clause may be in the above form or it may be an
<a name="./control:s6"></a><tt>else</tt> clause of the form
<p>
<p><tt>(else <i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...)</tt>
<p><tt><i>expr</i></tt> must evaluate to a pair.
<tt><i>expr</i></tt> is evaluated and the car of its value is compared
(using <tt>eqv?</tt>) against the keys of each clause in order.
If a clause containing a matching key is found, the variables in
<tt><i>formals</i></tt> are bound to the remaining elements
of the list and the expressions
<tt><i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...</tt> are evaluated in sequence.
The value of the last expression is returned.
The effect is identical to the application of
<p>
<p><tt>(lambda <i>formals</i> <i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...)</tt>
<p>to the cdr of the list.
<p>
If none of the clauses contains a matching key and an <tt>else</tt> clause
is present, the expressions <tt><i>body<sub>1</sub></i> <i>body<sub>2</sub></i> ...</tt> of the
<tt>else</tt> clause are evaluated in sequence and the value of the last
expression is returned.
<p>
If none of the clauses contains a matching key and no <tt>else</tt> clause
is present, the value is unspecified.
<p>
<p><tt>(define calc<br>
(lambda (x)<br>
(record-case x<br>
[(add) (x y) (+ x y)]<br>
[(sub) (x y) (- x y)]<br>
[(mul) (x y) (* x y)]<br>
[(div) (x y) (/ x y)]<br>
[else (assertion-violationf 'calc "invalid expression ~s" x)])))
<br>
<br>
(calc '(add 3 4)) <img src="math/csug/0.gif" alt="<graphic>"> 7<br>
(calc '(div 3 4)) <img src="math/csug/0.gif" alt="<graphic>"> 3/4</tt>
<p>
<h3><a name="g44"></a><a name="./control:h2"></a>Section 6.2. Mapping and Folding</h3>
<p>
<a name="./control:s7"></a><span class=formdef><b>procedure</b>: <tt>(ormap <i>procedure</i> <i>list<sub>1</sub></i> <i>list<sub>2</sub></i> ...)</tt></span>
<br>
<b>returns: </b>see explanation
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt>ormap</tt> is identical to the Revised<sup>6</sup> Report <tt>exists</tt>.
<p>
<a name="./control:s8"></a><span class=formdef><b>procedure</b>: <tt>(andmap <i>procedure</i> <i>list<sub>1</sub></i> <i>list<sub>2</sub></i> ...)</tt></span>
<br>
<b>returns: </b>see explanation
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt>andmap</tt> is identical to the Revised<sup>6</sup> Report <tt>for-all</tt>.
<p>
<h3><a name="g45"></a><a name="./control:h3"></a>Section 6.3. Continuations</h3>
<p>
<i>Chez Scheme</i> supports one-shot continuations as well as the standard
multi-shot continuations obtainable via <tt>call/cc</tt>.
One-shot continuations are continuations that may be invoked at most
once, whether explicitly or implicitly.
They are obtained with <tt>call/1cc</tt>.
<p>
<a name="./control:s9"></a><span class=formdef><b>procedure</b>: <tt>(call/1cc <i>procedure</i>)</tt></span>
<br>
<b>returns: </b>see below
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt>call/1cc</tt> obtains its continuation and passes it to <tt><i>procedure</i></tt>,
which should accept one argument.
The continuation itself is represented by a procedure.
This procedure normally takes one argument but may take an arbitrary
number of arguments depending upon whether the context of the call
to <tt>call/1cc</tt>
expects multiple return values or not.
When this procedure is applied to a value or values, it returns the values
to the continuation of the <tt>call/1cc</tt> application.
<p>
The continuation obtained by <tt>call/1cc</tt> is a
<a name="./control:s10"></a>"one-shot continuation."
A one-shot continuation should not be returned to multiple times, either
by invoking the continuation or returning normally from <tt><i>procedure</i></tt> more
than once.
A one-shot continuation is "promoted" into a normal (multishot)
continuation, however, if it is
still active when a
normal continuation is obtained by <tt>call/cc</tt>.
After a one-shot continuation is promoted into a multishot continuation,
it behaves exactly as if it had been obtained via <tt>call/cc</tt>.
This allows <tt>call/cc</tt> and <tt>call/1cc</tt> to be used together
transparently in many applications.
<p>
One-shot continuations may be more efficient for some applications than
multishot continuations.
See the paper "Representing control in the presence of one-shot
continuations" [<a class=citation href="./bibliography.html#g149">3</a>] for more information about
one-shot continuations, including how they are implemented in
<i>Chez Scheme</i>.
<p>
The following examples highlight the similarities and differences
between one-shot and normal continuations.
<p>
<p><tt>(define prod<br>
; compute the product of the elements of ls, bugging out<br>
; with no multiplications if a zero element is found<br>
(lambda (ls)<br>
(lambda (k)<br>
(if (null? ls)<br>
1<br>
(if (= (car ls) 0)<br>
(k 0)<br>
(* (car ls) ((prod (cdr ls)) k)))))))
<br>
<br>
(call/cc (prod '(1 2 3 4))) <img src="math/csug/0.gif" alt="<graphic>"> 24<br>
(call/1cc (prod '(1 2 3 4))) <img src="math/csug/0.gif" alt="<graphic>"> 24
<br>
<br>
(call/cc (prod '(1 2 3 4 0))) <img src="math/csug/0.gif" alt="<graphic>"> 0<br>
(call/1cc (prod '(1 2 3 4 0))) <img src="math/csug/0.gif" alt="<graphic>"> 0
<br>
<br>
(let ([k (call/cc (lambda (x) x))])<br>
(k (lambda (x) 0))) <img src="math/csug/0.gif" alt="<graphic>"> 0
<br>
<br>
(let ([k (call/1cc (lambda (x) x))])<br>
(k (lambda (x) 0))) <img src="math/csug/0.gif" alt="<graphic>"> <i>exception</i></tt>
<p><a name="./control:s11"></a><a name="dynamic-wind"></a>
<span class=formdef><b>procedure</b>: <tt>(dynamic-wind <i>in</i> <i>body</i> <i>out</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(dynamic-wind <i>critical?</i> <i>in</i> <i>body</i> <i>out</i>)</tt></span>
<br>
<b>returns: </b>values resulting from the application of <tt><i>body</i></tt>
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>The first form is identical to the Revised<sup>6</sup> Report <tt>dynamic-wind</tt>.
When the optional <tt><i>critical?</i></tt> argument is present and non-false,
the <tt><i>in</i></tt> thunk is invoked in a critical section along with the code
that records that the body has been entered, and the <tt><i>out</i></tt> thunk is
invoked in a critical section section along with the code that records
that the body has been exited.
Extreme caution must be taken with this form of <tt>dynamic-wind</tt>,
since an error or long-running computation can leave interrupts
and automatic garbage collection disabled.
<p>
<h3><a name="g46"></a><a name="./control:h4"></a>Section 6.4. Engines<a name="SECTENGINES"></a></h3>
<p>
<a name="./control:s12"></a>Engines are a high-level process abstraction supporting
<a name="./control:s13"></a><i>timed preemption</i> [<a class=citation href="./bibliography.html#g161">15</a>,<a class=citation href="./bibliography.html#g169">23</a>].
Engines may be used to simulate <a name="./control:s14"></a>multiprocessing, implement operating
system kernels, and perform <a name="./control:s15"></a>nondeterministic computations.
<p>
<a name="./control:s16"></a><span class=formdef><b>procedure</b>: <tt>(make-engine <i>thunk</i>)</tt></span>
<br>
<b>returns: </b>an engine
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>An engine is created by passing a thunk (no argument procedure)
to <tt>make-engine</tt>.
The body of the thunk is the computation to be performed by the engine.
An engine itself is a procedure of three arguments:
<p>
<dl compact>
<dt><tt><i>ticks</i></tt>:<dd>
<a name="./control:s17"></a>a positive integer that specifies
the amount of <i>fuel</i> to be given
to the engine.
An engine executes until this fuel runs out or until its computation
finishes.
<p>
<dt><tt><i>complete</i></tt>:<dd>
<a name="./control:s18"></a>a procedure of one or more
arguments that
specifies what to do if the computation finishes.
Its arguments are the amount of fuel left over and the
values produced by the computation.
<p>
<dt><tt><i>expire</i></tt>:<dd>
<a name="./control:s19"></a>a procedure of one argument that
specifies what to do if the fuel runs
out before the computation finishes.
Its argument is a new engine capable of continuing the computation
from the point of interruption.
</dl>
<p>
When an engine is applied to its arguments, it sets up a timer
to fire in <tt><i>ticks</i></tt> time units.
(See <tt>set-timer</tt> on page <a href="./system.html#desc:set-timer">310</a>.)
If the engine computation completes before the timer expires, the
system invokes <tt><i>complete</i></tt>, passing
it the number of <tt><i>ticks</i></tt> left over and
the values produced by the computation.
If, on the other hand, the timer goes off before the engine computation
completes, the system creates a new engine from the continuation of
the interrupted computation and passes this engine to <tt><i>expire</i></tt>.
<tt><i>complete</i></tt> and <tt><i>expire</i></tt> are invoked in the continuation
of the engine invocation.
<p>
An implementation of engines is given
in Section <a href="http://scheme.com/tspl4/./examples.html#g208">12.11</a>.
of <i>The Scheme Programming Language, 4th Edition</i>.
<p>
Do not use the timer interrupt (see <a name="./control:s20"></a><tt>set-timer</tt>) and <a name="./control:s21"></a>engines
at the same time, since engines are implemented in terms of the timer.
<p>
The following example creates an engine from a trivial computation,
3, and gives the engine 10 ticks.
<p>
<p><tt>(define eng<br>
(make-engine<br>
(lambda () 3)))
<br>
<br>
(eng 10<br>
(lambda (ticks value) value)<br>
(lambda (x) x)) <img src="math/csug/0.gif" alt="<graphic>"> 3</tt>
<p>It is often useful to pass <tt>list</tt> as the <tt><i>complete</i></tt>
procedure to an engine, causing an engine that completes to return a
list whose first element is the ticks remaining and whose remaining elements
are the values returned by the computation.
<p>
<p><tt>(define eng<br>
(make-engine<br>
(lambda () 3)))
<br>
<br>
(eng 10<br>
list<br>
(lambda (x) x)) <img src="math/csug/0.gif" alt="<graphic>"> (9 3)</tt>
<p>In the example above, the value is 3 and there are 9 ticks left over,
i.e., it takes one unit of fuel to evaluate 3.
(The fuel amounts given here are for illustration only.
Your mileage may vary.)
<p>
Typically, the engine computation does not finish in one try.
<a name="./control:s22"></a>The following example displays the use of an engine to
compute the 10th Fibonacci number in steps.
<p>
<p><tt>(define fibonacci<br>
(lambda (n)<br>
(let fib ([i n])<br>
(cond<br>
[(= i 0) 0]<br>
[(= i 1) 1]<br>
[else (+ (fib (- i 1))<br>
(fib (- i 2)))]))))
<br>
<br>
(define eng<br>
(make-engine<br>
(lambda ()<br>
(fibonacci 10))))
<br>
<br>
(eng 50<br>
list<br>
(lambda (new-eng)<br>
(set! eng new-eng)<br>
"expired")) <img src="math/csug/0.gif" alt="<graphic>"> "expired"
<br>
<br>
(eng 50<br>
list<br>
(lambda (new-eng)<br>
(set! eng new-eng)<br>
"expired")) <img src="math/csug/0.gif" alt="<graphic>"> "expired"
<br>
<br>
(eng 50<br>
list<br>
(lambda (new-eng)<br>
(set! eng new-eng)<br>
"expired")) <img src="math/csug/0.gif" alt="<graphic>"> "expired"
<br>
<br>
(eng 50<br>
list<br>
(lambda (new-eng)<br>
(set! eng new-eng)<br>
"expired")) <img src="math/csug/0.gif" alt="<graphic>"> (21 55)</tt>
<p>Each time the engine's fuel runs out, the <tt><i>expire</i></tt> procedure assigns
<tt>eng</tt> to the new engine.
The entire computation requires four blocks of 50 ticks to complete; of the
last 50 it uses all but 21.
Thus, the total amount of fuel used is 179 ticks.
This leads to the following procedure, <tt>mileage</tt>, which "times" a
computation using engines:
<p>
<p><tt>(define mileage<br>
(lambda (thunk)<br>
(let loop ([eng (make-engine thunk)] [total-ticks 0])<br>
(eng 50<br>
(lambda (ticks . values)<br>
(+ total-ticks (- 50 ticks)))<br>
(lambda (new-eng)<br>
(loop new-eng<br>
(+ total-ticks 50)))))))
<br>
<br>
(mileage (lambda () (fibonacci 10))) <img src="math/csug/0.gif" alt="<graphic>"> 179</tt>
<p>The choice of 50 for the number of ticks to use each time is
arbitrary, of course.
It might make more sense to pass a much larger number, say 10000,
in order to reduce the number of times the computation is interrupted.
<p>
The next procedure is similar to <tt>mileage</tt>, but it returns a list
of engines, one for each tick it takes to complete the computation.
Each of the engines in the list represents a "snapshot" of the
computation, analogous to a single frame of a moving picture.
<tt>snapshot</tt> might be useful for "single stepping" a computation.
<p>
<p><tt>(define snapshot<br>
(lambda (thunk)<br>
(let again ([eng (make-engine thunk)])<br>
(cons eng<br>
(eng 1 (lambda (t . v) '()) again)))))</tt>
<p>The recursion embedded in this procedure is rather strange.
The complete procedure performs the base case, returning the empty
list, and the expire procedure performs the recursion.
<p>
The next procedure, <a name="./control:s23"></a><tt>round-robin</tt>, could be the basis for a simple
time-sharing <a name="./control:s24"></a>operating system.
<tt>round-robin</tt> maintains a queue of processes (a list of engines),
cycling through the queue in a <i>round-robin</i> fashion, allowing each
process to run for a set amount of time.
<tt>round-robin</tt> returns a list of the values returned by the engine
computations in the order that the computations complete.
Each computation is assumed to produce exactly one value.
<p>
<p><tt>(define round-robin<br>
(lambda (engs)<br>
(if (null? engs)<br>
'()<br>
((car engs)<br>
1<br>
(lambda (ticks value)<br>
(cons value (round-robin (cdr engs))))<br>
(lambda (eng)<br>
(round-robin<br>
(append (cdr engs) (list eng))))))))</tt>
<p>Since the amount of fuel supplied each time, one tick, is constant,
the effect of <tt>round-robin</tt> is to return a list of the values sorted
from the quickest to complete to the slowest to complete.
Thus, when we call <tt>round-robin</tt> on a list of engines, each computing
one of the Fibonacci numbers, the output list is sorted with the earlier
Fibonacci numbers first, regardless of the order of the input list.
<p>
<p><tt>(round-robin<br>
(map (lambda (x)<br>
(make-engine<br>
(lambda ()<br>
(fibonacci x))))<br>
'(4 5 2 8 3 7 6 2))) <img src="math/csug/0.gif" alt="<graphic>"> (1 1 2 3 5 8 13 21)</tt>
<p>More interesting things can happen if the amount of fuel varies
each time through the loop.
<a name="./control:s25"></a>In this case, the computation would
be nondeterministic, i.e., the results would vary from call to call.
<p>
The following syntactic form, <a name="./control:s26"></a><tt>por</tt> (parallel-or), returns the
first of its expressions to complete with a true value.
<tt>por</tt> is implemented with the procedure <tt>first-true</tt>, which is
similar to <tt>round-robin</tt> but quits when any of the engines
completes with a true value.
If all of the engines complete, but none with a true value,
<tt>first-true</tt> (and hence <tt>por</tt>) returns <tt>#f</tt>.
Also, although <tt>first-true</tt> passes a fixed amount of fuel to each
engine, it chooses the next engine to run at random, and is thus
nondeterministic.
<p>
<p><tt>(define-syntax por<br>
(syntax-rules ()<br>
[(_ x ...)<br>
(first-true<br>
(list (make-engine (lambda () x)) ...))]))
<br>
<br>
(define first-true<br>
(let ([pick<br>
(lambda (ls)<br>
(list-ref ls (random (length ls))))])<br>
(lambda (engs)<br>
(if (null? engs)<br>
#f<br>
(let ([eng (pick engs)])<br>
(eng 1<br>
(lambda (ticks value)<br>
(or value<br>
(first-true<br>
(remq eng engs))))<br>
(lambda (new-eng)<br>
(first-true<br>
(cons new-eng<br>
(remq eng engs))))))))))</tt>
<p>The list of engines is maintained with <tt>pick</tt>, which randomly
chooses an element of the list, and <tt>remq</tt>, which removes the
chosen engine from the list.
Since <tt>por</tt> is nondeterministic, subsequent uses with the same
expressions may not return the same values.
<p>
<p><tt>(por 1 2 3) <img src="math/csug/0.gif" alt="<graphic>"> 2<br>
(por 1 2 3) <img src="math/csug/0.gif" alt="<graphic>"> 3<br>
(por 1 2 3) <img src="math/csug/0.gif" alt="<graphic>"> 2<br>
(por 1 2 3) <img src="math/csug/0.gif" alt="<graphic>"> 1</tt>
<p>Furthermore, even if one of the expressions is an infinite loop,
<tt>por</tt> still finishes as long as one of the other expressions
completes and returns a true value.
<p>
<p><tt>(por (let loop () (loop)) 2) <img src="math/csug/0.gif" alt="<graphic>"> 2</tt>
<p>With <tt>engine-return</tt> and <tt>engine-block</tt>, it is possible to
terminate an engine explicitly.
<tt>engine-return</tt> causes the engine to complete, as if the
computation had finished.
Its arguments are passed to the <tt><i>complete</i></tt> procedure along with the
number of ticks remaining.
It is essentially a nonlocal exit from the engine.
Similarly, <tt>engine-block</tt> causes the engine to expire, as if the
timer had run out.
A new engine is made from the continuation of the call to <tt>engine-block</tt>
and passed to the <tt><i>expire</i></tt> procedure.
<p>
<a name="./control:s27"></a><span class=formdef><b>procedure</b>: <tt>(engine-block)</tt></span>
<br>
<b>returns: </b>does not return
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>This causes a running engine to stop, create a new engine capable
of continuing the computation, and pass the new engine to the original
engine's third argument
(the expire procedure).
Any remaining fuel is forfeited.
<p>
<p><tt>(define eng<br>
(make-engine<br>
(lambda ()<br>
(engine-block)<br>
"completed")))
<br>
<br>
(eng 100<br>
(lambda (ticks value) value)<br>
(lambda (x)<br>
(set! eng x)<br>
"expired")) <img src="math/csug/0.gif" alt="<graphic>"> "expired"
<br>
<br>
(eng 100<br>
(lambda (ticks value) value)<br>
(lambda (x)<br>
(set! eng x)<br>
"expired")) <img src="math/csug/0.gif" alt="<graphic>"> "completed"</tt>
<p><a name="./control:s28"></a><span class=formdef><b>procedure</b>: <tt>(engine-return <i>obj</i> ...)</tt></span>
<br>
<b>returns: </b>does not return
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>This causes a running engine to stop and pass control to the
engine's <tt><i>complete</i></tt> argument.
The first argument passed to the complete procedure is the amount of
fuel remaining, as usual, and
the remaining arguments are the objects <tt><i>obj</i> ...</tt>
passed to <tt>engine-return</tt>.
<p>
<p><tt>(define eng<br>
(make-engine<br>
(lambda ()<br>
(reverse (engine-return 'a 'b 'c)))))
<br>
<br>
(eng 100<br>
(lambda (ticks . values) values)<br>
(lambda (new-eng) "expired")) <img src="math/csug/0.gif" alt="<graphic>"> (a b c)</tt>
<p>
<hr class=copyright align=left>
<p>
<a class=plain href="index.html">Chez Scheme Version 9 User's Guide</a><br>
Copyright © 2016 Cisco Systems, Inc.<br>
Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License Version 2.0</a>
(<a class=plain href="canned/copyright.html">full copyright notice.</a>).</br>
Revised June 2016 for Chez Scheme Version 9.4<br>
<a class=plain href="canned/about.html">about this book</a>
</tr></table>
</body>
</html>