From: Ummon Date: Mon, 27 Apr 2015 20:10:30 +0000 (+0200) Subject: Assignment 7 X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=e49a206d3f0e5101ac29fb00760c85f637c7fbc8;p=Scala.git Assignment 7 --- diff --git a/.gitignore b/.gitignore index 7fa6651..86f2a25 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,7 @@ build .DS_Store *~ target/ -testdata/debugger \ No newline at end of file +testdata/debugger +*.userprefs +bin/ +obj/ diff --git a/Assignment_02/.idea/misc.xml b/Assignment_02/.idea/misc.xml index 596024d..db4e48e 100644 --- a/Assignment_02/.idea/misc.xml +++ b/Assignment_02/.idea/misc.xml @@ -1,33 +1,5 @@ - - - - - - - - - @@ -39,74 +11,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Assignment_02/.idea/workspace.xml b/Assignment_02/.idea/workspace.xml index 78f9c29..05d401d 100644 --- a/Assignment_02/.idea/workspace.xml +++ b/Assignment_02/.idea/workspace.xml @@ -19,9 +19,6 @@ - - - @@ -31,7 +28,7 @@ - + @@ -47,13 +44,16 @@ + true + + + - @@ -82,6 +82,7 @@ + @@ -171,6 +172,7 @@ + @@ -195,60 +197,13 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -265,29 +220,6 @@ - - - - - - - - - - - - - - - - - - - - true - - - + + + + + + + + + + + + + + + + + + + @@ -337,6 +318,43 @@ + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + @@ -345,12 +363,6 @@ - - - - - + + + + + - - - + + + + - + + + - - - + + + - - - - - - - - - - - - + + + + + + @@ -443,11 +456,6 @@ - - - @@ -459,7 +467,15 @@ - + + + + + + + + + @@ -467,7 +483,7 @@ - + diff --git a/Assignment_06/Assignment_06.ipr b/Assignment_06/Assignment_06.ipr index 215f3af..a4fccaa 100644 --- a/Assignment_06/Assignment_06.ipr +++ b/Assignment_06/Assignment_06.ipr @@ -30,7 +30,11 @@ diff --git a/Assignment_06/Assignment_06.iws b/Assignment_06/Assignment_06.iws index 2b43238..3bfbb72 100644 --- a/Assignment_06/Assignment_06.iws +++ b/Assignment_06/Assignment_06.iws @@ -3,10 +3,7 @@ - - - @@ -34,11 +31,9 @@ - - - - - + + + @@ -106,8 +101,8 @@ @@ -132,6 +127,8 @@ + + @@ -284,8 +281,6 @@ - - @@ -622,41 +617,42 @@ - + + - - + - - - + - + + + @@ -679,12 +675,6 @@ + + + + + + + + + + + + + + + + + + + + + + + + - - - + @@ -714,9 +726,7 @@ - - - + @@ -724,9 +734,7 @@ - - - + @@ -734,9 +742,7 @@ - - - + @@ -744,9 +750,7 @@ - - - + @@ -754,9 +758,7 @@ - - - + @@ -800,9 +802,7 @@ - - - + @@ -846,9 +846,7 @@ - - - + @@ -892,9 +890,7 @@ - - - + @@ -938,9 +934,7 @@ - - - + @@ -984,9 +978,7 @@ - - - + @@ -1029,7 +1021,6 @@ - @@ -1045,7 +1036,6 @@ - @@ -1075,11 +1065,9 @@ - - - - - + + + diff --git a/Assignment_06/src/main/scala/Anagrams.scala b/Assignment_06/src/main/scala/Anagrams.scala index a69b872..b2abf26 100644 --- a/Assignment_06/src/main/scala/Anagrams.scala +++ b/Assignment_06/src/main/scala/Anagrams.scala @@ -68,8 +68,8 @@ object Anagrams { val combs = for ( i <- 1 to occurrences.length; - l <- occurrences combinations i; - l2 <- combinationsOnNumber(l) + l <- occurrences combinations i; // Combination on letters. + l2 <- combinationsOnNumber(l) // Combination on numbers ) yield l2 List() :: (combs toList) diff --git a/Assignment_07-FSharp/Assignment_07-FSharp.sln b/Assignment_07-FSharp/Assignment_07-FSharp.sln new file mode 100644 index 0000000..d557847 --- /dev/null +++ b/Assignment_07-FSharp/Assignment_07-FSharp.sln @@ -0,0 +1,17 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{f2a71f9b-5d33-465a-a702-920d77279786}") = "Assignment_07-FSharp", "Assignment_07-FSharp\Assignment_07-FSharp.fsproj", "{E7DC4EE2-C349-4535-85C8-4EF519523148}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E7DC4EE2-C349-4535-85C8-4EF519523148}.Debug|x86.ActiveCfg = Debug|x86 + {E7DC4EE2-C349-4535-85C8-4EF519523148}.Debug|x86.Build.0 = Debug|x86 + {E7DC4EE2-C349-4535-85C8-4EF519523148}.Release|x86.ActiveCfg = Release|x86 + {E7DC4EE2-C349-4535-85C8-4EF519523148}.Release|x86.Build.0 = Release|x86 + EndGlobalSection +EndGlobal diff --git a/Assignment_07-FSharp/Assignment_07-FSharp/AssemblyInfo.fs b/Assignment_07-FSharp/Assignment_07-FSharp/AssemblyInfo.fs new file mode 100644 index 0000000..101c687 --- /dev/null +++ b/Assignment_07-FSharp/Assignment_07-FSharp/AssemblyInfo.fs @@ -0,0 +1,21 @@ +module Assignment_07_FSharp.AssemblyInfo +open System.Reflection +open System.Runtime.CompilerServices + +[] +[] +[] +[] +[] +[] +[] + +// The assembly version has the format {Major}.{Minor}.{Build}.{Revision} + +[] + +//[] +//[] + +() + diff --git a/Assignment_07-FSharp/Assignment_07-FSharp/Assignment_07-FSharp.fsproj b/Assignment_07-FSharp/Assignment_07-FSharp/Assignment_07-FSharp.fsproj new file mode 100644 index 0000000..366c56d --- /dev/null +++ b/Assignment_07-FSharp/Assignment_07-FSharp/Assignment_07-FSharp.fsproj @@ -0,0 +1,44 @@ + + + + Debug + x86 + {E7DC4EE2-C349-4535-85C8-4EF519523148} + Exe + Assignment_07FSharp + Assignment_07-FSharp + + + true + full + false + bin\Debug + DEBUG + prompt + true + false + x86 + + + false + none + true + bin\Release + prompt + x86 + true + true + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_07-FSharp/Assignment_07-FSharp/Program.fs b/Assignment_07-FSharp/Assignment_07-FSharp/Program.fs new file mode 100644 index 0000000..601ecdb --- /dev/null +++ b/Assignment_07-FSharp/Assignment_07-FSharp/Program.fs @@ -0,0 +1,62 @@ +open System + +// Question 1. +type Stack<'a> = + | Elem of 'a * Stack<'a> + | Empty + member this.Push a = Elem (a, this) + member this.Top = + match this with + | Elem (a, _) -> a + | Empty -> failwith "Empty stack!" + member this.Pop = + match this with + | Elem (_, rest) -> rest + | Empty -> failwith "Empty stack!" + override this.ToString () = + match this with + | Elem (e, rest) -> e.ToString() + "," + rest.ToString() + | Empty -> "Empty" + +type Foo () = class end +type Bar () = inherit Foo () + +let question1 () = + printfn "Question 1" + let a = (((Empty.Push "hello").Push "world").Push "it's fun").Pop + printfn "a: %O" a + assert (a.ToString () = "world,hello,Empty") + + let b = ((Empty.Push 1).Push 3) + printfn "b.Top: %O" b.Top + assert (b.Top = 3) + + let c : Stack = (Empty.Push <| Bar ()).Push <| Bar () + printfn "%A" <| + match c.Top with + | :? Bar -> true + | _ -> false + + let d : Stack<#Foo> = (Empty.Push <| Bar ()).Push <| Bar () + printfn "%A" <| + match d.Top with + | :? Bar -> true + | _ -> false + +// Question 4. +let question4 () = + let rec primesSieve sequence = + seq { + let h = Seq.head sequence + yield h + yield! primesSieve <| Seq.filter (fun m -> m % h <> 0) sequence + } + let primes = primesSieve <| (Seq.initInfinite id |> Seq.skip 2) + printfn "first primes: %A" <| (Seq.take 100 primes |> Seq.toList) + +[] +let main argv = + question1 () + question4 () + 0 + diff --git a/Assignment_07/.idea/modules/assignment_07-build.iml b/Assignment_07/.idea/modules/assignment_07-build.iml new file mode 100644 index 0000000..58934ae --- /dev/null +++ b/Assignment_07/.idea/modules/assignment_07-build.iml @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_07/.idea/modules/assignment_07.iml b/Assignment_07/.idea/modules/assignment_07.iml new file mode 100644 index 0000000..a6ac3ce --- /dev/null +++ b/Assignment_07/.idea/modules/assignment_07.iml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_07/Assignment_07.ipr b/Assignment_07/Assignment_07.ipr new file mode 100644 index 0000000..a37f6eb --- /dev/null +++ b/Assignment_07/Assignment_07.ipr @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment_07/Assignment_07.iws b/Assignment_07/Assignment_07.iws new file mode 100644 index 0000000..3ef6b47 --- /dev/null +++ b/Assignment_07/Assignment_07.iws @@ -0,0 +1,553 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1430127821037 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + scala-sdk-2.11.5 + + + + + + + + 1.8 + + + + + + + + assignment_07 + + + + + + + + SBT: org.scala-lang:scala-library:2.11.6:jar + + + + + + + + \ No newline at end of file diff --git a/Assignment_07/build.sbt b/Assignment_07/build.sbt new file mode 100644 index 0000000..47c35fe --- /dev/null +++ b/Assignment_07/build.sbt @@ -0,0 +1,6 @@ +name := "Assignment_07" + +version := "1.0" + +scalaVersion := "2.11.6" + \ No newline at end of file diff --git a/Assignment_07/project/build.properties b/Assignment_07/project/build.properties new file mode 100644 index 0000000..d638b4f --- /dev/null +++ b/Assignment_07/project/build.properties @@ -0,0 +1 @@ +sbt.version = 0.13.8 \ No newline at end of file diff --git a/Assignment_07/project/plugins.sbt b/Assignment_07/project/plugins.sbt new file mode 100644 index 0000000..14a6ca1 --- /dev/null +++ b/Assignment_07/project/plugins.sbt @@ -0,0 +1 @@ +logLevel := Level.Warn \ No newline at end of file diff --git a/Assignment_07/src/main/scala/Main.scala b/Assignment_07/src/main/scala/Main.scala new file mode 100644 index 0000000..ebd6d30 --- /dev/null +++ b/Assignment_07/src/main/scala/Main.scala @@ -0,0 +1,95 @@ +object Main { + + // Question 1. + trait Stack[+A] { + def push[B >: A](elem: B): Stack[B] + def top: A + def pop: Stack[A] + } + + case class EmptyStack[A]() extends Stack[A] { + def push[B >: A](elem: B): Stack[B] = ElemStack(elem, this) + def top: A = throw new Exception + def pop: Stack[A] = throw new Exception + } + + case class ElemStack[A](top: A, rest: Stack[A]) extends Stack[A] { + def push[B >: A](elem: B): Stack[B] = ElemStack(elem, this) + def pop: Stack[A] = rest + override def toString = s"${top.toString()},${rest.toString()}" + } + + def question1 = { + println("Question 1") + // Construction, pop and toString. + val a = EmptyStack().push("hello").push("world").push("it's fun").pop + println(s"a: ${a}") + assert(a.toString() == "world,hello,EmptyStack()") + + // Getting top. + val b = EmptyStack().push(1).push(3) + println(s"b.top: ${b.top}") + assert(b.top == 3) + + // Variance checks. + class Foo + class Bar extends Foo + val c: Stack[Bar] = EmptyStack().push(new Bar()).push(new Bar()) + assert(c.top.isInstanceOf[Bar]) + assert(c.top.isInstanceOf[Foo]) + + // Variance check 2. + val d: Stack[Foo] = EmptyStack().push(new Bar()).push(new Bar()) + assert(d.top.isInstanceOf[Foo]) + } + + // Question 2. + abstract sealed class Temperature { + val temp: Double + } + + case class Celsius(v: Double) extends Temperature { + val temp = v + override def toString = s"$v° C" + } + + case class Kelvin(v: Double) extends Temperature { + val temp = v + override def toString = s"$v° K" + } + + object Temperature { + implicit def toCelsius(temp: Kelvin): Celsius = new Celsius(temp.v + 273.15) + implicit def toCelsius(temp: Int): Celsius = new Celsius(temp) + implicit def toKelvin(temp: Celsius): Kelvin = new Kelvin(temp.v - 273.15) + implicit def toKelvin(temp: Int): Kelvin = new Kelvin(temp) + } + + def question2 = { + println("Question 2") + val a: Celsius = 30 + val b: Kelvin = 30 + val c: Kelvin = Celsius(10) + val d: Celsius = c + val e: Temperature = d + println(a) + println(b) + println(e) + } + + // Question 3. + def addStream(s1: Stream[Int], s2: Stream[Int]): Stream[Int] = + (s1.head + s2.head) #:: addStream(s1.tail, s2.tail) + + lazy val fib: Stream[Int] = 0 #:: 1 #:: addStream(fib, fib.tail) + + def question3 = { + println(fib.take(30).toList) + } + + def main(args: Array[String]): Unit = { + question1 + question2 + question3 + } +}