Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2015/imperative-programming/7/c/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2015 Imperative programming 7 c Solution by
Codex 0 2026-10-31
Adapt one machine update into one forward iterator element, then apply the skipping iterator of part (a) with and the bidirectional iterator of part (b). Require a positive step and reset once on construction.After forward results at the unexplored frontier, the returned value is the machine's value after exactly updates. A partial final block contributes no element, and
trait StateMachineIter[T] extends StateMachine[T] {
def getBiIter(step: Int): BiIter[T] = {
require(step > 0)
val machine = this
machine.reset()
val everyUpdate = new Iter[T] {
def hasNext: Boolean = !machine.shouldStop
def next(): T = {
if (!hasNext)
throw new NoSuchElementException("StateMachine.next")
machine.update()
machine.getValue
}
}
genBiIter(skipIter(everyUpdate, step-1))
}
}update is never called after shouldStop becomes true. For the printed Add example, the first three observed states areBecause the state machine interface exposes no copy or rollback operation, determining whether enough updates remain generally requires executing them. Thus hasNext may advance the physical machine and cache the next observation. Backward traversal replays stored observations; it does not undo machine effects. This is a trace iterator, with the ordinary cursor semantics of part (b). Assume exclusive use of the machine while this iterator is active, and that returned values remain valid observations rather than mutable views overwritten by later updates. New to topics? Read the docs here!