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 06. Datatypes in Scala ====== ===== The datatype Nat ===== Consider the following implementation of a simple datatypes to encode natural number: <code scala> trait Nat {} case object Zero extends Nat {} case class Succ(n: Nat) extends Nat {} val three = Succ(Succ(Succ(Zero))) </code> Write a function which converts a ''Nat'' into an ''Int'': <code scala> def toInt(n: Nat): Int = n match { case Zero => ??? case Succ(n) => ??? } </code> Write a function which converts a positive ''Int'' into a ''Nat'': <code scala> def fromInt(i: Int): Nat = ??? </code> ===== The datatype ITree ===== <code scala> trait ITree { def contains (k: Int) : Boolean def ins (k: Int) : ITree def flatten: List[Int] } </code>