I am reading the book Scheme and the Art of Programming, but cannot think of an answer to the following question:
If r is
(escaper (lambda (continuation) (continuation body))
in (... (call/cc r) ...), when can r be rewritten as
(lambda (continuation) body)
The answer is: always.
escaperis not part of Scheme. It is defined by that book, Scheme and the Art of Programming, thus:The result of
(continuation body)in the "escaper-ed" version of(lambda (c..n) (c..n body))would be returned directly into the top level, except,continuationdoes not return. It jumps into its destination context(1), i.e. that which awaits the result of the(call/cc r)call, because thiscontinuationis set up by that call tocall/cc:So if
bodyreturns, that result is passed into(1)bycontinuation--1; and ifbodycallscontinuationwith a value, that value is passed into(1)bycontinuation--1. Nothing is returned tocontinuation--0, so its jump is never triggered.And in
exactly the same thing happens: if
bodyreturns, that result is simply returned into(1); and ifbodycallscontinuationwith a value, that value is passed into(1)bycontinuation--1.