"It turns out that `struct` is one of Julia’s keywords, and they cannot be used as variable names."
"It turns out that `struct` is one of Julia’s keywords, and they cannot be used as variable names. \n",
"\n",
"- For more Julia's keywords, see [https://docs.julialang.org/en/v1/base/base/#Keywords](https://docs.julialang.org/en/v1/base/base/#Keywords). \n",
"- For more details on `Variable`, see [https://docs.julialang.org/en/v1/manual/variables/](https://docs.julialang.org/en/v1/manual/variables/)."
]
},
{
...
...
@@ -294,6 +297,14 @@
"\"hello! \"^5"
]
},
{
"cell_type": "markdown",
"id": "362f9d04-eff9-4594-aa22-a9c8d601fb1e",
"metadata": {},
"source": [
"For more details: [https://docs.julialang.org/en/v1/manual/strings/](https://docs.julialang.org/en/v1/manual/strings/)."
]
},
{
"cell_type": "markdown",
"id": "61ff8194-669c-4ece-ba31-44e88ed808ac",
...
...
@@ -593,7 +604,7 @@
"id": "b597c363-8066-470e-b1c8-edd00b114317",
"metadata": {},
"source": [
"For more details: [https://docs.julialang.org/en/v1/manual/control-flow/](https://docs.julialang.org/en/v1/manual/control-flow/)"
"For more details: [https://docs.julialang.org/en/v1/manual/control-flow/](https://docs.julialang.org/en/v1/manual/control-flow/)."
]
},
{
...
...
@@ -762,6 +773,14 @@
"end"
]
},
{
"cell_type": "markdown",
"id": "b8eac9e9-dff9-4b86-88a7-e14f27b7f677",
"metadata": {},
"source": [
"For more detials: [https://docs.julialang.org/en/v1/base/collections/#lib-collections-iteration](https://docs.julialang.org/en/v1/base/collections/#lib-collections-iteration)."
]
},
{
"cell_type": "markdown",
"id": "990cefae-abfe-4649-8d68-3fac1b1e268b",
...
...
@@ -918,7 +937,9 @@
" - floor: Largest integer not greater than x.\n",
" - ceil: Smallest integer not less than x.\n",
" - round: Nearest integer to x.\n",
" - trunc: Integer part of x."
" - trunc: Integer part of x.\n",
"\n",
"For more details: [https://docs.julialang.org/en/v1/base/math/#Mathematical-Functions](https://docs.julialang.org/en/v1/base/math/#Mathematical-Functions)."
]
},
{
...
...
@@ -1738,19 +1759,11 @@
"source": [
"- Schreiben Sie eine Funktion, die einen Satz analysiert. Die Funktion sollte dann die Anzahl der Vokale (sowohl in Groß- als auch in Kleinbuchstaben) im Satz zählen und anzeigen."
Im Allgemeinen können Sie mathematische Operationen nicht auf Zeichenketten ausführen, auch wenn die Zeichenketten wie Zahlen aussehen. Daher sind die folgenden Operationen nicht erlaubt:
A boolean expression is an expression that is either true or false. The following examples use the operator ==, which compares two operands and produces true if they are equal and false otherwise:
Ein boolescher Ausdruck ist ein Ausdruck, der entweder wahr oder falsch ist. Die folgenden Beispiele verwenden den Operator ==, der zwei Operanden vergleicht und true zurückgibt, wenn sie gleich sind, andernfalls false:
There are three logical operators: && (and), || (or), and ! (not). The semantics (meaning) of these operators is similar to their meaning in English. For example, x > 0 && x < 10 is true only if x is greater than 0 and less than 10.
Es gibt drei logische Operatoren: && (und), || (oder) und ! (nicht). Die Bedeutung dieser Operatoren entspricht weitgehend ihrer Bedeutung im Englischen. Zum Beispiel ist x > 0 && x < 10 nur dann wahr, wenn x größer als 0 und kleiner als 10 ist.
Schließlich kehrt der ! Operator einen booleschen Ausdruck um, sodass !(x > y) wahr ist, wenn x > y falsch ist, das heißt, wenn x kleiner oder gleich y ist.
Sometimes you don’t know it’s time to end a loop until you get half way through the body. In that case you can use the `break` statement to jump out of the loop.
Manchmal weiß man nicht, wann es an der Zeit ist, eine Schleife zu beenden, bis man sich bereits in der Mitte des Schleifenkörpers befindet. In diesem Fall können Sie das `break`-Statement verwenden, um aus der Schleife auszubrechen.
The `break` statement exits the loop. When a `continue` statement is encountered inside a loop, control jumps to the beginning of the loop for the next iteration, skipping the execution of statements inside the body of the loop for the current iteration. For example:
Das `break`-Statement beendet die Schleife. Wenn ein `continue`-Statement in einer Schleife auftritt, springt die Kontrolle zum Anfang der Schleife für die nächste Iteration, wobei die Ausführung der Anweisungen im Körper der Schleife für die aktuelle Iteration übersprungen wird. Zum Beispiel:
For more detials: [https://docs.julialang.org/en/v1/base/collections/#lib-collections-iteration](https://docs.julialang.org/en/v1/base/collections/#lib-collections-iteration).
Use this formula to compute an estimate of π. It should use a while loop ot compute terms of the summation untile the last term is smaller than 1e-15. Afterwards, you can check the result by comparing it to π (to get π in Julia, write \pi and then press TAB).
Verwenden Sie diese Formel, um eine Schätzung von π zu berechnen. Sie sollte eine While-Schleife verwenden, um die Terme der Summation zu berechnen, bis der letzte Term kleiner als 1e-15 ist. Anschließend können Sie das Ergebnis überprüfen, indem Sie es mit π vergleichen (um π in Julia zu erhalten, schreiben Sie \pi und drücken dann die TAB-Taste).
Julia provides functions that convert values from one type to another. The parse function takes a string and converts it to any number type, if it can, or complains otherwise:
Julia bietet Funktionen, die Werte von einem Typ in einen anderen konvertieren. Die Funktion `parse` nimmt einen String und wandelt ihn in jeden Zahlen-Typ um, wenn es möglich ist, oder gibt andernfalls eine Fehlermeldung aus:
For more details: [https://docs.julialang.org/en/v1/base/math/#Mathematical-Functions](https://docs.julialang.org/en/v1/base/math/#Mathematical-Functions).
So far, we have only been using the functions that come with Julia, but it is also possible to add new functions.
Defining functions follows a rather intuitive syntax. The value obtained by evaluating the last expression of a `function` block will be automatically returned:
Bisher haben wir nur die Funktionen verwendet, die mit Julia geliefert werden, aber es ist auch möglich, neue Funktionen hinzuzufügen.
Die Definition von Funktionen folgt einer recht intuitiven Syntax. Der Wert, der durch Auswerten des letzten Ausdrucks eines `function`-Blocks erhalten wird, wird automatisch zurückgegeben:
Notice, that for each type combination a separate piece of code will be compiled even though we only *defined* the functionality a single time. This compilation takes place on first use of a particular tuple of types.
Beachten Sie, dass für jede Kombination von Typen ein separater Codeabschnitt kompiliert wird, obwohl wir die Funktionalität nur einmal *definiert* haben. Diese Kompilierung findet beim ersten Gebrauch eines bestimmten Tupels von Typen statt.
Julia makes a distinction between **functions** and **methods**. Roughly speaking **function**s specify *what* is done and **methods** specify *how* this is done.
Methods are concrete implementations in form of a list of Julia expressions to be executed, when the function name is used in the code. Multiple methods may be defined for the same function name. They differ in the number of arguments or in the supported argument types (more on this in the next notebook). When a particular function name is used in the code, Julia looks at the types of the arguments and uses this information to **dispatch** to the best-fitting method.
For our `myfold` example, one could easily imagine a few more method implementations, for example:
Julia unterscheidet zwischen **Funktionen** und **Methoden**. Grob gesagt geben **Funktionen** an, *was* gemacht wird, und **Methoden** geben an, *wie* dies gemacht wird.
Methoden sind konkrete Implementierungen in Form von einer Liste von Julia-Ausdrücken, die ausgeführt werden, wenn der Funktionsname im Code verwendet wird. Für den gleichen Funktionsnamen können mehrere Methoden definiert werden. Sie unterscheiden sich in der Anzahl der Argumente oder in den unterstützten Argumenttypen (dazu mehr im nächsten Notebook). Wenn ein bestimmter Funktionsname im Code verwendet wird, betrachtet Julia die Typen der Argumente und verwendet diese Informationen, um zur am besten passenden Methode zu **dispatchen**.
Für unser `myfold`-Beispiel könnte man sich leicht einige weitere Methodenimplementierungen vorstellen, zum Beispiel:
Standard functions (like `+` or `*`) are by no means special and behave exactly the same way as custom functions ... including the ability to define new methods for them:
Standardfunktionen (wie `+` oder `*`) sind keineswegs speziell und verhalten sich genau wie benutzerdefinierte Funktionen ... einschließlich der Möglichkeit, neue Methoden für sie zu definieren:
(**Wichtiger Hinweis:** Da wir weder die `+` Funktion noch den `String` Typ besitzen, handelt es sich hierbei um **Typenpiraterie** und sollte im Allgemeinen vermieden werden!)
Es mag nicht klar sein, warum es sich lohnt, ein Programm in Funktionen zu unterteilen. Es gibt mehrere Gründe dafür:
- Das Erstellen einer neuen Funktion bietet Ihnen die Möglichkeit, eine Gruppe von Anweisungen zu benennen, was Ihr Programm leichter lesbar und einfacher zu debuggen macht.
- Funktionen können ein Programm kleiner machen, indem sie wiederholten Code eliminieren. Später, wenn Sie eine Änderung vornehmen, müssen Sie sie nur an einer Stelle vornehmen.
- Das Aufteilen eines langen Programms in Funktionen ermöglicht es Ihnen, die Teile nacheinander zu debuggen und dann zu einem funktionierenden Ganzen zusammenzufügen.
- Gut gestaltete Funktionen sind oft für viele Programme nützlich. Sobald Sie eine geschrieben und debuggt haben, können Sie sie wiederverwenden.
- In Julia können Funktionen die Leistung erheblich verbessern.
It is legal for one function to call another; it is also legal for a function to call itself. It may not be obvious why that is a good thing, but it turns out to be one of the most magical things a program can do. For example, look at the following function:
Es ist erlaubt, dass eine Funktion eine andere aufruft; es ist auch erlaubt, dass eine Funktion sich selbst aufruft. Es mag nicht offensichtlich sein, warum das eine gute Sache ist, aber es stellt sich heraus, dass es eine der magischsten Dinge ist, die ein Programm tun kann. Schauen Sie sich zum Beispiel die folgende Funktion an:
If a recursion never reaches a base case, it goes on making recursive calls forever, and the program never terminates. This is known as infinite recursion, and it is generally not a good idea. Here is a minimal program with an infinite recursion:
Wenn eine Rekursion niemals einen Basisfall erreicht, macht sie unendliche rekursive Aufrufe, und das Programm terminiert nie. Dies wird als unendliche Rekursion bezeichnet und ist im Allgemeinen keine gute Idee. Hier ist ein minimales Programm mit einer unendlichen Rekursion:
- Create a function called `fibonacci_sum` that takes a positive integer n as input. This function should return the sum of the first n terms of the Fibonacci sequence.
- Erstellen Sie eine Funktion namens `fibonacci_sum`, die eine positive ganze Zahl n als Eingabe erhält. Diese Funktion sollte die Summe der ersten n Terme der Fibonacci-Folge zurückgeben.
- Write a function that converts a Celsius temperature in Fahrenheit and returns the result. Additionally, if the temperature is below freezing (0°C), the program should print a message indicating that it's below freezing.
- Schreiben Sie eine Funktion, die eine Celsius-Temperatur in Fahrenheit umrechnet und das Ergebnis zurückgibt. Zusätzlich sollte das Programm, wenn die Temperatur unter dem Gefrierpunkt (0°C) liegt, eine Meldung ausgeben, die anzeigt, dass es unter dem Gefrierpunkt liegt.
- Write a function which analyzes a sentence. The function should then count and display the number of vowels (both upper and lower case) in the sentence.
- Schreiben Sie eine Funktion, die einen Satz analysiert. Die Funktion sollte dann die Anzahl der Vokale (sowohl in Groß- als auch in Kleinbuchstaben) im Satz zählen und anzeigen.