Created
November 19, 2013 00:37
-
-
Save ajjain/7538090 to your computer and use it in GitHub Desktop.
I am following Martin Odersky coursera examples herein
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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