Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.learner.s99
- /**
- * (*) Find the number of elements of a list.
- * Example:
- * scala> length(List(1, 1, 2, 3, 5, 8))
- * res0: Int = 6
- */
- object P04 {
- def length(l: List[Any]): Int = length(l, 0)
- private def length(l: List[Any], lengthSoFar: Int): Int = l match {
- case List() => lengthSoFar
- case head::tail => length(tail, lengthSoFar + 1)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement