Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2019/functional-programming-design-and-analysis-of-algorithms/2/g/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2019 Functional programming, design and analysis of algorithms 2 g Solution by
Codex 0 2026-10-25
buildUnfold :: [a] -> Tree a
buildUnfold = unfold step
where
step [] = error "build: empty input"
step [x] = Left x
step xs = Right (split (length xs `div` 2) xs)On a singleton seed,
unfold produces the same leaf as build; on a longer finite seed, it recursively builds the same two pieces. Induction on input length proves buildUnfold = build on finite nonempty inputs. The empty-input qualification remains essential: a tree unfold cannot supply a missing empty-tree constructor. New to topics? Read the docs here!