Skip to content

Instantly share code, notes, and snippets.

@ajjain
Created November 19, 2013 00:37
Show Gist options
  • Save ajjain/7538090 to your computer and use it in GitHub Desktop.
Save ajjain/7538090 to your computer and use it in GitHub Desktop.
I am following Martin Odersky coursera examples herein
package learn.scala
object Exercise {
println("Welcome to the Exercises") //> Welcome to the Exercises
// EXERCISE 1:
// Write a product function that calculate product of values of a function for the points
// on a given interval
def prod(f: Int=>Int, a: Int, b: Int): Int = {
if(a > b) 1 else{
f(a) * prod(f, a+1, b)
}
} //> prod: (f: Int => Int, a: Int, b: Int)Int
prod(x=>x, 1, 5) //> res0: Int = 120
// EXERCISE 2:
// Write Factorial in terms of product
def factorial(a: Int): Int = {
prod(x=>x, 1, a)
} //> factorial: (a: Int)Int
factorial(10) //> res1: Int = 3628800
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment