diff --git a/chapter01/worksheets/solution1.1.1.1_as_object_AR.scala b/chapter01/worksheets/solution1.1.1.1_as_object_AR.scala new file mode 100644 index 0000000..53452e9 --- /dev/null +++ b/chapter01/worksheets/solution1.1.1.1_as_object_AR.scala @@ -0,0 +1,25 @@ +/** author: Andreas Röhler */ + +/** + Factorial of 10 + Find the product of integers from 1 to 10 (the factorial of 10). + */ + +object MyFactorial { + def myFactorial(a: Int): Int = { + (1 to a).foldRight(1)(_ * _) + } + def main(args: Array[String]) { + val expected = 3628800 + val result = this.myFactorial(10) + println("result: %s".format(result)) + assert(result == expected) + } +} + +MyFactorial.main(Array()) + +// scala> :load solution1.1.1.1_as_object_AR.scala +// Loading solution1.1.1.1_as_object_AR.scala... +// defined object MyFactorial +// result: 3628800 diff --git a/chapter01/worksheets/solution1.1.1.1_function_AR.scala b/chapter01/worksheets/solution1.1.1.1_function_AR.scala new file mode 100644 index 0000000..533fb1c --- /dev/null +++ b/chapter01/worksheets/solution1.1.1.1_function_AR.scala @@ -0,0 +1,23 @@ +/** author: Andreas Röhler */ + +/** + Factorial of 10 + Find the product of integers from 1 to 10 (the factorial of 10). + */ + +def myFactorial(a: Int): Int = { + (1 to a).foldRight(1)(_ * _) +} + +val result = myFactorial(10) +println("result: %s".format(result)) + +val expected = 3628800 +assert(result == expected) + +// scala> :load solution1.1.1.1_function_AR.scala +// Loading solution1.1.1.1_function_AR.scala... +// myFactorial: (a: Int)Int +// result: Int = 3628800 +// result: 3628800 +// expected: Int = 3628800