Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why does this expression evaluate to "hi" ?</p> <pre><code>(let ([x (call/cc (lambda (k) k))]) (x (lambda (ignore) "hi"))) </code></pre> <p>The first step is to decide what <code>k</code> looks like:</p> <pre><code>(define k (lambda (value) (let ((x value)) (x (lambda (ignore) "hi"))))) </code></pre> <p>We see immediately that this is the same as</p> <pre><code>(define k (lambda (x) (x (lambda (ignore) "hi")))) </code></pre> <p>But, I failed to mention one little detail. If <code>k</code> is ever invoked, it is as if it were invoked in tail position.</p> <p>So <code>(f (k 3))</code> for all continuations <code>k</code> built by <code>call/cc</code> is the same as <code>(k 3)</code>. That is always a little bit tricky.</p> <p>So, let's use <code>lambda^</code> to mean that the function it introduces is to be invoked as if it were in tail position.</p> <pre><code>(define k (lambda^ (x) (x (lambda (ignore) "hi")))) </code></pre> <p>Now we know what <code>k</code> is, we also need to know that returning out of <code>(call/cc (lambda (k) k))</code> is really using a default.</p> <p>It should have been written correctly as </p> <pre><code>(call/cc (lambda (k) (k k))). </code></pre> <p>There is always an implied invocation of k at the top of the body of the lambda expression passed to <code>call/cc</code>.</p> <p>We know what <code>k</code> is.</p> <p>So, we know that this must be the same as, (for ease of reading let's turn the <code>x</code>'s in the argument position into <code>y</code>'s.)</p> <pre><code>((lambda^ (x) (x (lambda (ignore) "hi"))) (lambda^ (y) (y (lambda (ignore) "hi")))) </code></pre> <p>So, we evaluate both positions to functions.</p> <p>Once we invoke the function in function position, we are done, since it is headed by <code>lambda^</code> So, we need to know that</p> <pre><code>((lambda^ (x) (x (lambda (ignore) "hi"))) (lambda^ (y) (y (lambda (ignore) "hi")))) </code></pre> <p>evaluates to, substituting for <code>x</code></p> <pre><code>((lambda^ (y) (y (lambda (ignore) "hi"))) (lambda (ignore) "hi")) </code></pre> <p>and one more step, substituting for <code>y</code></p> <p>leads to </p> <p><code>((lambda (ignore) "hi") (lambda (ignore) "hi"))</code>, which ignores its argument and returns "hi"</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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