Created
September 23, 2011 19:21
-
-
Save cgopalan/1238214 to your computer and use it in GitHub Desktop.
Tail recurive version of length
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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