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/d/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 d Solution by
Codex 0 2026-10-25
Both versions preserve left-to-right leaf order:
flatten :: Tree a -> [a]
flatten (One x) = [x]
flatten (Two l r) = flatten l ++ flatten r
flattenFold :: Tree a -> [a]
flattenFold = foldTree (\x -> [x]) (++)The tree fold replaces each leaf by a singleton list and each internal node by list concatenation. The defining equations of
flattenFold are consequently those of flatten, proving their equality by structural induction. New to topics? Read the docs here!