# HG changeset patch # User Christian Urban # Date 1573000605 0 # Node ID 21b52310bd8bccd11498c08baf581d635706bbba # Parent dea46bdfd648f836cb9d56c8f122d96173b3e18a updated diff -r dea46bdfd648 -r 21b52310bd8b cws/cw01.pdf Binary file cws/cw01.pdf has changed diff -r dea46bdfd648 -r 21b52310bd8b cws/cw01.tex --- a/cws/cw01.tex Tue Nov 05 00:41:02 2019 +0000 +++ b/cws/cw01.tex Wed Nov 06 00:36:45 2019 +0000 @@ -245,9 +245,9 @@ (first line), 2011 (second line) and 2012 (third line) you obtain: \begin{verbatim} - List(List(Some(311.349976), Some(20.544939)), - List(Some(300.222351), Some(31.638695)), - List(Some(330.555054), Some(39.478039)))) + List(List(Some(312.204773), Some(26.782711)), + List(Some(301.0466), Some(41.244694)), + List(Some(331.462585), Some(51.464207)))) \end{verbatim}\hfill[1 Mark] diff -r dea46bdfd648 -r 21b52310bd8b pics/blow.png Binary file pics/blow.png has changed diff -r dea46bdfd648 -r 21b52310bd8b progs/lecture1.scala --- a/progs/lecture1.scala Tue Nov 05 00:41:02 2019 +0000 +++ b/progs/lecture1.scala Wed Nov 06 00:36:45 2019 +0000 @@ -5,10 +5,13 @@ // (their names should be lower case) //==================================== +//var z = 9 +//z = 10 val x = 42 val y = 3 + 4 val z = x / y +val x = 70 // (you cannot reassign values: z = 9 will give an error) @@ -41,6 +44,7 @@ // ranges 1 to 10 (1 to 10).toList +(1 to 10).toList.toString (1 until 10).toList @@ -69,9 +73,9 @@ List(1,2,3) == List(3,1,2) -// this applies to "concrete" values...pretty much everything; -// but for example you cannot compare functions (later), -// and also not Arrays +// this applies to "concrete" values...pretty much +// everything; but for example you cannot compare +// functions (later), and also not arrays Array(1) == Array(1) @@ -97,6 +101,7 @@ println(lst.mkString(", ")) // some methods take more than one argument + println(lst.mkString("{", ",", "}")) // (in this case .mkString can take no, one, @@ -166,7 +171,7 @@ // type errors -math.sqrt("64") +math.sqrt("64".toDouble) // produces // @@ -193,6 +198,15 @@ // Function Definitions //====================== +def foo(s: String) : String = { + val tmp = s ++ s ++ s + val res = s ++ s + res +} + + +foo("test") + def incr(x: Int) : Int = x + 1 def double(x: Int) : Int = x + x def square(x: Int) : Int = x * x @@ -205,15 +219,14 @@ str(3) -// The general scheme for a function: you have to give a type -// to each argument and a return type of the function +// The general scheme for a function: you have to give a +// type to each argument and a return type of the function // // def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = { -// body +// // } -// // BTW: no returns!! // "last" line (expression) in a function determines the // result @@ -250,7 +263,6 @@ fact2(150) - def fib(n: Int) : Int = if (n == 0) 1 else if (n == 1) 1 else fib(n - 1) + fib(n - 2) @@ -276,13 +288,13 @@ // For-Comprehensions (not For-Loops) //==================================== - +(1 to 10).toList for (n <- (1 to 10).toList) yield { square(n) + 1 } for (n <- (1 to 10).toList; - m <- (1 to 10).toList) yield m * n + m <- (1 to 10).toList) yield (m, n) // you can assign the result of a for-comprehension @@ -296,15 +308,20 @@ // the list/set/... can also be constructed in any // other way + for (n <- Set(10,12,4,5,7,8,10)) yield n * n +for (n <- (1 to 10)) yield { + n * n +} + +if (1 == 2) "a" else "b" // with if-predicates / filters for (n <- (1 to 3).toList; m <- (1 to 3).toList; - if (n + m) % 2 == 0; - if (n * m) < 2) yield (n, m) + if (n + m) % 2 == 0) yield (n, m) for (n <- (1 to 3).toList; m <- (1 to 3).toList; @@ -330,13 +347,15 @@ // Functions producing multiple outputs //====================================== -def get_ascii(c: Char) : (Char, Int) = (c, c.toInt) +def get_ascii(c: Char) : (Char, Int) = + (c, c.toInt) get_ascii('a') // .maxBy, sortBy with pairs -def get_length(s: String) : (String, Int) = (s, s.length) +def get_length(s: String) : (String, Int) = + (s, s.length) val lst = List("zero", "one", "two", "three", "four", "ten") val strs = for (s <- lst) yield get_length(s) @@ -356,7 +375,8 @@ for (n <- (1 to 10)) println(n) - +(1 to 10).toList +(1 until 10).toList // BTW: a roundabout way of printing out a list, say val lst = ('a' to 'm').toList diff -r dea46bdfd648 -r 21b52310bd8b slides/slides01.pdf Binary file slides/slides01.pdf has changed diff -r dea46bdfd648 -r 21b52310bd8b slides/slides01.tex --- a/slides/slides01.tex Tue Nov 05 00:41:02 2019 +0000 +++ b/slides/slides01.tex Wed Nov 06 00:36:45 2019 +0000 @@ -197,8 +197,8 @@ \multicolumn{2}{r@{}}{\footnotesize$^*$ source: Stackoverflow Developer Survey, 2019} \end{tabular} \end{center} -\RIGHTarrow{2}{2.3}{5.45} -\DOWNarrow{2}{10.1}{3.6} +\RIGHTarrow{2}{2.4}{6.4} +\DOWNarrow{2}{10.1}{4.4} } @@ -518,8 +518,7 @@ \item Preliminary Part 8: 4\% (27 November) \item Preliminary Part 9: 4\% (5 December) \end{itemize}\medskip - \item Core Part: 35\% (15 January 2020) - + \item Core Part: 35\% (15 January 2020)\bigskip \end{itemize} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -766,7 +765,21 @@ \end{center} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -\end{document} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\begin{frame}[t] + +\begin{center} +\includegraphics[scale=0.4]{../pics/blow.png} +\end{center} + +\begin{textblock}{14}(2,12.4) +\Large\bf{}Mind-Blowing\\ Programming Languages: C/C++ +\end{textblock} +\end{frame} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + \end{document} diff -r dea46bdfd648 -r 21b52310bd8b testing1/collatz.scala --- a/testing1/collatz.scala Tue Nov 05 00:41:02 2019 +0000 +++ b/testing1/collatz.scala Wed Nov 06 00:36:45 2019 +0000 @@ -7,11 +7,12 @@ object CW6a { -def collatz(n: Long): Long = +/* + * def collatz(n: Long): Long = if (n == 1) 0 else if (n % 2 == 0) 1 + collatz(n / 2) else 1 + collatz(3 * n + 1) - +*/ def collatz_max(bnd: Long): (Long, Long) = { val all = for (i <- (1L to bnd)) yield (collatz(i), i) @@ -30,6 +31,20 @@ */ + + +def collatz(n: Long) : Long = { + if (n == 1) { + 1L + } else { + if (n % 2 == 0) { + collatz(n/2) + 1 + } else { + collatz((n*3)+1) + 1 + } + } +} + } diff -r dea46bdfd648 -r 21b52310bd8b testing1/drumb_test.sh --- a/testing1/drumb_test.sh Tue Nov 05 00:41:02 2019 +0000 +++ b/testing1/drumb_test.sh Wed Nov 06 00:36:45 2019 +0000 @@ -92,10 +92,10 @@ if [ $tsts -eq 0 ] then - echo -e " get_prices(List(\"GOOG\", \"AAPL\"), 2010 to 2012) ==" >> $out - echo -e " List(List(Some(311.349976), Some(20.544939))," >> $out - echo -e " List(Some(300.222351), Some(31.638695))," >> $out - echo -e " List(Some(330.555054), Some(39.478039)))" >> $out + echo " get_prices(List(\"GOOG\", \"AAPL\"), 2010 to 2012) ==" >> $out + echo " List(List(Some(312.204773), Some(26.782711))," >> $out + echo " List(Some(301.0466), Some(41.244694))," >> $out + echo " List(Some(331.462585), Some(51.464207)))" >> $out if (scala_assert "drumb.scala" "drumb_test3.scala") then diff -r dea46bdfd648 -r 21b52310bd8b testing1/drumb_test3.scala --- a/testing1/drumb_test3.scala Tue Nov 05 00:41:02 2019 +0000 +++ b/testing1/drumb_test3.scala Wed Nov 06 00:36:45 2019 +0000 @@ -1,9 +1,10 @@ import CW6b._ -assert(get_prices(List("GOOG", "AAPL"), 2010 to 2012) == - List(List(Some(311.349976), Some(20.544939)), - List(Some(300.222351), Some(31.638695)), - List(Some(330.555054), Some(39.478039)))) - +assert(get_prices(List("GOOG", "AAPL"), 2010 to 2012) == + List(List(Some(312.204773), Some(26.782711)), + List(Some(301.0466), Some(41.244694)), + List(Some(331.462585), Some(51.464207)))) + +