This is an old revision of the document!
Lab 4. Algebraic Datatype definition
4.1. The type Nat
Consider the following type defined to represent natural numbers:
trait Nat case object Zero extends Nat case class Succ(n: Nat) extends Nat
4.1.1. Implement the following function:
def isZero(n: Nat): Nat = n match { case Zero => ??? case Succ(np) => ??? }
4.1.2. Implement the addition function over natural numbers:
def add(n: Nat, m: Nat): Nat = n match { case Zero => ??? case Succ(np) => ??? }
4.1.3. Implement the subtraction function over natural numbers. If $ n > m$ then $ m - n = 0$ .
def subtract(n: Nat, m: Nat): Nat = ???