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-resit/4/e/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 (resit) 4 e Solution by
Codex 0 2026-10-31
Store two consecutive Fibonacci numbers as the state:
fibs :: [Integer]
fibs = unfold (const False) fst step (0,1)
where
step (a,b) = (b,a+b)The invariant is that the state is , with . Emitting its first component and updating the pair therefore produces .
Integer avoids fixed-width overflow; the bit cost of adding increasingly large integers is separate from the number of unfold steps. New to topics? Read the docs here!