Monday, September 12, 2011

Scala By Example Chapter 5 Solutions - Exercise 5.2.3

Exercise 5.2.3 Write factorial in terms of product.

object Factorial {
def product(f: Int => Int)(a: Int, b:Int): Int = {
if (a > b) 1 else f(a) * product(f)(a + 1, b)
}
def factorial(n: Int): Int = {
product(x => x)(1, n)
}
def main(args: Array[String]) {
println("Factorial of 6 is: " + factorial(6))
}
}

No comments: