List Length

object Length {

  def length[T](ls: List[T]): Int = {
    def loop(xs: List[T], acc: Int): Int =
      xs match {
        case Nil    => acc
        case h :: t => loop(t, acc + 1)
      }
    loop(ls, 0)
  }

  def main(args: Array[String]): Unit = {
    println(length(Nil))
    println(length(List(1, 2, 3, 4)))
    println(length(List(1, 2, "a", 4)))
    println(length(List(1)))
  }

}
0
4
4
1

 

posted on 2016-04-19 21:43  JonkeyGuan  阅读(404)  评论(0编辑  收藏  举报