Compare commits

...

5 Commits

Author SHA1 Message Date
e58c399bad
added assignment 7 ex 5 2025-04-16 10:26:44 +02:00
95fdbf7abf
added assignment 7 ex 4 2025-04-16 07:53:28 +02:00
17c1e4170d
added assignment 7 ex 3 2025-04-16 07:41:25 +02:00
d4878015e3
added assignment 7 ex 2 2025-04-15 15:59:25 +02:00
f693d69366
added assignment 7 ex 1 2025-04-15 15:59:16 +02:00
5 changed files with 101 additions and 0 deletions

35
src/Assignment7/Ex1.sc Normal file
View File

@ -0,0 +1,35 @@
trait Stack[+A] {
def push[B >: A](elem: B) : Stack[B] = ElemStack(elem, this)
def top: A
def pop: Stack[A]
}
case class EmptyStack[+A]() extends Stack[A] {
override def top: A = throw new IndexOutOfBoundsException("Stack is empty")
override def pop: Stack[A] = this
}
case class ElemStack[+A](elmt: A, base: Stack[A]) extends Stack[A] {
override def top: A = elmt
override def pop: Stack[A] = base
override def toString: String = elmt.toString + "," + base.toString
}
// Construction, pop and toString
val a = EmptyStack().push("hello").push("world").push("it's fun").pop
assert(a.toString() == "world,hello,EmptyStack()")
// Getting top
val b = EmptyStack().push(1).push(3)
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] == true)
assert(c.top.isInstanceOf[Foo] == true)
// Variance check 2
val d: Stack[Foo] = EmptyStack().push(new Bar()).push(new Bar())
assert(d.top.isInstanceOf[Foo])

9
src/Assignment7/Ex2.sc Normal file
View File

@ -0,0 +1,9 @@
def intsFrom(n: Int): LazyList[Int] = {
n #:: intsFrom(n + 1)
}
def primeNumbers(list: LazyList[Int]): LazyList[Int] = {
list.head #:: primeNumbers(list.filter(n => n % list.head != 0))
}
val ints: LazyList[Int] = intsFrom(2)
primeNumbers(ints).take(10).toList

12
src/Assignment7/Ex3.sc Normal file
View File

@ -0,0 +1,12 @@
def addStream(s1: LazyList[Int], s2: LazyList[Int]): LazyList[Int] = {
s1 zip s2 map (p => p._1 + p._2)
}
def fibonacci(): LazyList[Int] = {
0 #:: addStream(
1 #:: fibonacci(),
fibonacci()
)
}
fibonacci().take(10).toList

19
src/Assignment7/Ex4.sc Normal file
View File

@ -0,0 +1,19 @@
def THRESHOLD: Double = 0.0001
def sqr(x: Double): Double = x * x
def sqrt_stream(value: Double): LazyList[Double] = {
def helper(target: Double, approx: Double): LazyList[Double] = {
approx #:: helper(target, approx - (sqr(approx) - target) / (2 * approx))
}
helper(value, value)
}
sqrt_stream(2).take(10).toList
def threshold(list: LazyList[Double], thresh: Double) = {
list.zip(list.drop(1)).filter(p => math.abs(p._2 - p._1) < thresh).head._2
}
threshold(sqrt_stream(2), 1e-15)

26
src/Assignment7/Ex5.sc Normal file
View File

@ -0,0 +1,26 @@
/*
1
1 1
2 1
1 2 1 1
1 1 1 2 2 1
3 1 2 2 1 1
Next :
1 3 1 1 2 2 2 1
*/
def nextLine(current: List[Int]) : List[Int] = {
current.foldRight(List.empty[(Int, Int)])((x, acc) => {
(x, acc) match {
case (a, p :: rest) if a == p._2 => (p._1 + 1, p._2) :: rest
case _ => (1, x) :: acc
}
}).flatten(p => List(p._1, p._2))
}
def makeSequence(start: List[Int]): LazyList[List[Int]] = {
start #:: makeSequence(nextLine(start))
}
lazy val sequence: LazyList[List[Int]] = makeSequence(List(1))
sequence.take(7).toList