mirror of
https://github.com/MartinThoma/LaTeX-examples.git
synced 2025-04-26 06:48:04 +02:00
Cut (Prolog) hinzugefügt; weitere Scala-Beispiele hinzugefügt
This commit is contained in:
parent
6829f6cf97
commit
20994f6cc3
7 changed files with 101 additions and 1 deletions
Binary file not shown.
|
@ -56,6 +56,22 @@ die beiden Terme bereits identisch sind.
|
|||
|
||||
Weitere Informationen: \url{http://stackoverflow.com/a/8220315/562769}
|
||||
|
||||
\subsection{! (Cut)}\xindex{"! (Cut, Prolog)}
|
||||
Das \texttt{!} wird in Prolog als \textit{cut} bezeichnet. Ein Cut verhindert
|
||||
Backtracking nach dem cut.
|
||||
|
||||
Die Klauseln eines Prädikates werden von Prolog von links nach rechts evaluiert.
|
||||
Prolog bindet einen Wert an eine Variable in der linkesten Klausel. Wenn diese
|
||||
Klausel als \texttt{true} ausgewertet wird, dann versucht Prolog die nächste
|
||||
Klausel auszuwerten. Falls nicht, wird eine neuer Wert an die momentan
|
||||
betrachtete Klausel gebunden.
|
||||
|
||||
Da Klauseln über logische UND verbunden sind, führt eine nicht erfüllbare
|
||||
Klausel dazu, dass das gesamte Prädikat als \texttt{false} evaluiert wird.
|
||||
|
||||
Der cut ist ein Gate: Sind die Klauseln vor dem cut ein mal wahr, werden die
|
||||
Werte festgelegt.
|
||||
|
||||
\subsection{Arithmetik}
|
||||
Die Auswertung artihmetischer Ausdrücke muss in Prolog explizit durch \texttt{is}
|
||||
durchgeführt werden:
|
||||
|
@ -219,7 +235,7 @@ bei Prof. Dr. Snelting:
|
|||
|
||||
\inputminted[numbersep=5pt, tabsize=4]{prolog}{scripts/prolog/regex.pl}
|
||||
|
||||
\subsection{Two Bases}
|
||||
\subsection{Coffeetime 01: Two Bases}
|
||||
|
||||
\inputminted[numbersep=5pt, tabsize=4]{prolog}{scripts/prolog/01-two-bases.prolog}
|
||||
|
||||
|
|
|
@ -160,6 +160,28 @@ man immer wieder Quadriert:
|
|||
|
||||
\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=power-futures.scala]{scala}{scripts/scala/power-futures.scala}
|
||||
|
||||
\subsection{Coffeetime 01: Two Bases}
|
||||
Find three digits $X$, $Y$ and $Z$ such that $XYZ$ in base 10 is equal to $ZYX$
|
||||
in base 9.
|
||||
|
||||
\inputminted[linenos, numbersep=5pt, tabsize=2, frame=lines, label=01-TwoBases.scala]{scala}{scripts/scala/01-TwoBases.scala}
|
||||
|
||||
\subsection{Coffeetime 04: Exactly a third}
|
||||
Arrange the numerals 1-9 into a single fraction that equals exactly
|
||||
$\frac{1}{3}$.
|
||||
|
||||
No other math symbols wanted; just concatenation some digits for the
|
||||
numerator, and some to make a denominator.
|
||||
|
||||
\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=04-ExactlyAThird.scala]{scala}{scripts/scala/04-ExactlyAThird.scala}
|
||||
|
||||
\subsection{Coffeetime 05: Three Dice}\xindex{yield (Scala)@\texttt{yield} (Scala)}
|
||||
I roll three dice, and multiply the three numbers together.
|
||||
|
||||
What is the probability the total will be odd?
|
||||
|
||||
\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=05-ThreeDice.scala]{scala}{scripts/scala/05-ThreeDice.scala}
|
||||
|
||||
\section{Weitere Informationen}
|
||||
\begin{itemize}
|
||||
\item \url{http://www.scala-lang.org/api}
|
||||
|
|
|
@ -6,6 +6,9 @@ geschrieben. Dazu wurden
|
|||
die Folien von Prof.~Dr.~Snelting und Jun.-Prof.~Dr.~Hummel benutzt, die Struktur
|
||||
sowie einige Beispiele, Definitionen und Sätze übernommen.
|
||||
|
||||
Es wurden einige Aufgaben von \url{http://www.datagenetics.com/blog/june22014/index.html}
|
||||
genommen um Beispielcode für einfache Probleme zu schreiben.
|
||||
|
||||
Das Ziel dieses Skriptes ist vor allem
|
||||
in der Klausur als Nachschlagewerk zu dienen; es soll jedoch auch
|
||||
vorher schon für die Vorbereitung genutzt werden können und nach
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
object TwoBases {
|
||||
def test(x: Int, y: Int, z: Int) =
|
||||
(100*x + 10*y + z == math.pow(9,2)*z + 9*y + z)
|
||||
|
||||
def main(args: Array[String]) {
|
||||
for(x <- 0 to 9; y <- 0 to 9; z <- 0 to 9) {
|
||||
if(test(x, y, z)){
|
||||
println("%d%d%d".format(x, y, z));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
import scala.math.pow
|
||||
|
||||
object ExactlyAthrid {
|
||||
def main(arg: Array[String]) {
|
||||
val digits = List(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
for (c <- digits.combinations(4)) {
|
||||
for(d <- c.permutations) {
|
||||
// Get the numerator
|
||||
var numerator = 0;
|
||||
for((digit, place) <- d.zipWithIndex) {
|
||||
numerator += digit
|
||||
* pow(10, place).toInt;
|
||||
}
|
||||
|
||||
// Get the denominator
|
||||
var denominator = 3 * numerator;
|
||||
|
||||
// Check if all digits appear
|
||||
// exactly once
|
||||
var cdigits = numerator.toString
|
||||
+ denominator.toString;
|
||||
var cdigits_list = cdigits.toCharArray.
|
||||
distinct;
|
||||
|
||||
// Print solution
|
||||
if (cdigits_list.length == 9 &&
|
||||
!cdigits_list.contains('0')){
|
||||
println("%d / %d = 1/3".
|
||||
format(numerator, denominator));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
object ThreeDice {
|
||||
def main(arg: Array[String]) {
|
||||
val dice_results = List(1, 2, 3, 4, 5, 6);
|
||||
var outcomes = for(a <- dice_results;
|
||||
b <- dice_results;
|
||||
c <- dice_results)
|
||||
yield a*b*c;
|
||||
println("%d / %d".
|
||||
format(
|
||||
outcomes.filter(x => x % 2 == 1).length,
|
||||
outcomes.length));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue