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 ====== Consider the following type defined to represent lists of integers: <code scala> trait IList case object Void extends IList case class Cons(x: Int, xs: IList) extends IList </code> **4.1.** Implement ''isEmpty'' which checks if a list is empty: <code scala> def isEmpty(l: IList): Boolean = ??? </code> **4.2.** Implement ''size'' which returns the size of the list: <code scala> def size(l: IList): Int = ??? </code> **4.3.** Implement ''append'' which concatenates two lists: <code scala> def append(l1: IList, l2: IList): IList = ??? </code> **4.4.** (!) Implement ''last'' which returns the last element from a list: <code scala> def last(l: IList): Int = ??? </code> **4.5.** (!) Implement ''reverse''. There are two different ways to implement reverse (with direct and with tail-end recursion). Try both implementations. <code scala> def reverse(l: IList): IList = ??? </code> **4.6.** Implement ''contains'' which checks if an element is a member of a list. <code scala> def contains(e: Int, l: IList): Boolean = ??? </code> **4.7.** Implement ''max'' which returns the largest integer from a list: <code scala> def max(l: IList): Int = ??? </code> **4.8.** Implement ''take'' which returns a new list containing the first ''n'' elements of the original list: <code scala> def take(n: Int)(l: IList): IList = ??? </code> **4.9.** Implement ''drop'' which returns a new list containing the original list without the first ''n'' elements: <code scala> def drop(n: Int)(l: IList): IList = ??? </code> **4.10.** Implement ''isSorted'' which checks if a list is sorted: <code scala> def isSorted(l: IList): Boolean = ??? </code> **4.11.** Implement ''merge'' which merges two sorted lists: <code scala> def merge(l1: IList, l2: IList): IList = ??? </code> **4.12.** Implement ''mergeSort'' which sorts a list: <code scala> def mergesort(l: IList) IList = ??? </code>