I am getting an error while running my Scala Code on IntelliJ (using Scala 2.11.8):
package week4
/**
* Created by BigB on 15/08/17.
*/
trait List[T] {
def isEmpty: Boolean
def head: T
def tail: List[T]
}
class Cons[T](val head: T, val tail: List[T]) extends List[T] {
override def isEmpty: Boolean = false
}
class Nil[T] extends List[T] {
override def isEmpty: Boolean = true
override def head: T = throw new NoSuchElementException("Nil.head")
override def tail: List[T] =throw new NoSuchElementException("Nil.tail")
}
My Scala Worksheet has:
import week4._
object nth {
def nth[T](n: T, l: List[T]): T = {
if (l.isEmpty) throw new IndexOutOfBoundsException
else if (n==0) l.head
else nth(n-1, l.tail)
}
val l1 = new Cons(1, new Cons(2, new Cons(2, new Nil)))
nth(2, l1)
}
Errors:
Error:(9, 20) not found: type Cons lazy val l1 = new Cons(1, new Cons(2, new Cons(2, new Nil))) ^
Error:(6, 16) value - is not a member of type parameter T else nth(n-1, l.tail) ^