sealed abstract class IntList {
  def toList: List[Int] =
    this match {
      case ICons (h, t) => h :: t.toList
      case INil ()      => List()
    }

  def contains(x: Int) : Boolean = 
    this match {
      case INil() => false
      case ICons(h, t) => if (x == h) true else { t.contains(x) }
    }

  override def toString: String = 
    toList.mkString("IntList(" + ", " + ")")

  def insertOrdered(x: Int): IntList =
    this match {
      case ICons(h, t) if x > h  => ICons(h, t.insertOrdered(x))
      case _                     => ICons(x, this)
    }
}

case class ICons(h: Int, t: IntList) extends IntList
case class INil()                    extends IntList





