From 03c383fb35f7430cea7d1e742c919cad5c699c70 Mon Sep 17 00:00:00 2001
From: LordBaryhobal <lordbaryhobal@gmail.com>
Date: Tue, 1 Apr 2025 14:14:52 +0200
Subject: [PATCH] added midterm

---
 src/MidTerm1/Ex1.sc |  7 +++++++
 src/MidTerm1/Ex2.sc | 34 ++++++++++++++++++++++++++++++++++
 src/MidTerm1/Ex3.sc | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 78 insertions(+)
 create mode 100644 src/MidTerm1/Ex1.sc
 create mode 100644 src/MidTerm1/Ex2.sc
 create mode 100644 src/MidTerm1/Ex3.sc

diff --git a/src/MidTerm1/Ex1.sc b/src/MidTerm1/Ex1.sc
new file mode 100644
index 0000000..68318a3
--- /dev/null
+++ b/src/MidTerm1/Ex1.sc
@@ -0,0 +1,7 @@
+val pi: List[Long] = List(3, 1, 4, 1, 5, 9, 2)
+val slice: List[Int] = List(4, 1, 5)
+
+pi.indexOfSlice(slice)
+
+//List("Hello", "world", "Scala").foldRight(0)((a, b) => a + b.length)
+List("Hello", "world", "Scala").foldLeft(0)((a, b) => a + b.length)
\ No newline at end of file
diff --git a/src/MidTerm1/Ex2.sc b/src/MidTerm1/Ex2.sc
new file mode 100644
index 0000000..c4b8232
--- /dev/null
+++ b/src/MidTerm1/Ex2.sc
@@ -0,0 +1,34 @@
+import scala.annotation.tailrec
+
+@tailrec
+def foldLeft[A, B](list: List[A])(init: B)(f: (B, A) => B): B = {
+  list match {
+    case Nil => init
+    case head::tail => foldLeft(tail)(f(init, head))(f)
+  }
+}
+
+@tailrec
+def dropWhile[T](list: List[T])(predicate: T => Boolean): List[T] = {
+  list match {
+    case head::tail if predicate(head) => dropWhile(tail)(predicate)
+    case _ => list
+  }
+}
+
+def predicates[T](list: List[T])(preds: List[T => Boolean]): List[T] = {
+  list.foldRight(List.empty[T])((e: T, l: List[T]) => {
+    //if (preds.foldLeft(true)((a, b) => a && b(e))) e::l
+    if (preds.forall(b => b(e))) e::l
+    else l
+  })
+}
+
+def fixedPoint(f: Int => Int): Int => Int = {
+  def func(x: Int): Int = {
+    val y: Int = f(x)
+    if (x == y) y
+    else f(y)
+  }
+  func
+}
diff --git a/src/MidTerm1/Ex3.sc b/src/MidTerm1/Ex3.sc
new file mode 100644
index 0000000..fedf7f0
--- /dev/null
+++ b/src/MidTerm1/Ex3.sc
@@ -0,0 +1,37 @@
+import scala.annotation.tailrec
+
+abstract class Text {
+  def isEmpty: Boolean = {
+    this match {
+      case Chars(cs) => cs.isEmpty
+      case Concat(t1, t2) => t1.isEmpty && t2.isEmpty
+    }
+  }
+  def head: Char = {
+    this match {
+      case Chars(cs) => cs.head
+      case Concat(t1, t2) => if (t1.isEmpty) t2.head else t1.head
+    }
+  }
+  def tail: Text = {
+    this match {
+      case Chars(cs) => Chars(cs.tail)
+      case Concat(t1, t2) => if (t2.isEmpty) t1.tail else Concat(t1, t2.tail)
+    }
+  }
+  def map(f: Char => Char): Text = {
+    this match {
+      case Chars(cs) => Chars(cs.map(f))
+      case Concat(t1, t2) => Concat(t1.map(f), t2.map(f))
+    }
+  }
+}
+case class Chars(cs: List[Char]) extends Text
+case class Concat(t1: Text, t2: Text) extends Text
+
+@tailrec
+def equals(t1: Text, t2: Text): Boolean = {
+  if (t1.isEmpty) t2.isEmpty
+  else if (t2.isEmpty) false
+  else (t1.head == t2.head) && equals(t1.tail, t2.tail)
+}