list manipulation
Some codes to understand lists and recursions from practical scala
def nth[T](n: Int, list: List[T]): T = list match { case x::_ if n == 0 => x case x::xs => nth(n-1, xs) case _ => sys.error("illegal argument") } //println(nth(2, List(1,2,3))) def insertAt[T](list: List[T], element: T, n: Int): List[T] = { list match { case _ if n == 0 => element::list case x::xs => x::insertAt(xs, element, n-1) case Nil => sys.error("illegal argument") } } //println(insertAt(List(1,2,3), 4, 0)) def append[T](list: List[T], element: T): List[T] = { list match { case Nil => element::Nil case x::xs => x::append(xs, element) } } //println(append(List(1,2,3,4), 5)) def length[T](list: List[T]): Int = list match { case Nil => 0 case _::xs => 1 + length(xs) } //println(length(List(1,2,3))); def removeAt[T](list: List[T], n: Int): List[T] = { list match { case x::xs if n == 0 => xs case x::xs => x::removeAt(xs, n - 1) case Nil => sys.error("illegal argument") } } //println(removeAt(List(1,2,3,4), 0)) def concat[A](a: List[A], b: List[A]): List[A] = { a match { case Nil => b case x::xs => x::concat(xs, b) } } //println(concat(List(1,2), List(5,6)))