Learning Scala at the moment, learning about pattern matching in particular, I wrote this simple factorial method in Scala:
def factorial(n : Int) : Int = {
if(n <= 1)
1
else
n * factorial(n -1)
}
Then I thought to myself, I could just use pattern matching and wrote this:
def fact(n : Int) : Int = n match {
case 0 => 1
case n => n * fact(n -1)
}
But I thought the point of pattern matching was sorting on data, why would I ever need to use this on something like factorial?
much appreciated.