From 72219111494683f655b1faea242970a5636cd674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 3 Apr 2013 22:42:41 +0200 Subject: =?UTF-8?q?Scala=20:=C2=A0curry=20Scala=20:=20add=20factorial?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Scala/sandbox/curry.scala | 8 ++++++++ 1 file changed, 8 insertions(+) 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)) } } -- cgit v1.1-2-g2b99