# HG changeset patch # User Christian Urban # Date 1604944152 0 # Node ID 8aaf187d25f0e3eb90523f4d49f6539fa8cdc7a9 # Parent 542d2cf143b0ccd6d5063663e98fb93001c20c1e updated diff -r 542d2cf143b0 -r 8aaf187d25f0 progs/lecture1.scala --- a/progs/lecture1.scala Mon Nov 09 14:36:35 2020 +0000 +++ b/progs/lecture1.scala Mon Nov 09 17:49:12 2020 +0000 @@ -244,19 +244,21 @@ /* boolean operators == equals + != not equals ! not && || and, or */ -def fact2(n: BigInt) : BigInt = - if (n == 0) 1 else n * fact2(n - 1) -fact2(150) - -def fib(n: Int) : Int = +def fib(n: Int) : Int = { if (n == 0) 1 else if (n == 1) 1 else fib(n - 1) + fib(n - 2) +} + +fib(9) + + //gcd - Euclid's algorithm @@ -278,6 +280,14 @@ // "last" line (expression) in a function determines the // result +def average(xs: List[Int]) : Int = { + if (xs.length == 0) 0 + else xs.sum / xs.length +} + +average(List()) + + // For-Comprehensions (not For-Loops) //==================================== diff -r 542d2cf143b0 -r 8aaf187d25f0 slides/slides01.pdf Binary file slides/slides01.pdf has changed diff -r 542d2cf143b0 -r 8aaf187d25f0 slides/slides01.tex --- a/slides/slides01.tex Mon Nov 09 14:36:35 2020 +0000 +++ b/slides/slides01.tex Mon Nov 09 17:49:12 2020 +0000 @@ -606,6 +606,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[c, fragile] +%%\frametitle{General Scheme of} \small \begin{lstlisting}[language=Scala,numbers=none] @@ -620,6 +621,23 @@ \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\begin{frame}[c, fragile] + +\begin{lstlisting}[language=Scala,numbers=none] +def average(xs: List[Int]) : Int = { + val s = xs.sum + val n = xs.length + s / n +} +\end{lstlisting} + + +\end{frame} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\begin{frame}[c]