Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== Lab 4. Algebraic Datatype definition ====== ==== 4.1. The type Nat ===== Consider the following type defined to represent natural numbers: <code scala> trait Nat case object Zero extends Nat case class Succ(n: Nat) extends Nat </code> 4.1.1. Implement the following function: <code scala> def isZero(n: Nat): Nat = n match { case Zero => ??? case Succ(np) => ??? } </code> 4.1.2. Implement the addition function over natural numbers: <code scala> def add(n: Nat, m: Nat): Nat = n match { case Zero => ??? case Succ(np) => ??? } </code> 4.1.3. Implement the subtraction function over natural numbers. If $math[n > m] then $math[m - n = 0]. <code scala> def subtract(n: Nat, m: Nat): Nat = ??? </code>