summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2013-04-03 22:42:41 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2016-11-10 18:03:22 +0100
commit72219111494683f655b1faea242970a5636cd674 (patch)
tree6f4b61b20df1913a67703b7d81a34b3bf000bb6c
parent8cdb4a8d95b79f78f2e4f672a452db5e48eed030 (diff)
downloadcoursera-72219111494683f655b1faea242970a5636cd674.zip
coursera-72219111494683f655b1faea242970a5636cd674.tar.gz
Scala : curry Scala : add factorial
-rw-r--r--Scala/sandbox/curry.scala8
1 files changed, 8 insertions, 0 deletions
diff --git a/Scala/sandbox/curry.scala b/Scala/sandbox/curry.scala
index 5c5577a..1a2e44c 100644
--- a/Scala/sandbox/curry.scala
+++ b/Scala/sandbox/curry.scala
@@ -29,6 +29,12 @@ object Curry {
// add _ if you want to treat it as a partially applied function
def sumCubesShort = sumCurryShort(cube)_
+ // product
+ def product(f: Int => Int)(a: Int, b: Int): Int =
+ if (a > b) 1 else f(a) * product(f)(a + 1, b)
+
+ def fact(n: Int) = product(x => x)(1, n)
+
def run = {
println("Curry")
println(sum(x => x * x, 3, 5))
@@ -37,6 +43,8 @@ object Curry {
println(sumCubes(3, 5))
println(sumCurryShort(cube)(3, 5))
println(sumCubesShort(3, 5))
+ println(product(x => x * x)(3, 4))
+ println(fact(4))
}
}