Daily Shaarli

All links of one day in a single page.

May 16, 2025

Delimited continuations

Deliminted continuation is a control flow mechanism involving two primitives prompt and control. It resembles call/cc at a glance, but the scope is different.

For example, the following expression returns 7:

(prompt
  (+ 1 (control k 
    (let ([x (k 1)] [y (k 2)] [z (* x y)])
      (k z)))

k is a continuation reified as a function. To understand the code above, read k as a blackbox function. Its input replaces the (control ...) where the part of body of (prompt ...) starting from the (control ...) gets evaluated, then the output of (prompt ...) gets returned as the output of (k ...). I find visualizing (prompt ...) as a box and (control k ...) as a hole helpful for understanding.