A signal-processing prototype uses a pure lazy list with sharing. The function decode n returns n + 1 and takes exactly 3 ms per call. The notation [2,4..] denotes the unbounded sequence 2, 4, 6, and so on.
let samples = map decode [2,4..] early = sum (take 3 samples) later = sum (take 5 (drop 2 samples)) in early + later
map constructs elements lazily, take k selects k elements, and drop k skips k list cells without demanding their numeric contents. Both sums use the same samples binding. Assume the evaluator computes early before later, with no compiler transformations. Ignore all costs except calls to decode.
Find the final numeric result, the number of decode calls, and the total decoding time in ms. Enter each answer as a whole number.
Unlock full course notes by signing up
Cells versus square values
A pure lazy program defines the positive integer squares as follows. The notation [1..] denotes the unbounded list starting at 1; map applies its function lazily to the elements.
Here, take k selects the first k elements, drop k skips the first k, and head requests the next element. Traversing the list spine does not itself demand skipped numeric elements.
Each row starts a fresh evaluation with no previously cached squares. Assume no compiler transformations. Complete the table with the final result and the number of evaluations of n * n. Enter exact integers.
Where sharing begins and ends
A pure program evaluates the following expression using lazy evaluation with sharing. The function costly terminates and returns a number. Producing the final result requires every operand of the additions.
Assume no compiler transformation merges separate expressions. Which statements correctly describe this evaluation? Select all that apply.
From first demand to cached reuse
A single-threaded lazy runtime uses call-by-need. Establishing a binding creates a record holding its delayed expression. After successful evaluation, the runtime replaces that expression with its computed value.
The program below executes its two emit operations in the written order. Each operation demands the complete value of its argument. decode(packet) is deterministic, terminates successfully and has no side effects.
Order the events from establishing the binding through satisfying the second demand.
A right fold that stops demanding its input
A lazy language evaluates the following program. The notation [1..] denotes the infinite list of positive integers, and x:xs separates a list's first element from its remaining elements.
Function arguments are delayed. A conditional evaluates only its selected branch, while multiplication, addition and comparison demand their numeric operands. The output operation demands the complete numeric value of result.
For this hypothetical cost model, each evaluation of n >= 5 costs exactly 2 μs, each multiplication costs exactly 3 μs, and each addition costs exactly 1 μs. List generation and all other operations contribute zero to the counted cost.