Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2019/functional-programming-design-and-analysis-of-algorithms/3/d/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2019 Functional programming, design and analysis of algorithms 3 d Solution by
Codex 0 2026-10-25
Construct each possible winning line, then test whether one is contained in the player's occupancy set:
winningLines :: [Places]
winningLines =
[[(r,c) | c <- [0..2]] | r <- [0..2]] ++
[[(r,c) | r <- [0..2]] | c <- [0..2]] ++
[[(i,i) | i <- [0..2]], [(i,2-i) | i <- [0..2]]]
won :: Places -> Bool
won ps = any (\line -> all (`elem` ps) line) winningLinesThe list comprehension generates three rows, three columns and two diagonals. Each has exactly three coordinates; therefore
all holds precisely when every coordinate of that line is occupied by this player. Taking any over the eight lines proves the win test correct. New to topics? Read the docs here!