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.
def add_seven(x): return x + 7 def proposed_cps(x, k): return k(x) + 7
With k = lambda v: v + c, the proposed replacement produces the intended result for every integer x and integer constant c.
With k = lambda v: v * v, the proposed replacement produces the intended result for every integer x.
The call to k in the proposed replacement is a tail call because it appears within a return statement.
With k = lambda v: v, the proposed replacement produces the intended result for every integer x.
Unlock full course notes by signing up
Following two result handoffs
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.
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.
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.