From 1bb64008197e1c104d6d885496d3b9b02c0224d8 Mon Sep 17 00:00:00 2001 From: Michele Nottoli <michele.nottoli@gmail.com> Date: Thu, 5 Oct 2023 16:43:42 +0200 Subject: [PATCH] Multilingual version of notebook 1. --- 1_Basics.ipynb | 622 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 598 insertions(+), 24 deletions(-) diff --git a/1_Basics.ipynb b/1_Basics.ipynb index 4b76a6d..587665c 100644 --- a/1_Basics.ipynb +++ b/1_Basics.ipynb @@ -8,6 +8,14 @@ "# Basics" ] }, + { + "cell_type": "markdown", + "id": "aff6c2f6-3d10-412e-82aa-c199c98edbd7", + "metadata": {}, + "source": [ + "# Grundlagen" + ] + }, { "cell_type": "markdown", "id": "bf904dbe-3197-4a91-bbb8-05e1c7f7dfee", @@ -18,6 +26,16 @@ "Defining variables in Julia works like in most languages:" ] }, + { + "cell_type": "markdown", + "id": "3be02b91-9619-4cfd-b463-2add42babbcc", + "metadata": {}, + "source": [ + "## Variablen und elementare Datentypen\n", + "\n", + "Das Definieren von Variablen in Julia funktioniert wie in den meisten Sprachen:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -66,6 +84,14 @@ "The type is automatically inferred:" ] }, + { + "cell_type": "markdown", + "id": "816bc593-70cc-4e0f-b461-c9022d9587f6", + "metadata": {}, + "source": [ + "Der Typ wird automatisch abgeleitet:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -104,6 +130,14 @@ "Julia supports a large range of integer and floating-point types out of the box, for example:" ] }, + { + "cell_type": "markdown", + "id": "d5e586cc-874a-46d5-9c7c-d1136d73492e", + "metadata": {}, + "source": [ + "Julia unterstützt von Haus aus eine große Bandbreite von Ganzzahl- und Gleitkommazahlentypen, zum Beispiel:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -126,6 +160,14 @@ "If you give a variable an illegal name, you get a syntax error:" ] }, + { + "cell_type": "markdown", + "id": "bf9f6efc-6b47-415c-8504-35a2b1a80f18", + "metadata": {}, + "source": [ + "Wenn Sie einer Variable einen ungültigen Namen geben, erhalten Sie einen Syntaxfehler:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -161,7 +203,15 @@ "id": "cc36688b-c983-43f8-8bdf-9b82aae59909", "metadata": {}, "source": [ - "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." + ] + }, + { + "cell_type": "markdown", + "id": "2e5659b9-9e66-448a-a768-23d7085d7dac", + "metadata": {}, + "source": [ + "Es stellt sich heraus, dass `struct` eines der Schlüsselwörter von Julia ist und nicht als Variablennamen verwendet werden kann." ] }, { @@ -172,6 +222,14 @@ "### Strings" ] }, + { + "cell_type": "markdown", + "id": "e099c88a-5848-4730-be7d-b7651bc3db29", + "metadata": {}, + "source": [ + "### Zeichenketten" + ] + }, { "cell_type": "markdown", "id": "1b3531e9-33db-4c51-b394-c1def6621519", @@ -180,6 +238,14 @@ "In general, you can’t perform mathematical operations on strings, even if the strings look like numbers, so the following are illegal:" ] }, + { + "cell_type": "markdown", + "id": "925cbca6-cce3-4084-96a2-fe0a5acdb1e9", + "metadata": {}, + "source": [ + "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:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -200,6 +266,14 @@ "But there are two exceptions, * and ^." ] }, + { + "cell_type": "markdown", + "id": "c2c39aa8-d98b-42dd-8e7c-09463f78d22c", + "metadata": {}, + "source": [ + "Es gibt jedoch zwei Ausnahmen, * und ^." + ] + }, { "cell_type": "code", "execution_count": null, @@ -228,6 +302,14 @@ "### Exercises" ] }, + { + "cell_type": "markdown", + "id": "43fe0add-b90d-4ed4-862e-37a4361d2354", + "metadata": {}, + "source": [ + "### Übungen" + ] + }, { "cell_type": "markdown", "id": "e0aecc9c-e77f-449e-8aff-e1ffcf5d7833", @@ -237,6 +319,16 @@ " - The volume of a sphere with radius r is 4/3πr^3. What is the volume of a sphere with radius 5?" ] }, + { + "cell_type": "markdown", + "id": "3e392c67-df83-49fc-831d-12e157e4a85c", + "metadata": {}, + "source": [ + "Üben Sie, Julia als Taschenrechner zu verwenden:\n", + "\n", + " - Die Formel für das Volumen einer Kugel mit Radius r lautet 4/3πr^3. Was ist das Volumen einer Kugel mit Radius 5?" + ] + }, { "cell_type": "code", "execution_count": null, @@ -253,6 +345,14 @@ " - Suppose the cover price of a book is € 24.95, but bookstores get a 40 % discount. Shipping costs € 3 for the first copy and 75 cents for each additional copy. What is the total wholesale cost for 60 copies?" ] }, + { + "cell_type": "markdown", + "id": "32f7a6f1-6a19-4ebe-bd1b-846f861dc721", + "metadata": {}, + "source": [ + "- Angenommen, der Listenpreis eines Buches beträgt € 24,95, aber Buchhandlungen erhalten einen Rabatt von 40 %. Der Versand kostet € 3 für das erste Exemplar und 75 Cent für jedes zusätzliche Exemplar. Was sind die gesamten Großhandelskosten für 60 Exemplare?\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -271,6 +371,16 @@ "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:" ] }, + { + "cell_type": "markdown", + "id": "53e26ee8-c10d-42e4-bbb9-cb5f696139ba", + "metadata": {}, + "source": [ + "## Bedingungen und boolesche Ausdrücke\n", + "\n", + "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:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -299,6 +409,14 @@ "The == operator is one of the relational operators; the others are:" ] }, + { + "cell_type": "markdown", + "id": "4722b7b7-3f80-454f-bd8b-f67912105f79", + "metadata": {}, + "source": [ + "Der == Operator ist einer der relationalen Operatoren; die anderen sind:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -325,6 +443,14 @@ "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. " ] }, + { + "cell_type": "markdown", + "id": "01c9f186-294f-4659-bc32-cee3b6ff4f40", + "metadata": {}, + "source": [ + "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." + ] + }, { "cell_type": "code", "execution_count": null, @@ -344,6 +470,14 @@ "It is also worth mentioning that in combined logical expression not all conditions are necessarily executed." ] }, + { + "cell_type": "markdown", + "id": "857cde91-3430-480c-9f24-bb9c465248d9", + "metadata": {}, + "source": [ + "Es ist auch erwähnenswert, dass bei kombinierten logischen Ausdrücken nicht alle Bedingungen notwendigerweise ausgeführt werden." + ] + }, { "cell_type": "code", "execution_count": null, @@ -374,6 +508,14 @@ "Finally, the ! operator negates a boolean expression, so !(x > y) is true if x > y is false, that is, if x is less than or equal to y." ] }, + { + "cell_type": "markdown", + "id": "d8db49a8-6c6c-4abf-9a5b-5d27765ab1c7", + "metadata": {}, + "source": [ + "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." + ] + }, { "cell_type": "code", "execution_count": null, @@ -393,6 +535,14 @@ "Unsurprisingly Julia has the standard conditionals." ] }, + { + "cell_type": "markdown", + "id": "fbe0d04d-29c9-4bd0-9cfb-0c57a3b3f877", + "metadata": {}, + "source": [ + "Es überrascht nicht, dass Julia die üblichen Bedingungsanweisungen hat." + ] + }, { "cell_type": "code", "execution_count": null, @@ -419,6 +569,14 @@ "There is also an inline conditional." ] }, + { + "cell_type": "markdown", + "id": "64a3bf26-156f-40a8-8d8c-d4b2f838ef95", + "metadata": {}, + "source": [ + "Es gibt auch eine bedingte Inline-Anweisung." + ] + }, { "cell_type": "code", "execution_count": null, @@ -435,8 +593,15 @@ "id": "b597c363-8066-470e-b1c8-edd00b114317", "metadata": {}, "source": [ - "For more details\n", - " 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/)" + ] + }, + { + "cell_type": "markdown", + "id": "2539a6ec-e950-4b76-ad13-4ac9bb28ec26", + "metadata": {}, + "source": [ + "Für weitere Details siehe: [https://docs.julialang.org/en/v1/manual/control-flow/](https://docs.julialang.org/en/v1/manual/control-flow/)" ] }, { @@ -449,6 +614,16 @@ "Julia implements the standard for and while constructs." ] }, + { + "cell_type": "markdown", + "id": "7c8eb301-3404-4d13-bc5d-f4e2f71cfa12", + "metadata": {}, + "source": [ + "## Iterationen\n", + "\n", + "Julia implementiert die üblichen for- und while-Strukturen." + ] + }, { "cell_type": "code", "execution_count": null, @@ -469,6 +644,14 @@ "Where the `1:5` is a `Range`-object, but it could be any iterable. There are a few syntax variations, including:" ] }, + { + "cell_type": "markdown", + "id": "d0933cf7-b409-4c40-8184-70d0770a06b1", + "metadata": {}, + "source": [ + "Dabei ist `1:5` ein `Range`-Objekt, aber es könnte jede Art von iterierbarem Objekt sein. Es gibt einige syntaktische Variationen, einschließlich:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -476,12 +659,12 @@ "metadata": {}, "outputs": [], "source": [ - "sum = 0\n", + "result = 0\n", "# To get ∈ write \\in and then press TAB\n", "for j ∈ 1:0.5:3 # Note the step parameter\n", - " sum += j\n", + " result += j\n", "end\n", - "println(sum)" + "println(result)" ] }, { @@ -503,7 +686,15 @@ "id": "f19d5051-ce82-49cc-80aa-2434a6adc9d0", "metadata": {}, "source": [ - "The loops can be further controlled with the break and continue keywords." + "The loops can be further controlled with the `break` and `continue` keywords." + ] + }, + { + "cell_type": "markdown", + "id": "76563c39-4e45-4daf-b703-575909f70787", + "metadata": {}, + "source": [ + "Die Schleifen können mit den Schlüsselwörtern `break` und `continue` weiter gesteuert werden." ] }, { @@ -511,7 +702,15 @@ "id": "d0ebce97-3054-4eba-821d-92f27793d752", "metadata": {}, "source": [ - "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." + "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." + ] + }, + { + "cell_type": "markdown", + "id": "b38f525d-db83-4fbf-808c-bf39080650f5", + "metadata": {}, + "source": [ + "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." ] }, { @@ -537,7 +736,15 @@ "id": "98eee547-21fe-4022-bde5-b02ababa26dd", "metadata": {}, "source": [ - "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:" + "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:" + ] + }, + { + "cell_type": "markdown", + "id": "873c3625-3b99-48e5-9821-9b696a8f8a12", + "metadata": {}, + "source": [ + "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:" ] }, { @@ -563,6 +770,14 @@ "### Exercises" ] }, + { + "cell_type": "markdown", + "id": "625f1e1b-ab4d-425d-b848-94385ddcb690", + "metadata": {}, + "source": [ + "### Übungen" + ] + }, { "cell_type": "markdown", "id": "bdb2eef9-bdee-4cf3-8bab-d6c1d46ecc2f", @@ -575,6 +790,18 @@ "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)." ] }, + { + "cell_type": "markdown", + "id": "6a0dcd06-0926-4529-ac33-0aee315f85cf", + "metadata": {}, + "source": [ + "Der Mathematiker Srinivasa Ramanujan fand eine unendliche Reihe, die verwendet werden kann, um eine numerische Näherung von 1/π zu generieren:\n", + "\n", + "$$ \\frac{1}{\\pi} = \\frac{2\\sqrt{2}}{9801} \\sum_{k=0}^\\infty \\frac{(4k)! (1103 + 26390 k)}{(k!)^4 396^{4 k}}$$\n", + "\n", + "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).\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -591,6 +818,14 @@ "## Functions" ] }, + { + "cell_type": "markdown", + "id": "e2072b09-53e0-4354-984b-13813e93fef1", + "metadata": {}, + "source": [ + "## Funktionen" + ] + }, { "cell_type": "markdown", "id": "682a3feb-0b82-479d-9803-3011a1e6e521", @@ -599,6 +834,14 @@ "We have already seen one example of a function call:" ] }, + { + "cell_type": "markdown", + "id": "8fde350c-f73a-44b7-9459-b8f6d79120cf", + "metadata": {}, + "source": [ + "Wir haben bereits ein Beispiel für einen Funktionsaufruf gesehen:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -618,6 +861,15 @@ "It is common to say that a function “takes” an argument and “returns” a result. The result is also called the return value." ] }, + { + "cell_type": "markdown", + "id": "ef1e8206-add5-49f9-ab4c-4b730164aee6", + "metadata": {}, + "source": [ + "Der Name der Funktion ist println. Der Ausdruck in Klammern wird als Argument der Funktion bezeichnet.\n", + "Es ist üblich zu sagen, dass eine Funktion ein Argument \"nimmt\" und ein Ergebnis \"zurückgibt\". Das Ergebnis wird auch als Rückgabewert bezeichnet.\n" + ] + }, { "cell_type": "markdown", "id": "f63d270a-7149-4ffd-bc31-f746560a37ac", @@ -626,6 +878,14 @@ "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:" ] }, + { + "cell_type": "markdown", + "id": "bd083417-b51a-4f0e-a150-56a0f9c33569", + "metadata": {}, + "source": [ + "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:\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -661,6 +921,21 @@ " - trunc: Integer part of x." ] }, + { + "cell_type": "markdown", + "id": "9176592b-e9f3-46fd-9909-8b73da750434", + "metadata": {}, + "source": [ + "Julia stellt verschiedene mathematische Funktionen zur Verfügung:\n", + " - abs, sqrt, cbrt, exp, log, log10, log2\n", + " - sin, cos, tan, asin, acos, atan\n", + " - sinh, cosh, tanh, asinh, acosh, atanh\n", + " - floor: Größte ganze Zahl, die nicht größer als x ist.\n", + " - ceil: Kleinste ganze Zahl, die nicht kleiner als x ist.\n", + " - round: Nächstgelegene ganze Zahl zu x.\n", + " - trunc: Ganzzahliger Teil von x." + ] + }, { "cell_type": "markdown", "id": "d048851b-8eac-4dc3-81b7-ea1d625a1db2", @@ -670,6 +945,15 @@ "Defining functions follows a rather intuitive syntax. The value obtained by evaluating the last expression of a `function` block will be automatically returned:" ] }, + { + "cell_type": "markdown", + "id": "f43cb468-832a-4cab-b8a3-15bb9bcbfb0d", + "metadata": {}, + "source": [ + "Bisher haben wir nur die Funktionen verwendet, die mit Julia geliefert werden, aber es ist auch möglich, neue Funktionen hinzuzufügen.\n", + "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:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -690,6 +974,14 @@ "For one-line functions one may also use a convenient short-hand:" ] }, + { + "cell_type": "markdown", + "id": "e8dfb238-54be-4a95-9eda-4ce1fa5cfd74", + "metadata": {}, + "source": [ + "Für Einzeiler-Funktionen kann man auch eine praktische Kurzschreibweise verwenden:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -708,6 +1000,14 @@ "Both such functions are fully generic in the argument types" ] }, + { + "cell_type": "markdown", + "id": "9b688bbf-08b6-4fba-babe-b16b6e1119be", + "metadata": {}, + "source": [ + "Für Einzeiler-Funktionen kann man auch eine praktische Kurzschreibweise verwenden:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -728,6 +1028,14 @@ "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." ] }, + { + "cell_type": "markdown", + "id": "6add8ab6-7f66-431b-9e9e-e52d45379483", + "metadata": {}, + "source": [ + "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." + ] + }, { "cell_type": "code", "execution_count": null, @@ -746,6 +1054,14 @@ "and may be passed around to other functions, for example:" ] }, + { + "cell_type": "markdown", + "id": "3be89ab5-16f3-4633-bb8d-fd74cf39ddec", + "metadata": {}, + "source": [ + "und können an andere Funktionen übergeben werden, zum Beispiel:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -767,9 +1083,21 @@ "source": [ "Julia makes a distinction between **functions** and **methods**. Roughly speaking **function**s specify *what* is done and **methods** specify *how* this is done.\n", "\n", - "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 a second). 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. \n", + "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. \n", "\n", - "For our `myfold` example, one could easily imagine a few more method implementations, for example " + "For our `myfold` example, one could easily imagine a few more method implementations, for example:" + ] + }, + { + "cell_type": "markdown", + "id": "09a40182-e59f-42e4-b11e-0c3aedd0e7a6", + "metadata": {}, + "source": [ + "Julia unterscheidet zwischen **Funktionen** und **Methoden**. Grob gesagt geben **Funktionen** an, *was* gemacht wird, und **Methoden** geben an, *wie* dies gemacht wird.\n", + "\n", + "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**.\n", + "\n", + "Für unser `myfold`-Beispiel könnte man sich leicht einige weitere Methodenimplementierungen vorstellen, zum Beispiel:" ] }, { @@ -801,6 +1129,14 @@ "So now `myfold` works transparently with 1, 2 or 3 arguments:" ] }, + { + "cell_type": "markdown", + "id": "cc818df4-a037-4ba2-8588-1afe4a6de222", + "metadata": {}, + "source": [ + "Nun funktioniert `myfold` transparent mit 1, 2 oder 3 Argumenten:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -821,6 +1157,14 @@ "We can also check which method is actually employed using the `@which` macro:" ] }, + { + "cell_type": "markdown", + "id": "5a9a652b-80c4-482f-80b8-2c5340e61a21", + "metadata": {}, + "source": [ + "Wir können auch überprüfen, welche Methode tatsächlich verwendet wird, indem wir das `@which`-Makro verwenden:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -839,6 +1183,14 @@ "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:" ] }, + { + "cell_type": "markdown", + "id": "4046d2e8-2018-4f4f-b816-2a6156301f39", + "metadata": {}, + "source": [ + "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:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -868,6 +1220,14 @@ "(**Important note:** Since we neither own the `+` function nor the `String` type, this is known as **type piracy** and should in general be avoided!)" ] }, + { + "cell_type": "markdown", + "id": "ee25eff6-f56e-4230-b52d-6020dcc29bb2", + "metadata": {}, + "source": [ + "(**Wichtiger Hinweis:** Da wir weder die `+` Funktion noch den `String` Typ besitzen, handelt es sich hierbei um **Typenpiraterie** und sollte im Allgemeinen vermieden werden!)" + ] + }, { "cell_type": "markdown", "id": "89ffcdef-cf06-438f-8448-4bf6dfd1af7e", @@ -876,6 +1236,14 @@ "Now standard functions relying on `+` just magically work:" ] }, + { + "cell_type": "markdown", + "id": "a2c9d8cf-0462-415c-a466-edb33b3aadec", + "metadata": {}, + "source": [ + "Nun funktionieren Standardfunktionen, die auf `+` angewiesen sind, einfach magisch:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -891,7 +1259,15 @@ "id": "d8edaaf0-3de7-4a63-b0bd-bff53ba060f4", "metadata": {}, "source": [ - "Variables and Parameters Are Local:" + "Variables and parameters are local, they exist only inside the functions where they are defined:" + ] + }, + { + "cell_type": "markdown", + "id": "013b3faf-5f12-482b-956e-de97a2a4830d", + "metadata": {}, + "source": [ + "Variablen und Parameter sind lokal, sie existieren nur innerhalb der Funktionen, in denen sie definiert sind:" ] }, { @@ -920,8 +1296,21 @@ " - Functions can make a program smaller by eliminating repetitive code. Later, if you make a change, you only have to make it in one place.\n", " - Dividing a long program into functions allows you to debug the parts one at a time and then assemble them into a working whole.\n", " - Well-designed functions are often useful for many programs. Once you write and debug one, you can reuse it.\n", - " - In Julia, functions can improve performance a lot.\n", - "\n" + " - In Julia, functions can improve performance a lot." + ] + }, + { + "cell_type": "markdown", + "id": "0a398295-22e0-45dc-b43c-5f1915a16364", + "metadata": {}, + "source": [ + "Es mag nicht klar sein, warum es sich lohnt, ein Programm in Funktionen zu unterteilen. Es gibt mehrere Gründe dafür:\n", + "\n", + "- 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.\n", + "- 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.\n", + "- Das Aufteilen eines langen Programms in Funktionen ermöglicht es Ihnen, die Teile nacheinander zu debuggen und dann zu einem funktionierenden Ganzen zusammenzufügen.\n", + "- Gut gestaltete Funktionen sind oft für viele Programme nützlich. Sobald Sie eine geschrieben und debuggt haben, können Sie sie wiederverwenden.\n", + "- In Julia können Funktionen die Leistung erheblich verbessern.\n" ] }, { @@ -929,8 +1318,15 @@ "id": "7780ae66-db68-48c1-ba62-a883406f1f1c", "metadata": {}, "source": [ - "More details\n", - " https://docs.julialang.org/en/v1/manual/methods/" + "More details: [https://docs.julialang.org/en/v1/manual/methods/](https://docs.julialang.org/en/v1/manual/methods/)" + ] + }, + { + "cell_type": "markdown", + "id": "dfa722c2-bd4c-4162-b10d-24c0f8e990e0", + "metadata": {}, + "source": [ + "Weitere Details finden Sie unter: [https://docs.julialang.org/en/v1/manual/methods/](https://docs.julialang.org/en/v1/manual/methods/)" ] }, { @@ -941,12 +1337,28 @@ "### Exercises" ] }, + { + "cell_type": "markdown", + "id": "a4eafce6-b493-470f-86a3-93319684ee40", + "metadata": {}, + "source": [ + "### Übungen" + ] + }, { "cell_type": "markdown", "id": "247215e4-47e1-4eb6-978b-a09f14478bb8", "metadata": {}, "source": [ - " - Write a function printgrid that draws a grid like the following:" + " - Write a function `printgrid` that draws a grid like the following:" + ] + }, + { + "cell_type": "markdown", + "id": "08fe364b-e611-419b-97f2-e806881d6227", + "metadata": {}, + "source": [ + "- Schreiben Sie eine Funktion `printgrid`, die ein Raster wie folgt zeichnet:" ] }, { @@ -983,6 +1395,14 @@ " - Write a function that draws a similar grid with four rows and four columns." ] }, + { + "cell_type": "markdown", + "id": "41f2bf0f-73f4-4671-b059-1118c35ec76f", + "metadata": {}, + "source": [ + "- Schreiben Sie eine Funktion, die ein ähnliches Raster mit vier Zeilen und vier Spalten zeichnet." + ] + }, { "cell_type": "markdown", "id": "b953857f-e8d7-47aa-bc8d-8e2bd1cd20fd", @@ -993,6 +1413,24 @@ " - The function print does not advance to the next line." ] }, + { + "cell_type": "markdown", + "id": "2e606498-6553-4037-a9f9-3b519ffc64c9", + "metadata": {}, + "source": [ + "Tipps:\n", + "- Um mehr als einen Wert in einer Zeile auszugeben, können Sie eine kommagetrennte Sequenz von Werten ausgeben: println(\"+\", \"-\").\n", + "- Die Funktion print springt nicht zur nächsten Zeile." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1ca1789d-eddb-4f41-9ad9-9259c6f22a14", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "id": "2ebc911c-7fe0-4d7f-ab5d-d8084f25a93d", @@ -1003,6 +1441,16 @@ "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:" ] }, + { + "cell_type": "markdown", + "id": "53edcb8b-2f37-404f-8df6-d84b2327e96e", + "metadata": {}, + "source": [ + "### Rekursive Funktionen\n", + "\n", + "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:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -1038,6 +1486,14 @@ "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: " ] }, + { + "cell_type": "markdown", + "id": "73ea203a-3919-4e14-b50f-22c8362eb771", + "metadata": {}, + "source": [ + "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:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -1065,15 +1521,81 @@ "id": "22c8c79d-6d41-4758-9550-aaab4f1dc1b1", "metadata": {}, "source": [ - "### Exercise\n", - " - Write a recursive function to compute the factorial of a number.\n", + "### Exercises\n", + " - Write a recursive function to compute the factorial of a number." + ] + }, + { + "cell_type": "markdown", + "id": "a0402c3d-95fa-44a1-86ab-ca229508f68f", + "metadata": {}, + "source": [ + "### Übungen\n", + " - Schreiben Sie eine rekursive Funktion zur Berechnung der Fakultät einer Zahl." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f4102487-011e-474f-ab79-3d7db1d3bbd9", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "d9c31712-941c-4611-99e5-b33cdb12e28f", + "metadata": {}, + "source": [ " - Then write a function that computes bynomial coefficients\n", "$$ \\left(\\begin{array}{c} n \\\\ k \\end{array}\\right) = \\frac{n!}{k! (n-k)!} $$\n", - " by calling the function of the previous points.\n", - " - Use the two functions to compute\n", + " by calling the function of the previous points." + ] + }, + { + "cell_type": "markdown", + "id": "a24cdaef-98b8-46f5-a809-992547882a4b", + "metadata": {}, + "source": [ + " - Schreiben Sie dann eine Funktion, die die Binomialkoeffizienten berechnet\n", + "$$ \\left(\\begin{array}{c} n \\\\ k \\end{array}\\right) = \\frac{n!}{k! (n-k)!} $$\n", + "indem sie die Funktion aus den vorherigen Punkten aufruft." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b54c877e-5494-4657-ba7f-208bdc301225", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "4509db3f-8060-46ca-89ac-824c16f5751c", + "metadata": {}, + "source": [ + "- Use the two functions to compute\n", "$$ 15! \\qquad \\left(\\begin{array}{c} 5 \\\\ 2 \\end{array}\\right) \\qquad \\left(\\begin{array}{c} 5 \\\\ 3 \\end{array}\\right) $$" ] }, + { + "cell_type": "markdown", + "id": "a32900cc-c244-4d63-8ff6-4d11cee83eee", + "metadata": {}, + "source": [ + " - Verwenden Sie die beiden Funktionen, um zu berechnen\n", + "$$ 15! \\qquad \\left(\\begin{array}{c} 5 \\\\ 2 \\end{array}\\right) \\qquad \\left(\\begin{array}{c} 5 \\\\ 3 \\end{array}\\right) $$" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fb02aad5-d9d4-4f0d-8118-b02b73506b89", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "id": "240cc4bd-3e45-45b8-ad98-170dbb68f78d", @@ -1084,6 +1606,16 @@ "Function arguments can be either position based (like what we saw so far) or keyword based, like in the following example." ] }, + { + "cell_type": "markdown", + "id": "b5944412-0d39-4c0b-a0d5-fbeee22e1432", + "metadata": {}, + "source": [ + "### Schlüsselwortargumente\n", + "\n", + "Funktionsargumente können entweder positionsbasiert (wie wir bisher gesehen haben) oder schlüsselwortbasiert sein, wie im folgenden Beispiel." + ] + }, { "cell_type": "code", "execution_count": null, @@ -1117,6 +1649,16 @@ " - to add at a later point in time new functionality, without breaking retrocompatibility." ] }, + { + "cell_type": "markdown", + "id": "07e76016-d5a9-4616-b0b9-2dc9b6f3c8f6", + "metadata": {}, + "source": [ + "Dies ist in den folgenden Fällen sehr praktisch:\n", + " - um ein Standardverhalten der Funktion festzulegen, während die Möglichkeit besteht, es fein zu steuern.\n", + " - um zu einem späteren Zeitpunkt neue Funktionalitäten hinzuzufügen, ohne die Abwärtskompatibilität zu brechen." + ] + }, { "cell_type": "markdown", "id": "c939f951-2aaf-4e39-a60f-df7600af1d8f", @@ -1125,18 +1667,34 @@ "## Further Exercises" ] }, + { + "cell_type": "markdown", + "id": "883b3945-d30a-4f27-928f-458c9ad74e9b", + "metadata": {}, + "source": [ + "## Weitere Übungen" + ] + }, { "cell_type": "markdown", "id": "59e2e997-42f7-426c-bfd0-233724704117", "metadata": {}, "source": [ - " - 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." + " - 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." + ] + }, + { + "cell_type": "markdown", + "id": "54537041-60eb-4961-9805-1d7d405fde1a", + "metadata": {}, + "source": [ + "- 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." ] }, { "cell_type": "code", "execution_count": null, - "id": "3e704e3b-2c3e-48fa-98c1-3b6ba20f1d65", + "id": "7ca7c936-fb80-4aed-9788-75a55bb207ad", "metadata": {}, "outputs": [], "source": [] @@ -1149,6 +1707,14 @@ " - 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." ] }, + { + "cell_type": "markdown", + "id": "70dacb25-360f-4a7a-8c03-73cd0d75dc3e", + "metadata": {}, + "source": [ + "- 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." + ] + }, { "cell_type": "code", "execution_count": null, @@ -1165,10 +1731,18 @@ " - 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." ] }, + { + "cell_type": "markdown", + "id": "8cabe824-c7eb-46fc-afb8-e039528d2d2a", + "metadata": {}, + "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." + ] + }, { "cell_type": "code", "execution_count": null, - "id": "5ce5ed55-b379-44a6-99dc-bcc8130d8d08", + "id": "8de11d46-125a-4648-88ec-23707c2c4b1b", "metadata": {}, "outputs": [], "source": [] -- GitLab