forked from phyous/99-scala-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
P04.scala
43 lines (37 loc) · 954 Bytes
/
P04.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import scala.annotation.tailrec
/**
P04 (*) Find the number of elements of a list.
Example:
scala> length(List(1, 1, 2, 3, 5, 8))
res0: Int = 6
*/
// 1- My Solutions
// 'Regular' recursive solution
def length1[A](input: List[A]): Int = {
input match {
case _ :: tail => 1 + length1(tail)
case _ => 0
}
}
// Let's try with tail recursion
def length2[A](input: List[A]): Int = {
@tailrec
def recurse[A](sum: Int, l: List[A]): Int = {
l match {
case _ :: tail => recurse(sum + 1, tail)
case _ => sum
}
}
recurse(0, input)
}
// 2- "Official" Solution optimization
// My solutions check out with official ones. Could have also used foldLeft
// 3- Tests
val solutions: List[(List[Int] => Int)] = List(length1 _, length2 _)
println("Starting tests...")
solutions.foreach {
f =>
assert(f(List(1, 2, 3)) == 3, "Simple list test (1,2,3)")
assert(f(List()) == 0, "Empty list")
}
println("All tests passed!")