From 292a74d4b221e481dfe278feb2c2bef24df60b0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 28 Mar 2013 14:50:08 +0100 Subject: Scala : move sqrt into sandbox and Scala : add recursion.scala --- Scala/sandbox/main.scala | 7 +++++++ Scala/sandbox/recursion.scala | 29 +++++++++++++++++++++++++++++ Scala/sandbox/sqrt.scala | 28 ++++++++++++++++++++++++++++ Scala/sqrt/sqrt.scala | 27 --------------------------- 4 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 Scala/sandbox/main.scala create mode 100644 Scala/sandbox/recursion.scala create mode 100644 Scala/sandbox/sqrt.scala delete mode 100644 Scala/sqrt/sqrt.scala diff --git a/Scala/sandbox/main.scala b/Scala/sandbox/main.scala new file mode 100644 index 0000000..e770ece --- /dev/null +++ b/Scala/sandbox/main.scala @@ -0,0 +1,7 @@ + +object Main extends App { + + Sqrt.run + Recursion.run + +} diff --git a/Scala/sandbox/recursion.scala b/Scala/sandbox/recursion.scala new file mode 100644 index 0000000..e7c29a5 --- /dev/null +++ b/Scala/sandbox/recursion.scala @@ -0,0 +1,29 @@ + +object Recursion { + + // Tail recursion - tail calls + // the function calls itself (or another one) as last action + // -> the function's stack frame can be reused! + // -> this is as efficient as a loop + def gcd(a: Int, b: Int): Int = + if (b == 0) a else gcd(b, a % b) + + println(gcd(21, 56)) + + // there is still the multiplication to be performed + // -> the stack frame can't be reused + def fact(n: Int): Int = + if (n == 0) 1 else n * fact(n - 1) + + println(fact(4)) + + // tail recursive version of factorial + def factr(n: Int): Int = { + def factIter(acc: Int, n: Int): Int = + if (n == 0) acc else factIter(acc * n, n - 1) + factIter(1, n) + } + + def run = println(factr(4)) + +} diff --git a/Scala/sandbox/sqrt.scala b/Scala/sandbox/sqrt.scala new file mode 100644 index 0000000..2544425 --- /dev/null +++ b/Scala/sandbox/sqrt.scala @@ -0,0 +1,28 @@ + +object Sqrt { + + def abs(x:Double) = if (x < 0) -x else x + + def sqrt(x: Double) = { + + def sqrtIter(guess: Double): Double = + if (isGoodEnough(guess)) guess + else sqrtIter(improve(guess)) + + def isGoodEnough(guess: Double) = + abs(guess * guess - x ) /x < 0.001 + + def improve(guess: Double) = + (guess + x / guess) / 2 + + sqrtIter(1.0) + } + + def run = + println(sqrt(2)) + println(sqrt(0.001)) + println(sqrt(0.1e-20)) + println(sqrt(1.0e20)) + println(sqrt(1.0e50)) + +} diff --git a/Scala/sqrt/sqrt.scala b/Scala/sqrt/sqrt.scala deleted file mode 100644 index b99d61e..0000000 --- a/Scala/sqrt/sqrt.scala +++ /dev/null @@ -1,27 +0,0 @@ - -object Main extends App { - - def abs(x:Double) = if (x < 0) -x else x - - def sqrt(x: Double) = { - - def sqrtIter(guess: Double): Double = - if (isGoodEnough(guess)) guess - else sqrtIter(improve(guess)) - - def isGoodEnough(guess: Double) = - abs(guess * guess - x ) /x < 0.001 - - def improve(guess: Double) = - (guess + x / guess) / 2 - - sqrtIter(1.0) - } - - println(sqrt(2)) - println(sqrt(0.001)) - println(sqrt(0.1e-20)) - println(sqrt(1.0e20)) - println(sqrt(1.0e50)) - -} -- cgit v1.1-2-g2b99