Tabulating values through a continuation chain

The following Python-style function sums a list in continuation-passing style:


Python
def sum_k(xs, k):
    if not xs:
        return k(0)
    return sum_k(xs[1:], lambda s: k(xs[0] + s))


The program evaluates sum_k([4, 7, 2], k0), where k0 is lambda r: r * 10. Call the continuation built when xs is [4, 7, 2] c1, the one built when xs is [7, 2] c2, and the one built when xs is [2] c3. The table lists the continuations in the order they are applied. For each one, enter the integer it receives as its argument and the integer it passes to the next continuation, or returns if it is the last.

ContinuationBuilt when xs isArgument receivedValue passed on or returned
c32
c27, 2
c14, 7, 2
k0the initial call

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Continuation Passing Style Questions: