# HG changeset patch # User Christian Urban # Date 1765459410 0 # Node ID 2e3945ff7b66cceea6b14e83dcf1efdc42dcd1ca # Parent 28f78d9081d7bd8fa81eccc3ad73114e69099d06 updated diff -r 28f78d9081d7 -r 2e3945ff7b66 progs/lecture4.scala --- a/progs/lecture4.scala Fri Dec 05 10:20:00 2025 +0000 +++ b/progs/lecture4.scala Thu Dec 11 13:23:30 2025 +0000 @@ -37,7 +37,7 @@ length(List(1, 2, 3, 4)) -length[String](List(1, 2, 3, 4)) +length[Int](List(1, 2, 3, 4)) def map[A, B](lst: List[A], f: A => B): List[B] = lst match { @@ -247,9 +247,13 @@ // Tail recursion //================ +@tailrec def fact(n: BigInt): BigInt = if (n == 0) 1 else n * fact(n - 1) +fact(3) -> fact(2) + + fact(10) fact(1000) @@ -260,7 +264,7 @@ if (n == 0) acc else factT(n - 1, n * acc) -factT(1000,1) +factT(10,1) println(factT(100000, 1)) @@ -303,14 +307,17 @@ case _ :: tail => 1 + length(tail) } -length(List.fill(100000)(1)) +val long_lst = List.fill(100000)(1) + +length(long_lst) +long_lst.length def lengthT[A](xs: List[A], acc : Int = 0) : Int = xs match { case Nil => acc case _ :: tail => lengthT(tail, 1 + acc) } -lengthT(List.fill(100000)(1)) + @@ -628,15 +635,15 @@ // Regular expressions - the power of DSLs in Scala //================================================== -abstract class Rexp -case object ZERO extends Rexp // nothing -case object ONE extends Rexp // the empty string -case class CHAR(c: Char) extends Rexp // a character c -case class ALT(r1: Rexp, r2: Rexp) extends Rexp // alternative r1 + r2 -case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence r1 . r2 -case class STAR(r: Rexp) extends Rexp // star r* - - +enum Rexp { + case ZERO // nothing + case ONE // the empty string + case CHAR(c: Char) // a character c + case ALT(r1: Rexp, r2: Rexp) // alternative r1 + r2 + case SEQ(r1: Rexp, r2: Rexp) // sequence r1 . r2 + case STAR(r: Rexp) // star r* +} +import Rexp._ // writing (ab)* in the format above is // tedious @@ -676,7 +683,15 @@ val number = sign ~ digit ~ digit.% +extension (n: Int) { + def ++ = n + 1 +} +def --(n: Int) = n - 1 + +val n = 3 +val m = (n.++) +val k = --(n) // In mandelbrot.scala I used complex (imaginary) numbers // and implemented the usual arithmetic operations for complex diff -r 28f78d9081d7 -r 2e3945ff7b66 progs/lecture5.scala --- a/progs/lecture5.scala Fri Dec 05 10:20:00 2025 +0000 +++ b/progs/lecture5.scala Thu Dec 11 13:23:30 2025 +0000 @@ -1,12 +1,36 @@ // Scala Lecture 5 //================= -def foo(n: Int) = ??? +// (Immutable) OOP + + +import scala.util._ // Try,... +import io.Source // fromURL + +val my_url = "https://urbanchr.github.io/" + + + + +Source.fromURL(my_url)(using "ISO-8859-1").mkString + +Try(Source.fromURL(my_url)(using "ISO-8859-1").mkString).toOption -fop(10) +Try(Source.fromURL(my_url)(using "ISO-8859-1").mkString).getOrElse("") + + +// the same for files + +Try(Some(Source.fromFile("test.txt")(using "ISO-8859-1").mkString)).getOrElse(None) -List.fill(1)(100) -// (Immutable) OOP +// how to implement a function for reading +// (lines) from files... +// +def get_contents(name: String) : List[String] = + Try(Source.fromURL(name)(using "ISO-8859-1").getLines().toList).getOrElse(Nil) + +get_contents(my_url) + // Object Oriented Programming in Scala // ===================================== diff -r 28f78d9081d7 -r 2e3945ff7b66 slides/slides04.pdf Binary file slides/slides04.pdf has changed diff -r 28f78d9081d7 -r 2e3945ff7b66 slides/slides04.tex --- a/slides/slides04.tex Fri Dec 05 10:20:00 2025 +0000 +++ b/slides/slides04.tex Thu Dec 11 13:23:30 2025 +0000 @@ -178,7 +178,7 @@ %%Office: & N\liningnums{7.07} (North Wing, Bush House)\bigskip\\ Slides \& Code: & KEATS\bigskip\\ - Office Hour: & Thursdays 13:00 -- 14:00 (send email first)\\ + Office Hour: & Fridays 11:30 -- 12:30 (send email first)\\ Location: & N7.07 (North Wing, Bush House)\bigskip\\ %Pollev: & \texttt{\alert{https://pollev.com/cfltutoratki576}}\\ \\ @@ -516,6 +516,19 @@ \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\begin{frame} +\end{frame} +\begin{frame} +\end{frame} +\begin{frame} +\end{frame} +\begin{frame} +\end{frame} +\begin{frame} +\end{frame} +\begin{frame} +\end{frame} + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\begin{frame}[c,fragile] diff -r 28f78d9081d7 -r 2e3945ff7b66 slides/slides05.pdf Binary file slides/slides05.pdf has changed diff -r 28f78d9081d7 -r 2e3945ff7b66 slides/slides05.tex --- a/slides/slides05.tex Fri Dec 05 10:20:00 2025 +0000 +++ b/slides/slides05.tex Thu Dec 11 13:23:30 2025 +0000 @@ -161,11 +161,12 @@ \begin{center} \begin{tabular}{ll} Email: & christian.urban at kcl.ac.uk\\ + & I will try to stay on top of my inbox during Christmas\medskip\\ %Office: & N\liningnums{7.07} (North Wing, Bush House)\bigskip\\ Slides \& Code: & KEATS\bigskip\\ - Office Hour: & Thursdays 13:00 -- 14:00\\ - Location: & N7.07 (North Wing, Bush House)\bigskip\\ + %%Office Hour: & Thursdays 13:00 -- 14:00\\ + %%%Location: & N7.07 (North Wing, Bush House)\bigskip\\ %Pollev: & \texttt{\alert{https://pollev.com/cfltutoratki576}}\\ \\ \end{tabular}