object p01 {
def last(l: List[Int]) = l.reverse.head //> last: (l: List[Int])Int
last(List(1, 1, 2, 3, 5, 8)) //> res0: Int = 8
}
Em chua hieu noi dung de lam :D
object p03 {
def nth(n: Int, list: List[Int]):Int=
if(list.size == 0 || list.size<=n) throw new Exception
else list(n) //> nth: (n: Int, list: List[Int])Int
nth(2, List(1, 1, 2, 3, 5, 8)) //> res0: Int = 2
//nth(2, List())
//nth(10,List(1))
}
object p04 {
def length[A](list: List[A]): Int = list.length //> length: [A](list: List[A])Int
length(List(1, 1, 2, 3, 5, 8)) //> res0: Int = 6
length(List()) //> res1: Int = 0
length(Nil) //> res2: Int = 0
}
object p05 {
def reverse(list: List[Any]):List[Any]= list.reverse
//> reverse: (list: List[Any])List[Any]
reverse(List(1, 1, 2, 3, 5, 8)) //> res0: List[Any] = List(8, 5, 3, 2, 1, 1)
reverse(List("a")) //> res1: List[Any] = List(a)
reverse(Nil) //> res2: List[Any] = List()
}
object p06 {
def isPalindrome(list: List[Any]): Boolean = list.reverse == list
//> isPalindrome: (list: List[Any])Boolean
isPalindrome(List(1, 2, 3, 2, 1)) //> res0: Boolean = true
isPalindrome(List(1, 2, 3, 1)) //> res1: Boolean = false
isPalindrome(List(1)) //> res2: Boolean = true
isPalindrome(List(1, 1)) //> res3: Boolean = true
isPalindrome(List()) //> res4: Boolean = true
}