Scala:模式匹配
据说模式匹配很强大,可惜还没学会...来个裴波拉切数列吧...
def main(args: Array[String]) = {
for (i <- 0 to 10)
print(fib(i) + " ")
}
def fib(n: Int): Int = n match {
case 0 => 0
case 1 => 1
case _ => fib(n - 2) + fib(n - 1)
}
0 1 1 2 3 5 8 13 21 34 55