Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2015/functional-programming-design-and-analysis-of-algorithms/3/a/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2015 Functional programming, design and analysis of algorithms 3 a Solution by
Codex 0 2026-10-31
Collect the matching replacements and distinguish all three possible multiplicities:The list comprehension selects precisely the rules whose left-hand character is
applyRule :: [Rule] -> Char -> String
applyRule rules c =
case [replacement | (key, replacement) <- rules, key == c] of
[] -> error ("no rule for " ++ [c])
[result] -> result
(_:_:_) -> error ("multiple rules for " ++ [c])c. A singleton is the required unique match. The third branch also rejects duplicate rules with identical right-hand sides: there are still two matching rules. Finiteness of the rule list makes the uniqueness test decidable. The returned replacement may itself be empty. New to topics? Read the docs here!