scala/lib.scala
changeset 221 18905d086cbb
parent 193 317a2532c567
--- a/scala/lib.scala	Fri Mar 08 11:31:04 2013 +0000
+++ b/scala/lib.scala	Sun Mar 10 07:10:50 2013 +0000
@@ -1,15 +1,23 @@
 package object lib {
 
 //some list functions
-def tl[A](xs: List[A]) : List[A] = xs match {
-  case Nil => Nil
-  case x::xs => xs
-}
+//slow version
+//def tl[A](xs: List[A]) : List[A] = xs match {
+//  case Nil => Nil
+//  case x::xs => xs
+//}
+
+def tl[A](xs: List[A]) : List[A] = 
+  try { xs.tail } catch { case _:UnsupportedOperationException => Nil }
 
 def hd[A](xs: List[A]) : A = xs.head
 
+//slow version
+//def nth_of[A](xs: List[A], n: Int) = 
+//  if (n <= xs.length) Some(xs(n)) else None 
+
 def nth_of[A](xs: List[A], n: Int) = 
-  if (n <= xs.length) Some(xs(n)) else None 
+  try { Some(xs(n)) } catch { case _:IndexOutOfBoundsException => None }
 
 //some map functions
 def dget(m: Map[Int, Int], n: Int) = m.getOrElse(n, 0)