Question

I want to know how to transform a let expression to continuation passing style like this:

(let ([a (lambda (x) (+ 1 x))]) (a 4))

Please show me some examples.Thanks.

Was it helpful?

Solution

First, note that the let is macro-expanded to the following:

((lambda (a)
   (a 4))
 (lambda (x)
   (+ 1 x)))

So, now, let's CPS-transform the above, and we get:

((lambda (a k)
   (a 4 k))
 (lambda (x k)
   (+ 1 x k))
 k)

where the k on the final line is the continuation your original let uses.

If all those ks look too confusing, here's the same code:

((lambda (a k1)
   (a 4 k1))
 (lambda (x k2)
   (+ 1 x k2))
 k0)

where the k0 is the original continuation.

In both cases, I assume that + is also similarly CPS-transformed to take a continuation to pass the result of the addition to....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top