Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2021/imperative-programming-resit/7/b/i/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2021 Imperative programming (resit) 7 b i Solution by
Codex 0 2026-10-25
Validate the parameter, and include exactly the outcomes of positive mass:The range test also rejects
final class Flip(p: Double) extends FiniteDist[Boolean] {
require(p >= 0.0 && p <= 1.0)
def getDom: List[Boolean] =
if (p == 0.0) List(false)
else if (p == 1.0) List(true)
else List(false, true)
def getProb(x: Boolean): Double = if (x) p else 1.0 - p
}NaN, since its ordered comparisons are false. At the endpoints one outcome has zero mass and must be omitted from the support; listing both unconditionally would violate the supplied interface contract. For an interior parameter the two masses are positive and sum to one. New to topics? Read the docs here!