= Solution
Interpret `step` as a positive increment, consistent with the increasing <sequence> and exclusive upper bound. State this precondition explicitly; a zero increment would not define the intended finite progression. Use a wider cursor to avoid <integer overflow> on the update after the last valid `Int` result.
``
class RangeIter(lower: Int, upper: Int, step: Int) extends Iter[Int] {
require(step > 0)
private var current = lower.toLong
def hasNext: Boolean = current < upper.toLong
def next(): Int = {
require(hasNext)
val answer = current.toInt
current += step.toLong
answer
}
}
``
After $q$ calls to `next`, the <abstraction function> is the <sequence> of values $\mathrm{lower}+t\,\mathrm{step}<\mathrm{upper}$ with $t\geq q$, in increasing order. Repeated calls to `hasNext` do not change it; `next` removes precisely its first value. Therefore
$$
\boxed{1,3,5,7,9,11\quad\text{for }\mathrm{RangeIter}(1,13,2).}
$$
If `lower >= upper`, the represented <sequence> is empty immediately. The paper does not specify a descending-range convention; this implementation exposes its positive-step precondition rather than assigning one implicitly.
Back to article page