Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2017/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 2017 Functional programming, design and analysis of algorithms 3 a Solution by
Codex 0 2026-10-31
Using only list comprehensions:
type Matrix a = [[a]]
type Grid = Matrix Int
choicesComp :: Grid -> Matrix [Int]
choicesComp rows =
[[[d | d <- [1..9], n == 0 || d == n] | n <- row] | row <- rows]The input contract restricts each cell to zero or a digit from one to nine. A definition using standard list functions is:
choicesMap :: Grid -> Matrix [Int]
choicesMap = map (map choice)
where
choice :: Int -> [Int]
choice 0 = [1..9]
choice n = [n]
choices :: Grid -> Matrix [Int]
choices = choicesMapBoth preserve the row structure and construct the initial constraint domains of the Sudoku problem. The alternative names let both definitions coexist; either can supply
choices. New to topics? Read the docs here!