From: Ummon Date: Wed, 18 Mar 2015 13:14:03 +0000 (+0100) Subject: Questions 1 and 2 from the fifth assignment. X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=3caf6fced99f7fe7d910dfdc3baa9ade89145848;p=Scala.git Questions 1 and 2 from the fifth assignment. --- diff --git a/Assignment_04/.idea/workspace.xml b/Assignment_04/.idea/workspace.xml index 07f582a..562c9a3 100644 --- a/Assignment_04/.idea/workspace.xml +++ b/Assignment_04/.idea/workspace.xml @@ -31,8 +31,8 @@ - - + + @@ -436,12 +436,14 @@ - + + + - @@ -450,7 +452,6 @@ - @@ -459,7 +460,6 @@ - @@ -468,11 +468,13 @@ - + + + @@ -491,6 +493,9 @@ + + + @@ -527,8 +532,24 @@ - - + + + + + + + + + + + + + + + + + + diff --git a/Assignment_05/.idea/modules/assignment_05-build.iml b/Assignment_05/.idea/modules/assignment_05-build.iml new file mode 100644 index 0000000..bc8e9b8 --- /dev/null +++ b/Assignment_05/.idea/modules/assignment_05-build.iml @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_05/.idea/modules/assignment_05.iml b/Assignment_05/.idea/modules/assignment_05.iml new file mode 100644 index 0000000..1d03f1c --- /dev/null +++ b/Assignment_05/.idea/modules/assignment_05.iml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_05/build.sbt b/Assignment_05/build.sbt new file mode 100644 index 0000000..5a95ac4 --- /dev/null +++ b/Assignment_05/build.sbt @@ -0,0 +1,6 @@ +name := "Assignment_05" + +version := "1.0" + +scalaVersion := "2.11.6" + \ No newline at end of file diff --git a/Assignment_05/project/build.properties b/Assignment_05/project/build.properties new file mode 100644 index 0000000..858f009 --- /dev/null +++ b/Assignment_05/project/build.properties @@ -0,0 +1 @@ +sbt.version = 0.13.5 \ No newline at end of file diff --git a/Assignment_05/project/plugins.sbt b/Assignment_05/project/plugins.sbt new file mode 100644 index 0000000..14a6ca1 --- /dev/null +++ b/Assignment_05/project/plugins.sbt @@ -0,0 +1 @@ +logLevel := Level.Warn \ No newline at end of file diff --git a/Assignment_05/src/main/scala/Main.scala b/Assignment_05/src/main/scala/Main.scala new file mode 100644 index 0000000..a6886bc --- /dev/null +++ b/Assignment_05/src/main/scala/Main.scala @@ -0,0 +1,41 @@ + +object Main { + // Question 1: HOF on lists. + def lengthStrings(l: List[String]) = l map (_ length) + def dup[T](e: T, n: Int) = (1 to n) map (_ => e) + def dot(l1: List[Int], l2: List[Int]) = (l1 zip l2) map { case (a, b) => a * b } + + // Question 2: Folding functions on lists. + def areTrue(l: List[Boolean]) = l reduceLeft (_ && _) + def lString(l: List[String]): Int = l.foldLeft(0)((acc, s) => acc + s.length()) + def longest(l: List[String]): (Int, String) = l.foldLeft(0, ""){ case (curr@(len, longestStr), str) => if (str.length() > len) (str.length(), str) else curr } + def isPresent[T](l: List[T], e: T): Boolean = l.foldLeft(false)((acc, e2) => acc || e == e2) + def flattenList[T](l: List[T]): List[T] = l.foldRight (List.empty[T]) ((e: T, acc: List[T]) => + e match { + case l: List[T] => flattenList(l) ++ acc + case _ => e :: acc + } + ) + + def main(args: Array[String]): Unit = { + println("Question 1") + println(s"a) ${lengthStrings(List("How", "long", "are", "we?"))}") + println(s"b) ${dup("foo", 5)}") + println(s"c) ${dot(List(1, 2, 3), List(2, 4, 3))}") + + println("Question 2") + println("a)") + println(s"${areTrue(List(true, true, false))}") + println(s"${areTrue(List(true, true, true))}") + println("b)") + println(s"${lString(List("Folding", "is", "fun"))}") + println("c)") + println(s"${longest(List("What", "is", "the", "longest?"))}") + println("d)") + println(s"${isPresent(List(1, 2, 3, 4), 5)}") + println(s"${isPresent(List(1, 2, 3, 4), 3)}") + println("e)") + val l = List(List(1, 1), 2, List(3, List(5, 8))); + println(s"flattenList($l): ${flattenList(l)}") + } +}