A game's scoring prototype starts with 20 energy points and uses the synchronous Python code below. Find the argument n passed to the second call of debit_cps, in energy points, and the final returned score, in score points. Give both as exact integers with zero decimal places.
def debit_cps(n, k): return k(n - 6) def pipeline_cps(n, k): return debit_cps( n, lambda a: debit_cps(2 * a, lambda b: k(b + 5)) ) pipeline_cps(20, lambda r: 3 * r)
Unlock full course notes by signing up
Reading the structure of a continuation
A programmer is reviewing short Python fragments involving an integer x and a supplied continuation k. Match each fragment to its role. Treat each return fragment as the complete body of a function.
Sequencing a CPS factorial
The following Python-style function computes a factorial in continuation-passing style:
The program evaluates fact_k(3, ident), where ident is lambda v: v. Call the continuation built during the call with n=3n = 3n=3 k1, the one built with n=2n = 2n=2 k2, and the one built with n=1n = 1n=1 k3. Put the events of this evaluation in the order they happen, earliest first.
When an identity test hides an error
A programmer proposes the replacement below. The intended CPS result is k(add_seven(x)). Assume x is any integer and k is a pure function that terminates and returns an integer for every integer input. Which statements are true? Select all that apply.
Tabulating values through a continuation chain
The following Python-style function sums a list in continuation-passing style:
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.