Chapter 13

Chapter 13 - 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 13 Jumps One of the signal features of Scheme is its support for jumps or nonlocal control . Specifically, Scheme allows program control to jump to arbitrary locations in the program, in contrast to the more restrained forms of program control flow allowed by conditionals and procedure calls. Scheme's nonlocal control operator is a procedure named call-with-current-continuation . We will see how this operator can be used to create a breathtaking variety of control idioms. 13.1 call-with-current-continuation The operator call-with-current-continuation call s its argument, which must be a unary procedure, with a value called the `` current continuation ''. If nothing else, this explains the name of the operator. But it is a long name, and is often abbreviated call/cc . 6 The current continuation at any point in the execution of a program is an abstraction of the rest of the program . Thus in the program (+ 1 (call/cc (lambda (k) (+ 2 (k 3))))) the rest of the program, from the point of view of the call/cc -application, is the following program- with-a-hole (with [] representing the hole): (+ 1 []) In other words, this continuation is a program that will add 1 to whatever is used to fill its hole. This is what the argument of call/cc is called with . Remember that the argument of call/cc is the procedure (lambda (k) file:///C|/Documents%20and%20Settings/Linda%20Grauer. ..tes/Teach%20Yourself%20Scheme/t-y-scheme-Z-H-15.html (1 of 8) [2/6/2008 11:41:30 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 (+ 2 (k 3))) This procedure's body applies the continuation (bound now to the parameter k ) to the argument 3 . This is when the unusual aspect of the continuation springs to the fore. The continuation call abruptly abandons its own computation and replaces it with the rest of the program saved in k ! In other words, the part of the procedure involving the addition of 2 is jettisoned, and k 's argument 3 is sent directly to the program-with-the-hole: (+ 1 []) The program now running is simply (+ 1 3) which returns 4 . In sum, (+ 1 (call/cc (lambda (k) (+ 2 (k 3))))) => 4 The above illustrates what is called an escaping continuation, one used to exit out of a computation (here: the (+ 2 []) computation). This is a useful property, but Scheme's continuations can also be used to return to previously abandoned contexts, and indeed to invoke them many times. The ``rest of the program'' enshrined in a continuation is available whenever and how many ever times we choose to recall it, and this is what contributes to the great and sometimes confusing versatility of call/cc . As a quick example, type the following at the listener: (define r #f) (+ 1 (call/cc (lambda (k) (set! r k) (+ 2 (k 3))))) => 4 The latter expression returns 4 as before. The difference between this use of call/cc and the previous example is that here we also store the continuation k in a global variable
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 / 8

Chapter 13 - 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