Chapter 15

Chapter 15 - Teach Yourself Scheme in Fixnum Days [Go to...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Teach Yourself Scheme in Fixnum Days [Go to first , previous , next page; contents ; index ] Chapter 15 Engines An engine [ 17 ] represents computation that is subject to timed preemption. In other words, an engine's underlying computation is an ordinary thunk that runs as a timer-preemptable process. An engine is called with three arguments: (1) a number of time units or ticks , (2) a success procedure, and (3) a failure procedure. If the engine computation finishes within the allotted ticks , the success procedure is applied to the computation result and the remaining ticks. If the engine computation could not finish within the allotted ticks , the failure procedure is applied to a new engine representing the unfinished portion of the engine computation. For example, consider an engine whose underlying computation is a loop that printed the nonnegative integers in sequence. It is created as follows, with the soon-to-be-defined make-engine procedure. make-engine is called on an argument thunk representing the underlying computation, and it returns the corresponding engine: (define printn-engine (make-engine (lambda () (let loop ((i 0)) (display i) (display " ") (loop (+ i 1)))))) Here is a call to printn-engine : (define *more* #f) (printn-engine 50 list (lambda (ne) (set! *more* ne))) => 0 1 2 3 4 5 6 7 8 9 Ie, the loop gets to print upto a certain number (here 9 ) and then fails because of the clock interrupt. However, our failure procedure sets a global variable called *more* to the failed engine, which we can use to resume the loop where the previous engine left off: file:///C|/Documents%20and%20Settings/Linda%20Grauer. ..tes/Teach%20Yourself%20Scheme/t-y-scheme-Z-H-17.html (1 of 7) [2/6/2008 11:41:57 AM]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Teach Yourself Scheme in Fixnum Days (*more* 50 list (lambda (ne) (set! *more* ne))) => 10 11 12 13 14 15 16 17 18 19 We will now construct engines using call/cc to capture the unfinished computation of a failing engine. First we will construct flat engines, or engines whose computation cannot include the running of other engines. We will later generalize the code to the more general nestable engines or nesters , which can call other engines. But in both cases, we need a timer mechanism, or a clock . 15.1 The clock Our engines assume the presence of a global clock or interruptable timer that marks the passage of ticks as a program executes. We will assume the following clock interface -- if your Scheme provides any kind of alarm mechanism, it should be an easy matter to rig up a clock of the following type. (Appendix D defines a clock for the Guile [ 13 ] dialect of Scheme.) The internal state of our clock procedure consists of two items: (1) the number of remaining ticks; and (2) an interrupt handler to be invoked when the clock runs out of ticks. clock
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

Chapter 15 - Teach Yourself Scheme in Fixnum Days [Go to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online