summaryrefslogtreecommitdiffstats
path: root/Scala/example/src/main/scala
diff options
context:
space:
mode:
Diffstat (limited to 'Scala/example/src/main/scala')
-rw-r--r--Scala/example/src/main/scala/common/package.scala40
-rw-r--r--Scala/example/src/main/scala/example/Lists.scala42
2 files changed, 82 insertions, 0 deletions
diff --git a/Scala/example/src/main/scala/common/package.scala b/Scala/example/src/main/scala/common/package.scala
new file mode 100644
index 0000000..f1c74c3
--- /dev/null
+++ b/Scala/example/src/main/scala/common/package.scala
@@ -0,0 +1,40 @@
+import java.io.File
+
+package object common {
+
+ /** An alias for the `Nothing` type.
+ * Denotes that the type should be filled in.
+ */
+ type ??? = Nothing
+
+ /** An alias for the `Any` type.
+ * Denotes that the type should be filled in.
+ */
+ type *** = Any
+
+
+ /**
+ * Get a child of a file. For example,
+ *
+ * subFile(homeDir, "b", "c")
+ *
+ * corresponds to ~/b/c
+ */
+ def subFile(file: File, children: String*) = {
+ children.foldLeft(file)((file, child) => new File(file, child))
+ }
+
+ /**
+ * Get a resource from the `src/main/resources` directory. Eclipse does not copy
+ * resources to the output directory, then the class loader cannot find them.
+ */
+ def resourceAsStreamFromSrc(resourcePath: List[String]): Option[java.io.InputStream] = {
+ val classesDir = new File(getClass.getResource(".").toURI)
+ val projectDir = classesDir.getParentFile.getParentFile.getParentFile.getParentFile
+ val resourceFile = subFile(projectDir, ("src" :: "main" :: "resources" :: resourcePath): _*)
+ if (resourceFile.exists)
+ Some(new java.io.FileInputStream(resourceFile))
+ else
+ None
+ }
+}
diff --git a/Scala/example/src/main/scala/example/Lists.scala b/Scala/example/src/main/scala/example/Lists.scala
new file mode 100644
index 0000000..4e5feee
--- /dev/null
+++ b/Scala/example/src/main/scala/example/Lists.scala
@@ -0,0 +1,42 @@
+package example
+
+import common._
+
+object Lists {
+ /**
+ * This method computes the sum of all elements in the list xs. There are
+ * multiple techniques that can be used for implementing this method, and
+ * you will learn during the class.
+ *
+ * For this example assignment you can use the following methods in class
+ * `List`:
+ *
+ * - `xs.isEmpty: Boolean` returns `true` if the list `xs` is empty
+ * - `xs.head: Int` returns the head element of the list `xs`. If the list
+ * is empty an exception is thrown
+ * - `xs.tail: List[Int]` returns the tail of the list `xs`, i.e. the the
+ * list `xs` without its `head` element
+ *
+ * ''Hint:'' instead of writing a `for` or `while` loop, think of a recursive
+ * solution.
+ *
+ * @param xs A list of natural numbers
+ * @return The sum of all elements in `xs`
+ */
+ def sum(xs: List[Int]): Int = ???
+
+ /**
+ * This method returns the largest element in a list of integers. If the
+ * list `xs` is empty it throws a `java.util.NoSuchElementException`.
+ *
+ * You can use the same methods of the class `List` as mentioned above.
+ *
+ * ''Hint:'' Again, think of a recursive solution instead of using looping
+ * constructs. You might need to define an auxiliary method.
+ *
+ * @param xs A list of natural numbers
+ * @return The largest element in `xs`
+ * @throws java.util.NoSuchElementException if `xs` is an empty list
+ */
+ def max(xs: List[Int]): Int = ???
+}