浏览代码

added another scala example

Martin Thoma 11 年之前
父节点
当前提交
accdf1507c

二进制
documents/Programmierparadigmen/Programmierparadigmen.pdf


+ 22 - 0
documents/Programmierparadigmen/Scala.tex

@@ -89,6 +89,21 @@ Listen können erstellt und durchgegangen werden:
     \caption{Logische Operatoren in Scala}\xindex{Logische Operatoren!Scala}
 \end{table}
 
+\section{Datenstrukturen}
+\subsection{Listen}
+
+\begin{itemize}
+    \item Erstellt man mit \verb+var myList = List();+
+    \item Zugriff auf das \verb+i+-te Element mit \verb+myList(i)+
+\end{itemize}
+
+\subsection{Tupel}
+
+\begin{itemize}
+    \item Erstellt man mit \verb+var myTuple = (el1, el2, el3)+
+    \item Zugriff auf das \verb+i+-te Element mit \verb+myTuple._i+
+\end{itemize}
+
 \section{Companion Object}\xindex{Companion Object}
 Ein Companion Object ist ein Objekt mit dem Namen einer Klasse oder eines Traits.
 Im Gegensatz zu anderen Objekten / Traits hat das Companion Object zugriff auf
@@ -127,6 +142,13 @@ die Yahoo-Server, parst das XML und extrahiert die Stadt sowie die Temperatur:
 
 \inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=weather.scala]{scala}{scripts/scala/weather.scala}
 
+\subsection{High Product}
+Das folgende Skript berechnet folgendes: Wenn man aus den Ziffern 0 - 9 zwei
+Zahlen $a$, $b$ bilden darf, welche Zahlen muss man dann bilden um das größte Produkt
+$a \cdot b$ zu erhalten?
+
+\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=main.scala]{scala}{scripts/scala/HighProduct.scala}
+
 \section{Weitere Informationen}
 \begin{itemize}
     \item \url{http://www.scala-lang.org/api}

+ 37 - 0
documents/Programmierparadigmen/scripts/scala/HighProduct.scala

@@ -0,0 +1,37 @@
+object HighProduct {
+    def main(arg: Array[String]) {
+        var max = List(BigInt(0), BigInt(0),
+                       BigInt(0));
+        val digits = List(0, 1, 2, 3, 4, 5, 6,
+                          7, 8, 9);
+        for (c <- digits.combinations(4)) {
+            // Sort the digits so that the
+            // highest number gets built
+            val a = c.sorted(Ordering[Int]);
+            val b = (digits filterNot a.contains).
+                    sorted(Ordering[Int]);
+            // calculate number a
+            var anum = BigInt(0);
+            for ((digit, place) <- a.zipWithIndex) {
+                anum += digit *
+                        scala.math.pow(10, place).
+                            toInt;
+            }
+            // calculate number b
+            var bnum = BigInt(0);
+            for ((digit, place) <- b.zipWithIndex) {
+                bnum += digit *
+                        scala.math.pow(10, place).
+                            toInt;
+            }
+
+            // calculate number a
+            if (anum * bnum > max(0)) {
+                max = List(anum * bnum, anum, bnum);
+            }
+        }
+        println("%d • %d = %d".format(max(1),
+                                      max(2),
+                                      max(0)));
+    }
+}