Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The essence of <code>call-with-concurrent-continuation</code>, or <code>call/cc</code> for short, is the ability to grab checkpoints, or continuations, during the execution of a program. Then, you can go back to those checkpoints by applying them like functions.</p> <p>Here's a simple example where the continuation isn't used:</p> <pre><code>&gt; (call/cc (lambda (k) (+ 2 3))) 5 </code></pre> <p>If you don't use the continuation, it's hard to tell the difference. Here's a few where we actually use it:</p> <pre><code>&gt; (call/cc (lambda (k) (+ 2 (k 3)))) 3 &gt; (+ 4 (call/cc (lambda (k) (+ 2 3)))) 9 &gt; (+ 4 (call/cc (lambda (k) (+ 2 (k 3))))) 7 </code></pre> <p>When the continuation is invoked, control flow jumps back to where the continuation was grabbed by <code>call/cc</code>. Think of the <code>call/cc</code> expression as a hole that gets filled by whatever gets passed to <code>k</code>.</p> <p><code>list-iter</code> is a substantially more complex use of <code>call/cc</code>, and might be a difficult place to begin using it. First, here's an example usage:</p> <pre><code>&gt; (define i (list-iter '(a b c))) &gt; (i) a &gt; (i) b &gt; (i) c &gt; (i) list-ended &gt; (i) list-ended </code></pre> <p>Here's a sketch of what's happening:</p> <ol> <li><code>list-iter</code> returns a procedure of no arguments <code>i</code>.</li> <li>When <code>i</code> is invoked, we grab a continuation immediately and pass it to <code>control-state</code>. When that continuation, bound to <code>return</code>, is invoked, we'll immediately return to whoever invoked <code>i</code>.</li> <li>For each element in the list, we grab a new continuation and overwrite the definition of <code>control-state</code> with that new continuation, meaning that we'll resume from there the next time step 2 comes along.</li> <li>After setting up <code>control-state</code> for the next time through, we pass the current element of the list back to the <code>return</code> continuation, yielding an element of the list.</li> <li>When <code>i</code> is invoked again, repeat from step 2 until the <code>for-each</code> has done its work for the whole list.</li> <li>Invoke the <code>return</code> continuation with <code>'list-ended</code>. Since <code>control-state</code> isn't updated, it will keep returning <code>'list-ended</code> every time <code>i</code> is invoked.</li> </ol> <p>As I said, this is a fairly complex use of <code>call/cc</code>, but I hope this is enough to get through this example. For a gentler introduction to continuations, I'd recommend picking up <a href="http://rads.stackoverflow.com/amzn/click/026256100X" rel="nofollow">The Seasoned Schemer</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload