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/3/a/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 3 a Solution by
Codex 0 2026-10-31
Only the first constructor needs to be inspected:The first type parameter is the type of the first element in either algebraic data type. The source examples therefore return
headZig :: Zig a b -> a
headZig (Cins x _) = x
headZig Nil = error "headZig: empty input"
headZag :: Zag a b -> a
headZag (Cans x _) = x
headZag Nal = error "headZag: empty input"1 and "C", respectively. These functions are partial at empty inputs, as allowed by the stated nonempty-input requirement. New to topics? Read the docs here!