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/4/b/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 4 b Solution by
Codex 0 2026-10-31
Enumerate finite shells of constant coordinate sum:Here natural numbers include zero. Every generated coordinate is nonnegative. Conversely, a given triple occurs in exactly the shell and at exactly those loop indices . Each shell contains only finitely many triples, so every triple is reached after finitely many earlier outputs. This is fair enumeration, unlike nesting three unrestricted infinite lists. For the positive-integer convention, add one to each generated coordinate.
triples :: [(Integer, Integer, Integer)]
triples =
[(a, b, k-a-b) |
k <- [0..],
a <- [0..k],
b <- [0..k-a]] New to topics? Read the docs here!