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/1/c/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 1 c Solution by
Codex 0 2026-10-31
Use the right fold definition Set The derived Haskell definition is thereforeEach step prepends the next element to the accumulator. This is a left fold derivation from list concatenation, rather than merely a proposed implementation.
xs ++ ys = foldr (:) ys xs, the right-identity law xs ++ [] = xs, and the finite-list involution law reverse (reverse xs) = xs. The last two laws are the additional results used here. Part (b) now givesxs ++ ys
= foldr (:) ys xs
= foldl (flip (:)) ys (reverse xs)ys = [] to obtain xs = foldl (flip (:)) [] (reverse xs). Substitute reverse xs for xs and simplify the double reversal:reverse xs
= foldl (flip (:)) [] (reverse (reverse xs))
= foldl (flip (:)) [] xsreverse :: [a] -> [a]
reverse = foldl (flip (:)) [] New to topics? Read the docs here!