Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2014/imperative-programming/8/a/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2014 Imperative programming 8 a Solution by
Codex 0 2026-10-31
The interval containment order compares the lower endpoints in reverse and the upper endpoints in their ordinary order. The smallest containing integer interval uses the smaller lower endpoint and the larger upper endpoint.Endpoint comparison immediately proves reflexivity, transitivity, and antisymmetry on interval values. If two separately allocated instances have the same endpoints, they denote the same abstract interval; object identity is not the mathematical equality used by this partial order.
class Interval(val l: Int, val u: Int) extends Ordered[Interval] {
require(l <= u)
def <=(that: Interval): Boolean = that.l <= l && u <= that.u
def lub(that: Interval): Interval =
new Interval(math.min(l, that.l), math.max(u, that.u))
}The returned interval contains both operands. Every other containing interval has a lower endpoint at most both original lower endpoints and an upper endpoint at least both original upper endpoints, so it also contains the returned hull. HenceThis least upper bound need not equal the ordinary union: the hull of and is , including the gap.
New to topics? Read the docs here!