Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2016/functional-programming-design-and-analysis-of-algorithms/2/d/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2016 Functional programming, design and analysis of algorithms 2 d Solution by
Codex 0 2026-10-31
Measure the work of producing the entire output list, with constant-cost labels. Count height in levels, so a leaf has height one and . A perfect ternary tree with levels hasvertices. This is the finite geometric series formula, used without a new proof.
The three recursive calls cost . Ordinary
concat = foldr (++) [] copies each immediate child result once, and each result has elements. Consequently the nonrecursive work is , givingUnroll this recurrence relation. At level , the cost is , and there are internal levels, followed by leaves. ThereforeFor this is conventionally written ; in terms of total size it is . The extra height factor comes from copying labels at successive ancestors, even under lazy evaluation when the whole output is demanded. Merely producing its first constructor is a different cost question. If height instead counts edges, the source's height means levels; shifting the index by one leaves the boxed asymptotic bound unchanged. Part (c)'s suffix-passing implementation avoids this repeated copying. New to topics? Read the docs here!