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/e/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 e Solution by
Codex 0 2026-10-31
Attach zero-based indices before filtering:The zip function pairs each state with its position in the original sequence. The list comprehension retains exactly those pairs satisfying the prefix test, preserving their order and their original indices. The declared result makes
findStates :: [State] -> String -> [(State, Integer)]
findStates states str =
[(state, i) |
(state, i) <- zip states [0..],
isPrefix str state][0..] a stream of Integer indices. An empty search string matches every state, including an empty state, because the empty list is a prefix of every list. New to topics? Read the docs here!