From: Ummon Date: Tue, 17 Mar 2015 08:30:28 +0000 (+0100) Subject: Add the first four assignment. X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=a4c2a3420da0d87af2368e48f1ab61e56caf76cd;p=Scala.git Add the first four assignment. --- a4c2a3420da0d87af2368e48f1ab61e56caf76cd diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7fa6651 --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +.idea/scopes/ +.idea/dictionaries/ +.idea/inspectionProfiles/ +.idea/libraries/ +.idea/modules/ +.idea/.name +.idea/*.xml +!.idea/vcs.xml +!.idea/uiDesigner.xml +/classes +/dist +/out +/SDK/*SDK +/target +/*.bat +*.class +out +build +.DS_Store +*~ +target/ +testdata/debugger \ No newline at end of file diff --git a/Assignment_01/.idea/.name b/Assignment_01/.idea/.name new file mode 100644 index 0000000..7ee4ff1 --- /dev/null +++ b/Assignment_01/.idea/.name @@ -0,0 +1 @@ +Assignment_01 \ No newline at end of file diff --git a/Assignment_01/.idea/compiler.xml b/Assignment_01/.idea/compiler.xml new file mode 100644 index 0000000..a852314 --- /dev/null +++ b/Assignment_01/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + \ No newline at end of file diff --git a/Assignment_01/.idea/copyright/profiles_settings.xml b/Assignment_01/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/Assignment_01/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Assignment_01/.idea/encodings.xml b/Assignment_01/.idea/encodings.xml new file mode 100644 index 0000000..d821048 --- /dev/null +++ b/Assignment_01/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Assignment_01/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_5_jar.xml b/Assignment_01/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_5_jar.xml new file mode 100644 index 0000000..d36848f --- /dev/null +++ b/Assignment_01/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_5_jar.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_01/.idea/misc.xml b/Assignment_01/.idea/misc.xml new file mode 100644 index 0000000..25fba24 --- /dev/null +++ b/Assignment_01/.idea/misc.xml @@ -0,0 +1,115 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_01/.idea/modules.xml b/Assignment_01/.idea/modules.xml new file mode 100644 index 0000000..f12fa01 --- /dev/null +++ b/Assignment_01/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Assignment_01/.idea/modules/assignment_01-build.iml b/Assignment_01/.idea/modules/assignment_01-build.iml new file mode 100644 index 0000000..0cdff2d --- /dev/null +++ b/Assignment_01/.idea/modules/assignment_01-build.iml @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_01/.idea/modules/assignment_01.iml b/Assignment_01/.idea/modules/assignment_01.iml new file mode 100644 index 0000000..315785d --- /dev/null +++ b/Assignment_01/.idea/modules/assignment_01.iml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_01/.idea/sbt.xml b/Assignment_01/.idea/sbt.xml new file mode 100644 index 0000000..beb29cf --- /dev/null +++ b/Assignment_01/.idea/sbt.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/Assignment_01/.idea/scala_compiler.xml b/Assignment_01/.idea/scala_compiler.xml new file mode 100644 index 0000000..5386195 --- /dev/null +++ b/Assignment_01/.idea/scala_compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Assignment_01/.idea/scopes/scope_settings.xml b/Assignment_01/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/Assignment_01/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/Assignment_01/.idea/uiDesigner.xml b/Assignment_01/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/Assignment_01/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_01/.idea/vcs.xml b/Assignment_01/.idea/vcs.xml new file mode 100644 index 0000000..6564d52 --- /dev/null +++ b/Assignment_01/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Assignment_01/.idea/workspace.xml b/Assignment_01/.idea/workspace.xml new file mode 100644 index 0000000..90d53f7 --- /dev/null +++ b/Assignment_01/.idea/workspace.xml @@ -0,0 +1,571 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1424253574086 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + scala-sdk-2.11.5 + + + + + + + + 1.7 + + + + + + + + assignment_01 + + + + + + + + SBT: org.scala-lang:scala-library:2.11.5:jar + + + + + + + + \ No newline at end of file diff --git a/Assignment_01/build.sbt b/Assignment_01/build.sbt new file mode 100644 index 0000000..c1320c2 --- /dev/null +++ b/Assignment_01/build.sbt @@ -0,0 +1,6 @@ +name := "Assignment_01" + +version := "1.0" + +scalaVersion := "2.11.5" + \ No newline at end of file diff --git a/Assignment_01/project/build.properties b/Assignment_01/project/build.properties new file mode 100644 index 0000000..858f009 --- /dev/null +++ b/Assignment_01/project/build.properties @@ -0,0 +1 @@ +sbt.version = 0.13.5 \ No newline at end of file diff --git a/Assignment_01/project/plugins.sbt b/Assignment_01/project/plugins.sbt new file mode 100644 index 0000000..14a6ca1 --- /dev/null +++ b/Assignment_01/project/plugins.sbt @@ -0,0 +1 @@ +logLevel := Level.Warn \ No newline at end of file diff --git a/Assignment_01/src/main/scala/Main.scala b/Assignment_01/src/main/scala/Main.scala new file mode 100644 index 0000000..12e6f09 --- /dev/null +++ b/Assignment_01/src/main/scala/Main.scala @@ -0,0 +1,19 @@ +package org.gburri + +object Main { + val epsilon = 0.0001 + def isGoodEnough(x1: Double, x2: Double) = Math.abs(x1 - x2) < epsilon + def improve(x: Double, a: Double) = x - (x * x - a) / (2 * x) + + def sqrt(a: Double, x: Double = 10): Double = { + val xNext = improve(x, a) + if (isGoodEnough(x, xNext)) xNext + else sqrt(a, xNext) + } + + def main(args: Array[String]): Unit = { + println("Assignment 1 / Question 2 :") + printf(" sqrt(9) : %f\n", sqrt(9)) + printf(" sqrt(612) : %f\n", sqrt(612)) + } +} diff --git a/Assignment_02/.idea/.name b/Assignment_02/.idea/.name new file mode 100644 index 0000000..6078fce --- /dev/null +++ b/Assignment_02/.idea/.name @@ -0,0 +1 @@ +Assignment_02 \ No newline at end of file diff --git a/Assignment_02/.idea/compiler.xml b/Assignment_02/.idea/compiler.xml new file mode 100644 index 0000000..a852314 --- /dev/null +++ b/Assignment_02/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + \ No newline at end of file diff --git a/Assignment_02/.idea/copyright/profiles_settings.xml b/Assignment_02/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/Assignment_02/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Assignment_02/.idea/encodings.xml b/Assignment_02/.idea/encodings.xml new file mode 100644 index 0000000..d821048 --- /dev/null +++ b/Assignment_02/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Assignment_02/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_5_jar.xml b/Assignment_02/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_5_jar.xml new file mode 100644 index 0000000..d36848f --- /dev/null +++ b/Assignment_02/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_5_jar.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_02/.idea/misc.xml b/Assignment_02/.idea/misc.xml new file mode 100644 index 0000000..596024d --- /dev/null +++ b/Assignment_02/.idea/misc.xml @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_02/.idea/modules.xml b/Assignment_02/.idea/modules.xml new file mode 100644 index 0000000..812c9f2 --- /dev/null +++ b/Assignment_02/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Assignment_02/.idea/modules/assignment_02-build.iml b/Assignment_02/.idea/modules/assignment_02-build.iml new file mode 100644 index 0000000..8d8ec5d --- /dev/null +++ b/Assignment_02/.idea/modules/assignment_02-build.iml @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_02/.idea/modules/assignment_02.iml b/Assignment_02/.idea/modules/assignment_02.iml new file mode 100644 index 0000000..0a076ce --- /dev/null +++ b/Assignment_02/.idea/modules/assignment_02.iml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_02/.idea/sbt.xml b/Assignment_02/.idea/sbt.xml new file mode 100644 index 0000000..beb29cf --- /dev/null +++ b/Assignment_02/.idea/sbt.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/Assignment_02/.idea/scala_compiler.xml b/Assignment_02/.idea/scala_compiler.xml new file mode 100644 index 0000000..abf4333 --- /dev/null +++ b/Assignment_02/.idea/scala_compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Assignment_02/.idea/scopes/scope_settings.xml b/Assignment_02/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/Assignment_02/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/Assignment_02/.idea/vcs.xml b/Assignment_02/.idea/vcs.xml new file mode 100644 index 0000000..6564d52 --- /dev/null +++ b/Assignment_02/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Assignment_02/.idea/workspace.xml b/Assignment_02/.idea/workspace.xml new file mode 100644 index 0000000..78f9c29 --- /dev/null +++ b/Assignment_02/.idea/workspace.xml @@ -0,0 +1,542 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1424792062954 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + scala-sdk-2.11.5 + + + + + + + + 1.7 + + + + + + + + assignment_02 + + + + + + + + SBT: org.scala-lang:scala-library:2.11.5:jar + + + + + + + + \ No newline at end of file diff --git a/Assignment_02/build.sbt b/Assignment_02/build.sbt new file mode 100644 index 0000000..ccaf3a3 --- /dev/null +++ b/Assignment_02/build.sbt @@ -0,0 +1,6 @@ +name := "Assignment_02" + +version := "1.0" + +scalaVersion := "2.11.5" + \ No newline at end of file diff --git a/Assignment_02/project/build.properties b/Assignment_02/project/build.properties new file mode 100644 index 0000000..858f009 --- /dev/null +++ b/Assignment_02/project/build.properties @@ -0,0 +1 @@ +sbt.version = 0.13.5 \ No newline at end of file diff --git a/Assignment_02/project/plugins.sbt b/Assignment_02/project/plugins.sbt new file mode 100644 index 0000000..14a6ca1 --- /dev/null +++ b/Assignment_02/project/plugins.sbt @@ -0,0 +1 @@ +logLevel := Level.Warn \ No newline at end of file diff --git a/Assignment_02/src/main/scala/Main.scala b/Assignment_02/src/main/scala/Main.scala new file mode 100644 index 0000000..db72a45 --- /dev/null +++ b/Assignment_02/src/main/scala/Main.scala @@ -0,0 +1,70 @@ +import scala.annotation.tailrec + +object Main { + // Question 1. + // a) + @tailrec + def fac(n: Int, acc: Int = 1) : Int = + if (n == 1) acc else fac(n - 1, acc * n) + + // b) + def fib(n: Int) : Int = { + n match { + case 0 => 0 + case 1 => 1 + case _ => fib(n - 1) + fib(n - 2) + } + } + + /*@tailrec + def fibTail(n: Int, acc: Int = 0) : Int = { + def stage() + n match { + case 0 => 0 + case 1 => 1 + case _ => + } + }*/ + + // Question 2. + def sum(f: Double => Double, a: Int, b: Int): Double = { + def iter(a: Int, acc: Double): Double = + if (a > b) acc + else iter(a + 1, acc + f(a)) + iter(a, 0.0) + } + + // Question 3. + def product(f: Double => Double) (a: Int) (b: Int): Double = { + def iter(a: Int, acc: Double): Double = + if (a > b) acc + else iter(a + 1, acc * f(a)) + iter(a, 1.0) + } + + // c) + def generalization(init: Double) (reducer: (Double, Double) => Double) (f: Double => Double) (a: Int) (b: Int): Double = { + def iter(a: Int, acc: Double): Double = + if (a > b) acc + else iter(a + 1, reducer(acc, f(a))) + iter(a, init) + } + + def main(args: Array[String]) = { + println("Question 1") + println(s"fac(4): ${fac(4)}") + println(s"fib(10): ${fib(4)}") + + println("\nQuestion 2") + val sumOfSquares = sum(x => x * x, 2, 4) + println(s"sum of square from 2 to 4 : $sumOfSquares") + + println("\nQuestion 3") + + def factorial = product (x => x) (1) _ + println(s"factorial(4) : ${factorial(4)}") + + // def sum2 = generalization (0) ((x, y) => x + y) _ _ _ + // def product2 = generalization (1) (_ * _) _ _ _ + } +} diff --git a/Assignment_03/.idea/.name b/Assignment_03/.idea/.name new file mode 100644 index 0000000..b130068 --- /dev/null +++ b/Assignment_03/.idea/.name @@ -0,0 +1 @@ +Assignement_03 \ No newline at end of file diff --git a/Assignment_03/.idea/compiler.xml b/Assignment_03/.idea/compiler.xml new file mode 100644 index 0000000..a852314 --- /dev/null +++ b/Assignment_03/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + \ No newline at end of file diff --git a/Assignment_03/.idea/copyright/profiles_settings.xml b/Assignment_03/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/Assignment_03/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Assignment_03/.idea/encodings.xml b/Assignment_03/.idea/encodings.xml new file mode 100644 index 0000000..d821048 --- /dev/null +++ b/Assignment_03/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Assignment_03/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_6_jar.xml b/Assignment_03/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_6_jar.xml new file mode 100644 index 0000000..204c638 --- /dev/null +++ b/Assignment_03/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_6_jar.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_03/.idea/misc.xml b/Assignment_03/.idea/misc.xml new file mode 100644 index 0000000..7b4247f --- /dev/null +++ b/Assignment_03/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Assignment_03/.idea/modules.xml b/Assignment_03/.idea/modules.xml new file mode 100644 index 0000000..b7129af --- /dev/null +++ b/Assignment_03/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Assignment_03/.idea/modules/assignement_03-build.iml b/Assignment_03/.idea/modules/assignement_03-build.iml new file mode 100644 index 0000000..2b31743 --- /dev/null +++ b/Assignment_03/.idea/modules/assignement_03-build.iml @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_03/.idea/modules/assignment_03.iml b/Assignment_03/.idea/modules/assignment_03.iml new file mode 100644 index 0000000..914bd95 --- /dev/null +++ b/Assignment_03/.idea/modules/assignment_03.iml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_03/.idea/sbt.xml b/Assignment_03/.idea/sbt.xml new file mode 100644 index 0000000..beb29cf --- /dev/null +++ b/Assignment_03/.idea/sbt.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/Assignment_03/.idea/scala_compiler.xml b/Assignment_03/.idea/scala_compiler.xml new file mode 100644 index 0000000..edc915b --- /dev/null +++ b/Assignment_03/.idea/scala_compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Assignment_03/.idea/scopes/scope_settings.xml b/Assignment_03/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/Assignment_03/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/Assignment_03/.idea/vcs.xml b/Assignment_03/.idea/vcs.xml new file mode 100644 index 0000000..6564d52 --- /dev/null +++ b/Assignment_03/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Assignment_03/.idea/workspace.xml b/Assignment_03/.idea/workspace.xml new file mode 100644 index 0000000..2102a92 --- /dev/null +++ b/Assignment_03/.idea/workspace.xml @@ -0,0 +1,522 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + 1425397588150 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + scala-sdk-2.11.5 + + + + + + + + 1.7 + + + + + + + + assignement_03-build + + + + + + + + SBT: org.scala-lang:scala-library:2.11.6:jar + + + + + + + + \ No newline at end of file diff --git a/Assignment_03/build.sbt b/Assignment_03/build.sbt new file mode 100644 index 0000000..06422e1 --- /dev/null +++ b/Assignment_03/build.sbt @@ -0,0 +1,5 @@ +name := "Assignment_03" + +version := "1.0" + +scalaVersion := "2.11.6" diff --git a/Assignment_03/project/build.properties b/Assignment_03/project/build.properties new file mode 100644 index 0000000..858f009 --- /dev/null +++ b/Assignment_03/project/build.properties @@ -0,0 +1 @@ +sbt.version = 0.13.5 \ No newline at end of file diff --git a/Assignment_03/project/plugins.sbt b/Assignment_03/project/plugins.sbt new file mode 100644 index 0000000..14a6ca1 --- /dev/null +++ b/Assignment_03/project/plugins.sbt @@ -0,0 +1 @@ +logLevel := Level.Warn \ No newline at end of file diff --git a/Assignment_03/src/main/scala/Main.scala b/Assignment_03/src/main/scala/Main.scala new file mode 100644 index 0000000..96f8a0c --- /dev/null +++ b/Assignment_03/src/main/scala/Main.scala @@ -0,0 +1,88 @@ +object Main { + abstract class IntSet() { + def add(x: Int): IntSet + def +(x: Int): IntSet = this add x + def contains(x: Int): Boolean + def foreach(f: Int => Unit): Unit + def union(other: IntSet): IntSet + def intersect(other: IntSet): IntSet + def excl(x: Int): IntSet + def -(x: Int): IntSet = this excl x + def isEmpty(): Boolean + } + + class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet { + def add(x: Int): IntSet = { + if (x < elem) new NonEmpty(elem, left add x, right) + else if (x > elem) new NonEmpty(elem, left, right add x) + else this + } + def contains(x: Int): Boolean = { + if (x < elem) left contains x + else if (x > elem) right contains x + else true + } + override def toString(): String = s"($left|$elem|$right)" + def foreach(f: Int => Unit): Unit = { + left foreach f + f(elem) + right foreach f + } + def union(other: IntSet): IntSet = { + val newSet = (left union right) union other + newSet add elem + } + def intersect(other: IntSet): IntSet = { + val l = left intersect other + val r = right intersect other + val newSet = l union r + if (other contains elem) + newSet add elem + else + newSet + } + def excl(x: Int): IntSet = { + if (x < elem) new NonEmpty(elem, left excl x, right) + else if (x > elem) new NonEmpty(elem, left, right excl x) + else left union right + } + def isEmpty(): Boolean = false + } + + object Empty extends IntSet { + def add(x: Int): IntSet = new NonEmpty(x, Empty, Empty) + def contains(x: Int): Boolean = false + override def toString(): String = "-" + def foreach(f: Int => Unit): Unit = () + def union(other: IntSet): IntSet = other + def intersect(other: IntSet): IntSet = Empty + def excl(x: Int): IntSet = Empty + def isEmpty(): Boolean = true + } + + def main(args: Array[String]) = { + println("Question 1") + println("a)") + println(Empty) + println(Empty.add(3)) + println(Empty.add(3).add(2)) + + println("b)") + val s = Empty.add(3).add(2).add(6).add(1) + s foreach println + + println("c)") + s foreach ((v: Int) => print(s"${v + 1}, ")) + println() + + println("Question 2") + val s2 = Empty.add(2).add(3).add(8).add(10).add(1) + println(s"a) $s union $s2 = ${s union s2}") + println(s"b) $s intersect $s2 = ${s intersect s2}") + + println("Question 3") + println(s"a) $s excl 3 = ${s excl 3}") + println(s"b) Empty + 3 + 4 + 12 + 5 = ${Empty + 3 + 4 + 12 + 5}") + println(s"c) $s - 1 - 2 - 3 - 4 = ${s - 1 - 2 - 3 - 4}") + } +} diff --git a/Assignment_04/.idea/.name b/Assignment_04/.idea/.name new file mode 100644 index 0000000..5d93f29 --- /dev/null +++ b/Assignment_04/.idea/.name @@ -0,0 +1 @@ +Assignment_4 \ No newline at end of file diff --git a/Assignment_04/.idea/compiler.xml b/Assignment_04/.idea/compiler.xml new file mode 100644 index 0000000..a852314 --- /dev/null +++ b/Assignment_04/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + \ No newline at end of file diff --git a/Assignment_04/.idea/copyright/profiles_settings.xml b/Assignment_04/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/Assignment_04/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Assignment_04/.idea/encodings.xml b/Assignment_04/.idea/encodings.xml new file mode 100644 index 0000000..d821048 --- /dev/null +++ b/Assignment_04/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Assignment_04/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_6_jar.xml b/Assignment_04/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_6_jar.xml new file mode 100644 index 0000000..204c638 --- /dev/null +++ b/Assignment_04/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_6_jar.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_04/.idea/misc.xml b/Assignment_04/.idea/misc.xml new file mode 100644 index 0000000..3cbc891 --- /dev/null +++ b/Assignment_04/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Assignment_04/.idea/modules.xml b/Assignment_04/.idea/modules.xml new file mode 100644 index 0000000..44752ef --- /dev/null +++ b/Assignment_04/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Assignment_04/.idea/modules/assignment_04-build.iml b/Assignment_04/.idea/modules/assignment_04-build.iml new file mode 100644 index 0000000..cc23cb8 --- /dev/null +++ b/Assignment_04/.idea/modules/assignment_04-build.iml @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_04/.idea/modules/assignment_04.iml b/Assignment_04/.idea/modules/assignment_04.iml new file mode 100644 index 0000000..c836978 --- /dev/null +++ b/Assignment_04/.idea/modules/assignment_04.iml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_04/.idea/sbt.xml b/Assignment_04/.idea/sbt.xml new file mode 100644 index 0000000..beb29cf --- /dev/null +++ b/Assignment_04/.idea/sbt.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/Assignment_04/.idea/scala_compiler.xml b/Assignment_04/.idea/scala_compiler.xml new file mode 100644 index 0000000..918cc1f --- /dev/null +++ b/Assignment_04/.idea/scala_compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Assignment_04/.idea/scopes/scope_settings.xml b/Assignment_04/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/Assignment_04/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/Assignment_04/.idea/vcs.xml b/Assignment_04/.idea/vcs.xml new file mode 100644 index 0000000..6564d52 --- /dev/null +++ b/Assignment_04/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Assignment_04/.idea/workspace.xml b/Assignment_04/.idea/workspace.xml new file mode 100644 index 0000000..07f582a --- /dev/null +++ b/Assignment_04/.idea/workspace.xml @@ -0,0 +1,537 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1426241866133 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_04/build.sbt b/Assignment_04/build.sbt new file mode 100644 index 0000000..c950ab8 --- /dev/null +++ b/Assignment_04/build.sbt @@ -0,0 +1,6 @@ +name := "Assignment_04" + +version := "1.0" + +scalaVersion := "2.11.6" + \ No newline at end of file diff --git a/Assignment_04/project/build.properties b/Assignment_04/project/build.properties new file mode 100644 index 0000000..858f009 --- /dev/null +++ b/Assignment_04/project/build.properties @@ -0,0 +1 @@ +sbt.version = 0.13.5 \ No newline at end of file diff --git a/Assignment_04/project/plugins.sbt b/Assignment_04/project/plugins.sbt new file mode 100644 index 0000000..14a6ca1 --- /dev/null +++ b/Assignment_04/project/plugins.sbt @@ -0,0 +1 @@ +logLevel := Level.Warn \ No newline at end of file diff --git a/Assignment_04/src/main/scala/Main.scala b/Assignment_04/src/main/scala/Main.scala new file mode 100644 index 0000000..a828603 --- /dev/null +++ b/Assignment_04/src/main/scala/Main.scala @@ -0,0 +1,158 @@ +object Main { + + // Question 1: Expressions. + sealed abstract class Expr + case class Number(n: Int) extends Expr + case class Sum(e1: Expr, e2: Expr) extends Expr + case class Product(e1: Expr, e2: Expr) extends Expr + + def eval(e: Expr): Int = e match { + case Number(n) => n + case Sum(e1, e2) => eval(e1) + eval(e2) + case Product(e1, e2) => eval(e1) * eval(e2) + } + + def showFromProduct(e: Expr) : String = e match { + case Sum(e1, e2) => s"(${show(e1)} + ${show(e2)})" + case e => show(e) + } + + def show(e: Expr): String = e match { + case Number(n) => n.toString() + case Sum(e1, e2) => s"${show(e1)} + ${show(e2)}" + case Product(e1, e2) => s"${showFromProduct(e1)} * ${showFromProduct(e2)}" + } + + // Question 2: Trees. + sealed abstract class BinaryTree + case class Leaf(value: Int) extends BinaryTree + case class Node(left: BinaryTree, right: BinaryTree) extends BinaryTree + + def sum(tree: BinaryTree): Int = tree match { + case Leaf(v) => v + case Node(l, r) => sum(l) + sum(r) + } + + def min(a: Int, b: Int): Int = if (a < b) a else b + def minimum(tree: BinaryTree): Int = tree match { + case Leaf(v) => v + case Node(l, r) => min(minimum(l), minimum(r)) + } + + // Question 3: Lists functions. + def last[T](l: List[T]): T = l match { + case Nil => throw new Exception("empty list") + case x :: Nil => x + case x :: xs => last(xs) + } + def init[T](l: List[T]): List[T] = l match { + case Nil => Nil + case x :: Nil => Nil + case x :: xs => x :: init(xs) + } + def concat[T](l1: List[T])(l2: List[T]): List[T] = l1 match { + case Nil => l2 + case x :: xs => x :: concat(xs)(l2) + } + def reverse[T](l: List[T]): List[T] = l match { + case Nil => Nil + case x :: xs => concat(reverse(xs))(List(x)) + } + def take[T](l: List[T])(n: Int): List[T] = l match { + case Nil => Nil + case _ if n == 0 => Nil + case x :: xs => x :: take(xs)(n - 1) + } + def drop[T](l: List[T])(n: Int): List[T] = l match { + case Nil => Nil + case xs if n == 0 => xs + case x :: xs => drop(xs)(n - 1) + } + def apply[T](l: List[T])(n: Int): T = l match { + case Nil => throw new Exception("out of bounds") + case x :: xs => if (n == 0) x else apply(xs)(n - 1) + } + + // Question 4: Predicates on lists. + def any[T](l: List[T])(p: T => Boolean): Boolean = l match { + case Nil => false + case x :: xs => if (p(x)) true else any(xs)(p) + } + def every[T](l: List[T])(p: T => Boolean): Boolean = l match { + case Nil => true + case x :: xs => if (p(x)) every(xs)(p) else false + } + + def main(args: Array[String]) = { + println("Assignment 4") + println("Question 1: Expressions") + + val expr0 = Sum(Product(Number(2), Number(3)), Number(4)) + println(s"Expr0: ${show(expr0)}") + assert(eval(expr0) == 10) + + val expr1 = Product(Number(4), Number(12)) + println(s"Expr1: ${show(expr1)}") + assert(eval(expr1) == 48) + + val expr2 = Product(Sum(Number(2), Number(3)), Number(4)) + println(s"Expr2: ${show(expr2)}") + assert(eval(expr2) == 20) + + val expr3 = Product(Number(2), Sum(Number(3), Number(4))) + println(s"Expr3: ${show(expr3)}") + assert(eval(expr3) == 14) + println() + + + println("Question 2: Tree") + val t = Node(Node(Leaf(4), Leaf(8)), Leaf(2)) + println(s"minimum($t): ${minimum(t)}") + assert(minimum(t) == 2) + val t2 = Node(Leaf(8), Node(Leaf(2), Leaf(1))) + println(s"minimum($t2): ${minimum(t2)}") + assert(minimum(t2) == 1) + println() + + + println("Question 3: Lists functions") + + val l1 = List(2, 3, 1, 4, 2, 9) + val l2 = List(8) + val l3 : List[Int] = Nil + + println(s"last($l1): ${last(l1)}") + println(s"last($l2): ${last(l2)}") + + println(s"init($l1): ${init(l1)}") + println(s"init($l2): ${init(l2)}") + println(s"init($l3): ${init(l3)}") + + println(s"concat(concat($l1)($l2))($l3): ${concat(concat(l1)(l2))(l3)}") + + println(s"reverse($l1): ${reverse(l1)}") + println(s"reverse($l2): ${reverse(l2)}") + println(s"reverse($l3): ${reverse(l3)}") + + println(s"take($l1)(3): ${take(l1)(3)}") + println(s"take($l1)(6): ${take(l1)(6)}") + println(s"take($l1)(10): ${take(l1)(10)}") + + println(s"drop($l1)(3): ${drop(l1)(3)}") + println(s"drop($l1)(6): ${drop(l1)(6)}") + println(s"drop($l1)(10): ${drop(l1)(10)}") + + println(s"apply($l1)(0): ${apply(l1)(0)}") + println(s"apply($l1)(3): ${apply(l1)(3)}") + println(s"apply($l1)(5): ${apply(l1)(5)}") + println() + + + println("Question 4: Predicates on lists") + println(s"any($l1)(_ == 4): ${any(l1)(_ == 4)}") + println(s"any($l1)(_ == 13): ${any(l1)(_ == 13)}") + + println(s"every($l1)(_ < 4): ${every(l1)(_ < 4)}") + println(s"every($l1)(_ < 13): ${every(l1)(_ < 13)}") + } +}