The Continuation Guild Rocket Pods

Lisp Dialect

Scheme (limited support in Common Lisp with continuation-passing style, or through the use of special libraries)

image with no caption

Synopsis

In the 1970s, a special dialect of Lisp was created that featured a particularly powerful programming feature called continuations. Basically, continuations let you put “time travel” into your code. This allows you to do things like run programs backward, sideways, or in other crazy ways. For instance, it’s great for implementing advanced programming techniques, such as nondeterministic programming. In nondeterministic programming, you write code that offers the computer multiple choices for what to do next. If one choice isn’t satisfactory, the computer can “roll back time” with continuations to try a different path.

Example A-8. Example

(define continuation null)

 (define (foo n)
    (* (call-with-current-continuation
           (lambda (c)
             (set! continuation c)
              (+ n 1)))
      2))

Note

This example is in the Scheme Lisp dialect and won’t run in Common Lisp.

How It Kills Bugs

There are many situations where having time travel in your code can make the code easier to understand. The classic example is in a web server. Often, a person must visit several pages on a web page in order to perform a single action. With a continuation-aware web server, you can write code that pretends these pages were visited all at the same time, making your code a lot less buggy. Later on, the web server uses continuations to break your code into several parts (by using the time-travel abilities of continuations), taking care of all the ugly details of handling a multipage web action.

Explanation

In the example, we create a simple function called foo , which adds one to a number, and then doubles it. For instance, running (foo 7) will return 16. However, inside the function, there is a call to call-with-current-continuation , which captures the state of the function before the doubling step. It saves this “moment in time” in the variable continuation . The current state of the running program is captured at this line . Everything that happens after the continuation was captured will then be executed if we call the captured continuation. The only part of the foo command that happens after the continuation was captured is the multiplication by two . Consequently, the variable continuation is now a time machine that we can use to jump into this past moment to switch out the number we want to double with another one. So, if we were to now call (continuation 100), it would return 200 (which is 100 doubled). We have traveled backward in time!

Weakness

Continuations are such an awesome feature that they don’t really have a downside. The only real problem they present is for creators of programming languages. True continuations are technically difficult to put into a programming language, so few languages support them. Scheme happens to be one of them. To learn more about continuation-based web servers, see “Implementation and Use of the PLT Scheme Web Server”by Shriram Krishnamurthi, et al.

image with no caption
image with no caption
image with no caption
image with no caption
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.84.169