Skip to content

Instantly share code, notes, and snippets.

@cgopalan
Created September 23, 2011 19:21
Show Gist options
  • Save cgopalan/1238214 to your computer and use it in GitHub Desktop.
Save cgopalan/1238214 to your computer and use it in GitHub Desktop.
Tail recurive version of length
object TailRecursiveListLength {
def trlength(l: List[Int], counter: Int): Int = l match {
case Nil => counter
case x :: xs => trlength(xs, counter + 1)
}
def main(args: Array[String]) {
println("Length of list () is: " + trlength(List(), 0))
println("Length of list (1,6,2,5,8,93) is: " + trlength(List(1,6,2,5,8,93), 0))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment