diff --git a/2_Advanced_Data_Types.ipynb b/2_Advanced_Data_Types.ipynb index 8ce0a004709e0bf6864d8b3338e68b70098dbe48..9b18641c3e5e85ffacfb9eaae194d6e8587e84ea 100644 --- a/2_Advanced_Data_Types.ipynb +++ b/2_Advanced_Data_Types.ipynb @@ -8,6 +8,14 @@ "# Advanced Data Types" ] }, + { + "cell_type": "markdown", + "id": "5de78ecc-bbde-49ff-b618-b8c8c440ce47", + "metadata": {}, + "source": [ + "# Fortgeschrittene Datentypen" + ] + }, { "cell_type": "markdown", "id": "7969b8f9-335a-4bd2-82f7-1d9795234ed5", @@ -16,6 +24,14 @@ "## Dictionaries" ] }, + { + "cell_type": "markdown", + "id": "8ffb53e2-1813-4fdf-962e-e066bc9b737c", + "metadata": {}, + "source": [ + "## Wörterbücher" + ] + }, { "cell_type": "markdown", "id": "ea8518c1-8e1d-4532-8055-e04c3aa3e7c4", @@ -26,6 +42,16 @@ "The function `Dict` creates a new dictionary with no items. Because Dict is the name of a built-in function, you should avoid using it as a variable name. " ] }, + { + "cell_type": "markdown", + "id": "548487ed-dc9d-4404-a2ab-451c0ea742e4", + "metadata": {}, + "source": [ + "Ein Wörterbuch enthält eine Sammlung von Indizes, die als Schlüssel bezeichnet werden, und eine Sammlung von Werten. Jeder Schlüssel ist mit einem einzigen Wert verbunden. Die Zuordnung eines Schlüssels und eines Wertes wird als Schlüssel-Wert-Paar oder manchmal als Element bezeichnet.\n", + "\n", + "Die Funktion `Dict` erstellt ein neues Wörterbuch ohne Elemente. Da Dict der Name einer integrierten Funktion ist, sollte man ihn vermeiden, als Variablenname zu verwenden." + ] + }, { "cell_type": "code", "execution_count": null, @@ -54,6 +80,14 @@ "You can also initialize a dictionary with items as:" ] }, + { + "cell_type": "markdown", + "id": "09b62b4d-748b-4e1d-91f2-7db610ad02ac", + "metadata": {}, + "source": [ + "Du kannst auch ein Wörterbuch mit Elementen initialisieren, wie folgt:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -72,6 +106,14 @@ "Dictionaries are mutable, meaning that it is always possible to modify their elements, add new key-value pairs or removing existing ones." ] }, + { + "cell_type": "markdown", + "id": "809e7c99-22f0-4440-921d-e5904cadc8a4", + "metadata": {}, + "source": [ + "Wörterbücher sind veränderbar, was bedeutet, dass es immer möglich ist, ihre Elemente zu ändern, neue Schlüssel-Wert-Paare hinzuzufügen oder bestehende zu entfernen." + ] + }, { "cell_type": "code", "execution_count": null, @@ -95,6 +137,14 @@ "In general, the order of the key-value pairs is unpredictable. However this is not important, as the values are always accessed using the keys." ] }, + { + "cell_type": "markdown", + "id": "d8b4ee03-5592-4567-883b-e0842fefeff4", + "metadata": {}, + "source": [ + "Im Allgemeinen ist die Reihenfolge der Schlüssel-Wert-Paare unvorhersehbar. Dies ist jedoch nicht wichtig, da die Werte immer mithilfe der Schlüssel abgerufen werden." + ] + }, { "cell_type": "code", "execution_count": null, @@ -113,6 +163,14 @@ "If the key isn’t in the dictionary, you get an exception:" ] }, + { + "cell_type": "markdown", + "id": "84cf8b52-f061-4b28-a327-bc527820517c", + "metadata": {}, + "source": [ + "Wenn der Schlüssel nicht im Wörterbuch vorhanden ist, tritt eine Ausnahme auf:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -131,6 +189,14 @@ "The `length` function works on dictionaries; it returns the number of key-value pairs:" ] }, + { + "cell_type": "markdown", + "id": "f83d51c5-2c1d-4449-8cd5-e80f224b9a76", + "metadata": {}, + "source": [ + "Die Funktion `length` funktioniert auch bei Wörterbüchern; sie gibt die Anzahl der Schlüssel-Wert-Paare zurück:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -149,6 +215,14 @@ "The function `keys` returns a collection with the keys of the dictionary:" ] }, + { + "cell_type": "markdown", + "id": "b4ac7b46-9fc0-47fd-b6c0-c97791517b51", + "metadata": {}, + "source": [ + "Die Funktion `keys` gibt eine Sammlung mit den Schlüsseln des Wörterbuchs zurück:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -167,6 +241,14 @@ "Now you can use the `∈` operator to see whether something appears as a key in the dictionary:" ] }, + { + "cell_type": "markdown", + "id": "f9bc6fe5-3f7a-458e-942a-11bd3e83dcc6", + "metadata": {}, + "source": [ + "Nun kannst du den Operator `∈` verwenden, um zu sehen, ob etwas als Schlüssel im Wörterbuch vorhanden ist:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -186,6 +268,14 @@ "To see whether something appears as a value in a dictionary, you can use the function `values`, which returns a collection of values, and then use the `∈` operator:" ] }, + { + "cell_type": "markdown", + "id": "864d141e-6cd2-45e6-95fe-e84cca35cf70", + "metadata": {}, + "source": [ + "Um zu überprüfen, ob etwas als Wert in einem Wörterbuch vorhanden ist, kannst du die Funktion `values` verwenden, die eine Sammlung von Werten zurückgibt, und dann den Operator `∈` verwenden:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -207,6 +297,16 @@ "You can traverse the keys of the dictionary in a `for` statement." ] }, + { + "cell_type": "markdown", + "id": "5b255b31-8aca-49b4-a95f-302c59baa975", + "metadata": {}, + "source": [ + "### Schleifen über Wörterbücher\n", + "\n", + "Du kannst die Schlüssel des Wörterbuchs in einer `for`-Schleife durchlaufen." + ] + }, { "cell_type": "code", "execution_count": null, @@ -231,6 +331,14 @@ "Again, the keys are in no particular order. To traverse the keys in alphabetically sorted order, you can combine `sort` and `collect`:" ] }, + { + "cell_type": "markdown", + "id": "a6b3285a-58f6-41b6-8755-2732a367424e", + "metadata": {}, + "source": [ + "Auch hier sind die Schlüssel in keiner bestimmten Reihenfolge. Um die Schlüssel in alphabetisch sortierter Reihenfolge zu durchlaufen, kannst du `sort` und `collect` kombinieren:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -256,6 +364,15 @@ " - Write a function that given a string as input counts how many times each letter appears. Use a dictionary." ] }, + { + "cell_type": "markdown", + "id": "964165ee-4d0d-4122-b6b2-d3926101cab2", + "metadata": {}, + "source": [ + "### Übungen\n", + " - Schreibe eine Funktion, die bei Eingabe eines Strings zählt, wie oft jeder Buchstabe vorkommt. Verwende ein Wörterbuch." + ] + }, { "cell_type": "code", "execution_count": null, @@ -272,6 +389,14 @@ "## Tuples and named tuples" ] }, + { + "cell_type": "markdown", + "id": "0acaa2f9-372c-4a3e-be01-27cfe2853fdb", + "metadata": {}, + "source": [ + "## Tupel und benannte Tupel" + ] + }, { "cell_type": "markdown", "id": "a722109e-86cc-44e3-9256-a3e93a1668f5", @@ -280,6 +405,14 @@ "A tuple is a sequence of values. The values can be of any type, and they are indexed by integers. The tuples are immutable and each element can have its own type." ] }, + { + "cell_type": "markdown", + "id": "68ea4303-ce3c-4647-8e9f-89afd0a6a670", + "metadata": {}, + "source": [ + "Ein Tupel ist eine Sequenz von Werten. Die Werte können beliebigen Typs sein und sie werden durch Ganzzahlen indexiert. Die Tupel sind unveränderlich und jedes Element kann seinen eigenen Typ haben." + ] + }, { "cell_type": "code", "execution_count": null, @@ -298,6 +431,14 @@ "Although it is not necessary, it is common to enclose tuples in parentheses:" ] }, + { + "cell_type": "markdown", + "id": "2632b11b-de4b-4f21-bcc6-06a32517c121", + "metadata": {}, + "source": [ + "Obwohl es nicht notwendig ist, ist es üblich, Tupel in Klammern zu setzen:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -316,6 +457,14 @@ "To create a tuple with a single element, you have to include a final comma:" ] }, + { + "cell_type": "markdown", + "id": "86fa8b6b-bbbf-4c9c-a5c0-63b00b2c59cb", + "metadata": {}, + "source": [ + "Um ein Tupel mit einem einzigen Element zu erstellen, musst du ein abschließendes Komma setzen:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -338,6 +487,14 @@ "Another way to create a tuple is the built-in function tuple. With no argument, it creates an empty tuple:" ] }, + { + "cell_type": "markdown", + "id": "d6f34b5b-2aaa-49db-b2a8-d5891fe33ee1", + "metadata": {}, + "source": [ + "Eine andere Möglichkeit, ein Tupel zu erstellen, ist die integrierte Funktion `tuple`. Ohne Argument erstellt sie ein leeres Tupel:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -360,6 +517,14 @@ "Using the bracket operator it is possible to access an element. Note, differently from other programming languages (for example C, C++, Python) in Julia the first element is 1." ] }, + { + "cell_type": "markdown", + "id": "efc3d061-7e93-4863-a802-a7cdb869af7e", + "metadata": {}, + "source": [ + "Mit dem eckigen Klammern-Operator ist es möglich, auf ein Element zuzugreifen. Beachte, dass in Julia das erste Element, anders als in anderen Programmiersprachen (zum Beispiel C, C++, Python), mit 1 indexiert wird." + ] + }, { "cell_type": "code", "execution_count": null, @@ -378,6 +543,14 @@ "It is also possible to get multiple elements by using slices." ] }, + { + "cell_type": "markdown", + "id": "17a0f0ee-e03f-4669-a5f1-889d875577d9", + "metadata": {}, + "source": [ + "Es ist auch möglich, mehrere Elemente mithilfe von Slices zu erhalten." + ] + }, { "cell_type": "code", "execution_count": null, @@ -396,6 +569,14 @@ "But if you try to modify one of the elements of the tuple, you get an error:" ] }, + { + "cell_type": "markdown", + "id": "d0f25a1f-0a4c-4ab4-a339-f5ba64806681", + "metadata": {}, + "source": [ + "Aber wenn du versuchst, eines der Elemente des Tupels zu ändern, tritt ein Fehler auf:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -414,6 +595,14 @@ "Because tuples are immutable, you can’t modify the elements." ] }, + { + "cell_type": "markdown", + "id": "d956168e-36e4-4abb-a3d6-3c53a857e78d", + "metadata": {}, + "source": [ + "Da Tupel unveränderlich sind, kannst du die Elemente nicht ändern." + ] + }, { "cell_type": "markdown", "id": "a10cb9a7-8f44-44f8-831f-178b780e4ac9", @@ -424,6 +613,16 @@ "The built-in function `divrem` takes two arguments and returns a tuple of two values, the quotient and remainder. You can store the result as a tuple: " ] }, + { + "cell_type": "markdown", + "id": "6a115819-54fd-4550-b097-59fea3ecb523", + "metadata": {}, + "source": [ + "Genau genommen kann eine Funktion nur einen Wert zurückgeben, aber wenn der Wert ein Tupel ist, hat dies den gleichen Effekt wie das Zurückgeben mehrerer Werte. Zum Beispiel, wenn du zwei Ganzzahlen teilen und den Quotienten und den Rest berechnen möchtest, ist es ineffizient, zuerst x ÷ y und dann x % y zu berechnen. Es ist besser, beides gleichzeitig zu berechnen.\n", + "\n", + "Die integrierte Funktion `divrem` nimmt zwei Argumente entgegen und gibt ein Tupel aus zwei Werten zurück, dem Quotienten und dem Rest. Du kannst das Ergebnis als Tupel speichern:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -442,6 +641,14 @@ "Alternatively, we can use tuple assignment to store the elements separately:" ] }, + { + "cell_type": "markdown", + "id": "9cd48d9c-cf1d-4546-a2c0-4bb54a42e1e8", + "metadata": {}, + "source": [ + "Alternativ können wir die Zuweisung als Tupel verwenden, um die Elemente getrennt zu speichern:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -464,6 +671,16 @@ "Functions can take a variable number of arguments. A parameter name that ends with `...` gathers arguments into a tuple. For example, `printall` takes any number of arguments and prints them one by line:" ] }, + { + "cell_type": "markdown", + "id": "e4177fab-a386-4274-8657-bbc6698ec1e5", + "metadata": {}, + "source": [ + "### Sammeln und verteilen\n", + "\n", + "Funktionen können eine variable Anzahl von Argumenten entgegennehmen. Ein Parametername, der mit `...` endet, sammelt die Argumente in ein Tupel. Zum Beispiel nimmt `printall` eine beliebige Anzahl von Argumenten entgegen und gibt sie nacheinander aus:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -489,6 +706,14 @@ "The complement of gather is scatter. If you have a sequence of values and you want to pass it to a function as multiple arguments, you can use the `...` operator. For example, `divrem` takes exactly two arguments; it doesn’t work with a tuple:" ] }, + { + "cell_type": "markdown", + "id": "28b3cd91-bf05-43bc-a253-2bee077a1df1", + "metadata": {}, + "source": [ + "Das Gegenstück zu \"sammeln\" ist \"verteilen\". Wenn du eine Sequenz von Werten hast und sie als mehrere Argumente an eine Funktion übergeben möchtest, kannst du den Operator `...` verwenden. Zum Beispiel nimmt `divrem` genau zwei Argumente an; es funktioniert nicht mit einem Tupel:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -508,6 +733,14 @@ "But if you scatter the tuple, it works:" ] }, + { + "cell_type": "markdown", + "id": "db3cd00d-d41c-4f68-95d4-c4e5b283734a", + "metadata": {}, + "source": [ + "Aber wenn du das Tupel verteilst, funktioniert es:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -527,6 +760,14 @@ "### Dictionaries and Tuples" ] }, + { + "cell_type": "markdown", + "id": "dad5d7bf-0dd5-450b-90f3-7768f2d91c18", + "metadata": {}, + "source": [ + "### Wörterbücher und Tupel" + ] + }, { "cell_type": "markdown", "id": "00e97ccc-8e64-4eef-84be-85a266eb3490", @@ -535,6 +776,14 @@ "Dictionaries can be used as iterators that iterate the key-value pairs. You can use it in a `for` loop like this:" ] }, + { + "cell_type": "markdown", + "id": "3a38cabd-b056-4140-a75c-e0fa383a2a74", + "metadata": {}, + "source": [ + "Wörterbücher können als Iteratoren verwendet werden, um die Schlüssel-Wert-Paare zu durchlaufen. Du kannst es in einer `for`-Schleife wie folgt verwenden:\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -556,6 +805,14 @@ "It is common to use tuples as keys in dictionaries. For example, a telephone directory might map from last-name, first-name pairs to telephone numbers. Assuming that we have defined last, first and number, we could write:" ] }, + { + "cell_type": "markdown", + "id": "5a012731-cf42-487b-9d02-4a4b1d0475c0", + "metadata": {}, + "source": [ + "Es ist üblich, Tupel als Schlüssel in Wörterbüchern zu verwenden. Zum Beispiel könnte ein Telefonverzeichnis Nachname-Vorname-Paare auf Telefonnummern abbilden. Angenommen, wir haben last, first und number definiert, könnten wir schreiben:" + ] + }, { "cell_type": "markdown", "id": "e5462d35-fa68-42d8-ba00-c5dd1747c557", @@ -572,6 +829,14 @@ "### Named tuples" ] }, + { + "cell_type": "markdown", + "id": "7bd0a942-de38-4c9e-96df-a786fa814254", + "metadata": {}, + "source": [ + "### Benannte Tupel" + ] + }, { "cell_type": "markdown", "id": "e514c4bd-c17c-44f4-a89f-88154cc1bf6c", @@ -580,6 +845,14 @@ "Named tuples are a special type of tuple in Julia where each element has a specific name associated with it. This allows you to access elements by their names, making the code more readable and self-explanatory." ] }, + { + "cell_type": "markdown", + "id": "2284786f-cb8b-4173-944a-e50d79ceca2d", + "metadata": {}, + "source": [ + "Benannte Tupel sind ein besonderer Tupeltyp in Julia, bei dem jedem Element ein spezifischer Name zugeordnet ist. Dies ermöglicht es dir, auf Elemente anhand ihrer Namen zuzugreifen, was den Code lesbarer und selbsterklärender macht." + ] + }, { "cell_type": "code", "execution_count": null, @@ -598,6 +871,14 @@ "Named tuples are immutable, however it is possible to update the content, or to add new elements, by creating a new version. For example:" ] }, + { + "cell_type": "markdown", + "id": "01e99a5c-f331-4ad6-95e0-e403b8ad327b", + "metadata": {}, + "source": [ + "Benannte Tupel sind unveränderlich, aber es ist möglich, den Inhalt zu aktualisieren oder neue Elemente hinzuzufügen, indem eine neue Version erstellt wird. Zum Beispiel:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -618,6 +899,15 @@ " - Write a function `swap_first_last` that takes a tuple and returns a new tuple with the first and last elements swapped. Tip: use a combination of the `...` operator and of this slice `[2:end-1]`." ] }, + { + "cell_type": "markdown", + "id": "70061322-9435-496a-ac0d-c026f41e5936", + "metadata": {}, + "source": [ + "### Exercises\n", + " - Write a function `swap_first_last` that takes a tuple and returns a new tuple with the first and last elements swapped. Tip: use a combination of the `...` operator and of this slice `[2:end-1]`." + ] + }, { "cell_type": "code", "execution_count": null, @@ -634,6 +924,14 @@ "## Objects and structs" ] }, + { + "cell_type": "markdown", + "id": "5629a477-5d19-43a2-8bb4-e10b10f2ffde", + "metadata": {}, + "source": [ + "## Objekte und Strukturen" + ] + }, { "cell_type": "markdown", "id": "00b9bdb1-86ad-47f6-b8c3-88e85878eaff", @@ -651,6 +949,23 @@ "Here we will investigate the third option." ] }, + { + "cell_type": "markdown", + "id": "18412e4f-e5fb-4a92-a083-ac2e5b58cde3", + "metadata": {}, + "source": [ + "Zu diesem Zeitpunkt weißt du bereits, wie man Funktionen verwendet, um Code zu organisieren, und integrierte Typen, um Daten zu organisieren. Der nächste Schritt ist zu lernen, wie man eigene Typen erstellt, um sowohl Code als auch Daten zu organisieren.\n", + "\n", + "Wir haben viele der integrierten Typen von Julia verwendet; jetzt werden wir einen neuen Typ definieren. Als Beispiel werden wir einen Typ namens Punkt erstellen, der einen Punkt im zweidimensionalen Raum repräsentiert.\n", + "\n", + "Es gibt verschiedene Möglichkeiten, wie wir Punkte in Julia darstellen könnten:\n", + " - Wir könnten die Koordinaten separat in zwei Variablen, x und y, speichern.\n", + " - Wir könnten die Koordinaten als Elemente in einem Tupel speichern.\n", + " - Wir könnten einen neuen Typ erstellen, um Punkte als Objekte darzustellen.\n", + "\n", + "Hier werden wir die dritte Option untersuchen." + ] + }, { "cell_type": "markdown", "id": "9aa53d46-033b-4559-a5d8-e95357a257a5", @@ -659,6 +974,14 @@ "A programmer-defined composite type is also called a struct. The struct definition for a point looks like this:" ] }, + { + "cell_type": "markdown", + "id": "270c430a-4157-4e54-bb27-660c4028e5c6", + "metadata": {}, + "source": [ + "Ein vom Programmierer definierter zusammengesetzter Typ wird auch als Struktur (engl. struct) bezeichnet. Die Strukturdefinition für einen Punkt sieht so aus:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -682,6 +1005,16 @@ "A struct is like a factory for creating objects. To create a point, you call Point as if it were a function having as arguments the values of the fields. When Point is used as a function, it is called a constructor. The constructor returns an instance of the object." ] }, + { + "cell_type": "markdown", + "id": "6b99219f-70ba-41a7-8c52-e3803a360087", + "metadata": {}, + "source": [ + "Die Kopfzeile zeigt an, dass die neue Struktur Point genannt wird. Der Körper definiert die Attribute oder Felder der Struktur. Die Point-Struktur hat zwei Felder: x und y.\n", + "\n", + "Eine Struktur ist wie eine Fabrik zur Erzeugung von Objekten. Um einen Punkt zu erstellen, rufst du Point auf, als wäre es eine Funktion, und übergibst die Werte der Felder als Argumente. Wenn Point als Funktion verwendet wird, wird dies Konstruktor genannt. Der Konstruktor gibt eine Instanz des Objekts zurück.\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -701,6 +1034,14 @@ "The type speficication (`::Real`) is optional, but can be helpful to enforce correct usage of the struct." ] }, + { + "cell_type": "markdown", + "id": "e35cbbf5-491e-44ff-b2c0-0cf6a8853083", + "metadata": {}, + "source": [ + "Die Typangabe (`::Real`) ist optional, kann aber hilfreich sein, um die korrekte Verwendung der Struktur zu erzwingen." + ] + }, { "cell_type": "code", "execution_count": null, @@ -719,6 +1060,14 @@ "The values of the fields can be accessed using the `.` operator." ] }, + { + "cell_type": "markdown", + "id": "b86615f4-592f-41f7-b9d1-f5d1573cfd86", + "metadata": {}, + "source": [ + "Die Werte der Felder können mithilfe des `.` Operators abgerufen werden." + ] + }, { "cell_type": "code", "execution_count": null, @@ -738,6 +1087,14 @@ "Structs are however by default immutable, after construction the fields can not change value:" ] }, + { + "cell_type": "markdown", + "id": "7c6baa9c-56e2-4595-9e6b-c798d9cea002", + "metadata": {}, + "source": [ + "Die Werte der Felder können mithilfe des `.` Operators abgerufen werden." + ] + }, { "cell_type": "code", "execution_count": null, @@ -756,6 +1113,14 @@ "Where required, mutable composite types can be declared with the keyword mutable struct. Here is the definition of a mutable point:" ] }, + { + "cell_type": "markdown", + "id": "48b46edb-b8fd-46ca-a2ec-4aa0acbf6c01", + "metadata": {}, + "source": [ + "Wenn erforderlich, können veränderbare zusammengesetzte Typen mit dem Schlüsselwort `mutable struct` deklariert werden. Hier ist die Definition eines veränderbaren Punkts:\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -780,6 +1145,14 @@ "A third option is to let some fields of an unmutable struct to be mutable. For example a dictionary inside an unmutable struct can be modified." ] }, + { + "cell_type": "markdown", + "id": "06f44f63-bf8e-45f5-9495-006e21fc7560", + "metadata": {}, + "source": [ + "Eine dritte Option ist es, einige Felder einer unveränderlichen Struktur als veränderbar zu definieren. Zum Beispiel kann ein Wörterbuch innerhalb einer unveränderlichen Struktur geändert werden." + ] + }, { "cell_type": "code", "execution_count": null, @@ -807,6 +1180,14 @@ "You can pass an instance as an argument in the usual way. For example:" ] }, + { + "cell_type": "markdown", + "id": "53aa5851-35c2-4dd0-a1e0-b5bc8e21b9f6", + "metadata": {}, + "source": [ + "Du kannst eine Instanz auf die übliche Weise als Argument übergeben. Zum Beispiel:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -844,6 +1225,14 @@ "Functions can return instances as return values." ] }, + { + "cell_type": "markdown", + "id": "916562a2-3402-41bb-b15d-01333915df99", + "metadata": {}, + "source": [ + "Funktionen können Instanzen als Rückgabewerte zurückgeben." + ] + }, { "cell_type": "code", "execution_count": null, @@ -873,6 +1262,16 @@ " - Write a function called `point_distance` which takes two points as arguments and returns the Euclidean distance between them." ] }, + { + "cell_type": "markdown", + "id": "4afa9ca0-e6cd-4713-8caa-689c9dfdee42", + "metadata": {}, + "source": [ + "### Übung\n", + "\n", + " - Schreibe eine Funktion namens `point_distance`, die zwei Punkte als Argumente entgegennimmt und die euklidische Distanz zwischen ihnen zurückgibt." + ] + }, { "cell_type": "code", "execution_count": null, @@ -892,6 +1291,16 @@ "Each object (instance of a struct) is stored at some memory address. The operator `===` checks if two variables point to the same memory address of the object. For example" ] }, + { + "cell_type": "markdown", + "id": "eecb6161-768e-4ae6-a600-d892894f8824", + "metadata": {}, + "source": [ + "### Referenzen und Werte\n", + "\n", + "Jedes Objekt (Instanz einer Struktur) wird an einer bestimmten Speicheradresse gespeichert. Der Operator `===` überprüft, ob zwei Variablen auf die gleiche Speicheradresse des Objekts zeigen. Zum Beispiel" + ] + }, { "cell_type": "code", "execution_count": null, @@ -911,6 +1320,14 @@ "This means that any change made to `book_copy` will also modify `book`." ] }, + { + "cell_type": "markdown", + "id": "582e0ac0-2304-429e-a678-c37ab157d5d6", + "metadata": {}, + "source": [ + "Das bedeutet, dass jede Änderung, die an `book_copy` vorgenommen wird, auch `book` modifiziert." + ] + }, { "cell_type": "code", "execution_count": null, @@ -932,6 +1349,14 @@ "If a new, distinct, object is needed (in other words, a copy by value), we can use the function `deepcopy`. " ] }, + { + "cell_type": "markdown", + "id": "79339467-8994-4a5c-8987-3420d14cb290", + "metadata": {}, + "source": [ + "If a new, distinct, object is needed (in other words, a copy by value), we can use the function `deepcopy`. " + ] + }, { "cell_type": "code", "execution_count": null, @@ -951,6 +1376,14 @@ "Finally, the `==` operator between structs defaults to the `===` operator as Julia has no way of knowing how to compare custom structs. However, it is always possible to reimplement the `==` operator for our custom types." ] }, + { + "cell_type": "markdown", + "id": "4cc4c096-d856-4d43-8808-94edcd3ad906", + "metadata": {}, + "source": [ + "Schließlich entspricht der `==` Operator zwischen Strukturen standardmäßig dem `===` Operator, da Julia nicht weiß, wie benutzerdefinierte Strukturen verglichen werden sollen. Es ist jedoch immer möglich, den `==` Operator für unsere benutzerdefinierten Typen neu zu implementieren." + ] + }, { "cell_type": "code", "execution_count": null, @@ -976,6 +1409,14 @@ "With this we can compare if two books are the same, even if they are two distinct instances of the same struct." ] }, + { + "cell_type": "markdown", + "id": "02b533ba-2325-488a-838b-94cdf5ca25bb", + "metadata": {}, + "source": [ + "Damit können wir vergleichen, ob zwei Bücher gleich sind, auch wenn es sich um zwei unterschiedliche Instanzen desselben Typs handelt." + ] + }, { "cell_type": "markdown", "id": "9263a1ae-52ee-4afc-9d9f-0f1341b64bc0", @@ -984,6 +1425,14 @@ "## Pure and impure functions" ] }, + { + "cell_type": "markdown", + "id": "4e457ee2-813a-42a8-8ee0-600bb4b50812", + "metadata": {}, + "source": [ + "## Reine und unreine Funktionen" + ] + }, { "cell_type": "markdown", "id": "f638e9b3-9926-46fc-81e0-d49c877a3bbb", @@ -996,6 +1445,18 @@ "Let's consider a modified version of the book for an example." ] }, + { + "cell_type": "markdown", + "id": "fb008ae0-a457-45db-8f77-425d56e95b30", + "metadata": {}, + "source": [ + "Wir haben gesehen, dass es ziemlich praktisch ist, Objekte an Funktionen zu übergeben. Bisher haben wir Funktionen gesehen, die den Inhalt ausgeben oder ein neues Objekt erstellen. Keine dieser Funktionen ändert den Inhalt der Eingabeobjekte.\n", + " - Eine Funktion, die die Eingabe nicht ändert, wird auch als **rein** bezeichnet.\n", + " - Eine Funktion, die die Eingabe ändert, wird als **unrein** bezeichnet. In Julia werden unreine Funktionen durch Hinzufügen eines `!` als letztem Zeichen des Namens hervorgehoben.\n", + "\n", + "Betrachten wir eine modifizierte Version des Buchs als Beispiel." + ] + }, { "cell_type": "code", "execution_count": null, @@ -1015,6 +1476,14 @@ "Then we could think of a function which updates the database whenever we order new copies." ] }, + { + "cell_type": "markdown", + "id": "7dac83b5-1086-4ca9-9b36-827497825be6", + "metadata": {}, + "source": [ + "Dann könnten wir über eine Funktion nachdenken, die die Datenbank aktualisiert, wenn wir neue Exemplare bestellen." + ] + }, { "cell_type": "code", "execution_count": null, @@ -1036,18 +1505,20 @@ }, { "cell_type": "markdown", - "id": "93bc2e46-8530-4d1e-b7fa-55a74d1831c8", + "id": "b30bd560-5ff3-4706-9f53-e6a5158651e9", "metadata": {}, "source": [ - "## Exercise" + "### Exercise\n", + " - Write an inpure function `sell_book!` for updating the book object whenever we sell a single copy. The function should complain if there are no copies available." ] }, { "cell_type": "markdown", - "id": "b30bd560-5ff3-4706-9f53-e6a5158651e9", + "id": "93bc2e46-8530-4d1e-b7fa-55a74d1831c8", "metadata": {}, "source": [ - "Write an inpure function `sell_book!` for updating the book object whenever we sell a single copy. The function should complain if there are no copies available." + "### Übung\n", + " - Schreibe eine unreine Funktion `sell_book!`, um das Buchobjekt zu aktualisieren, wenn wir ein Exemplar verkaufen. Die Funktion sollte eine Meldung ausgeben, wenn keine Exemplare verfügbar sind." ] }, { @@ -1066,6 +1537,14 @@ "## Further exercises" ] }, + { + "cell_type": "markdown", + "id": "d5bb0b58-e85e-4edb-80b6-19d2c7d49ac3", + "metadata": {}, + "source": [ + "## Weitere Übungen" + ] + }, { "cell_type": "markdown", "id": "8446f1db-d40f-4311-9597-3b3b4d913151", @@ -1074,6 +1553,14 @@ "- Create a time struct which stores the time of the day (hour, minute and second). Write a function which takes as input the time object and prints it. Then write a function which sums together two time objects and returns a new time object. If the total is more than 24 hours, it should reset as if it was the next day." ] }, + { + "cell_type": "markdown", + "id": "15fd6da2-f862-40ef-a58d-5e7be488565f", + "metadata": {}, + "source": [ + "- Erstelle eine Zeitstruktur, die die Uhrzeit des Tages (Stunde, Minute und Sekunde) speichert. Schreibe eine Funktion, die als Eingabe das Zeitobjekt nimmt und es ausgibt. Schreibe dann eine Funktion, die zwei Zeitobjekte zusammenzählt und ein neues Zeitobjekt zurückgibt. Wenn die Summe mehr als 24 Stunden beträgt, sollte sie wie am nächsten Tag zurückgesetzt werden." + ] + }, { "cell_type": "code", "execution_count": null, @@ -1090,6 +1577,14 @@ " - Create a custom version of the `==` function which checks if two time objects are the same." ] }, + { + "cell_type": "markdown", + "id": "2e0fde12-1214-4709-a7cb-5ebfaa0af17a", + "metadata": {}, + "source": [ + "- Erstelle eine benutzerdefinierte Version der `==` Funktion, die überprüft, ob zwei Zeitobjekte identisch sind." + ] + }, { "cell_type": "code", "execution_count": null, @@ -1106,6 +1601,14 @@ " - Create a custom version of the `<` and `<=` functions for time objects." ] }, + { + "cell_type": "markdown", + "id": "4e8c3c1e-14fb-439f-97c7-076196f7e5b4", + "metadata": {}, + "source": [ + "- Erstelle eine benutzerdefinierte Version der `<` und `<=` Funktionen für Zeitobjekte." + ] + }, { "cell_type": "code", "execution_count": null, @@ -1119,7 +1622,15 @@ "id": "1bdbd748-7482-4f54-99a7-3d6e74b65575", "metadata": {}, "source": [ - " - Create a custom version of the `<` and `<=` functions for time objects." + " - Create a custom version of the `>` and `>=` functions for time objects." + ] + }, + { + "cell_type": "markdown", + "id": "b79aad8c-647e-46d0-9b04-6a77cd219953", + "metadata": {}, + "source": [ + "- Erstelle eine benutzerdefinierte Version der `>` und `>=` Funktionen für Zeitobjekte." ] }, {