diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-04-03 22:05:32 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:21 +0100 |
commit | f885b79568a221f9d1cf8edc612f69bec5aad1cc (patch) | |
tree | a41f0e5bded6f5081015836ed5f9be2d323907fd /Scala | |
parent | 747d8bca01440a4274033627cc235274c3af308f (diff) | |
download | coursera-f885b79568a221f9d1cf8edc612f69bec5aad1cc.zip coursera-f885b79568a221f9d1cf8edc612f69bec5aad1cc.tar.gz |
Scala : add sandbox/sum
Diffstat (limited to 'Scala')
-rw-r--r-- | Scala/sandbox/sum.scala | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/Scala/sandbox/sum.scala b/Scala/sandbox/sum.scala new file mode 100644 index 0000000..a09f26b --- /dev/null +++ b/Scala/sandbox/sum.scala @@ -0,0 +1,14 @@ + +object Sum { + + def sum (f: Int => Int, a: Int, b: Int) = { + def loop(a: Int, acc: Int): Int = + if (a > b) acc + else loop(a + 1, f(a) + acc) + loop(a, 0) + } + + def run = + println(sum(x => x * x, 3, 5)) + +} |