# HG changeset patch # User Christian Urban # Date 1544169474 0 # Node ID b8cdaf51ffef8476dc33c965197ea8d03e985ba9 # Parent 0c752ac51cfa4ba5c7a57ad8933bce5201d8c546 updated diff -r 0c752ac51cfa -r b8cdaf51ffef progs/lecture5.scala --- a/progs/lecture5.scala Fri Dec 07 03:15:46 2018 +0000 +++ b/progs/lecture5.scala Fri Dec 07 07:57:54 2018 +0000 @@ -6,9 +6,9 @@ // Laziness with style //===================== -// The concept of lazy evaluation doesn’t really exist in -// non-functional languages, but it is pretty easy to grasp. -// Consider first +// The concept of lazy evaluation doesn’t really +// exist in non-functional languages, but it is +// pretty easy to grasp. Consider first def square(x: Int) = x * x @@ -16,8 +16,9 @@ // this is called strict evaluation -// pretty expensive operation +// say we have a pretty expensive operation def peop(n: BigInt): Boolean = peop(n + 1) + val a = "foo" val b = "foo" @@ -43,25 +44,24 @@ def generatePrimes (s: Stream[Int]): Stream[Int] = s.head #:: generatePrimes(s.tail.filter(_ % s.head != 0)) -val primes: Stream[Int] = generatePrimes(Stream.from(2)) +val primes = generatePrimes(Stream.from(2)) // the first 10 primes primes.take(10).toList -//primes.filter(_ > 100).take(2000).toList - time_needed(1, primes.filter(_ > 100).take(3000).toList) time_needed(1, primes.filter(_ > 100).take(3000).toList) +// a stream of successive numbers +Stream.from(2) -Stream.from(2) Stream.from(2).take(10) Stream.from(2).take(10).print Stream.from(10).take(10).print Stream.from(2).take(10).force -// itterative version of the Fibonacci numbers +// iterative version of the Fibonacci numbers def fibIter(a: BigInt, b: BigInt): Stream[BigInt] = a #:: fibIter(b, a + b) @@ -88,12 +88,6 @@ case class STAR(r: Rexp) extends Rexp // star r* - -// writing (ab)* in the format above is -// tedious -val r0 = STAR(SEQ(CHAR('a'), CHAR('b'))) - - // some convenience for typing in regular expressions import scala.language.implicitConversions import scala.language.reflectiveCalls @@ -107,17 +101,12 @@ charlist2rexp(s.toList) -val r1 = STAR("ab") -val r2 = STAR(ALT("ab", "baa baa black sheep")) -val r3 = STAR(SEQ("ab", ALT("a", "b"))) - implicit def RexpOps (r: Rexp) = new { def | (s: Rexp) = ALT(r, s) def % = STAR(r) def ~ (s: Rexp) = SEQ(r, s) } - implicit def stringOps (s: String) = new { def | (r: Rexp) = ALT(s, r) def | (r: String) = ALT(s, r) @@ -144,7 +133,8 @@ // task: enumerate exhaustively regular expression // starting from small ones towards bigger ones. -// 1st idea: enumerate them up to a level +// 1st idea: enumerate them all in a Set +// up to a level def enuml(l: Int, s: String) : Set[Rexp] = l match { case 0 => Set(ZERO, ONE) ++ s.map(CHAR).toSet @@ -156,9 +146,11 @@ (for (r1 <- rs) yield STAR(r1)) } +enuml(1, "a") enuml(1, "a").size enuml(2, "a").size -enuml(3, "a").size // out of heap space +enuml(3, "a").size +enuml(4, "a").size // out of heap space def enum(rs: Stream[Rexp]) : Stream[Rexp] = @@ -168,7 +160,7 @@ enum(ZERO #:: ONE #:: "ab".toStream.map(CHAR)).take(200).force -enum(ZERO #:: ONE #:: "ab".toStream.map(CHAR)).take(200000).force +enum(ZERO #:: ONE #:: "ab".toStream.map(CHAR)).take(5000000) val is = @@ -325,7 +317,7 @@ -// The End ... Almost Christimas +// The End ... Almost Christmas //=============================== // I hope you had fun! @@ -336,7 +328,8 @@ // reason not to. // I did it, but this is actually not a good reason: -// generating new labels +// generating new labels: + var counter = -1 def Fresh(x: String) = { diff -r 0c752ac51cfa -r b8cdaf51ffef slides/slides05.tex --- a/slides/slides05.tex Fri Dec 07 03:15:46 2018 +0000 +++ b/slides/slides05.tex Fri Dec 07 07:57:54 2018 +0000 @@ -99,6 +99,29 @@ \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\begin{frame}[c] +\frametitle{Marks for CW7 (Part 1 + 2)} + +Raw marks (234 submissions): + +\begin{itemize} +\item 6\%: \hspace{4mm}192 students +\item 5\%: \hspace{4mm}16 +\item 4\%: \hspace{4mm}7 +\item 3\%: \hspace{4mm}2 +\item 2\%: \hspace{4mm}6 +\item 1\%: \hspace{4mm}1 +\item 0\%: \hspace{4mm}9 +\end{itemize} +\end{frame} + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[c,fragile] \small