Commit 95ba575a authored by Andreas Pfurtscheller's avatar Andreas Pfurtscheller

Add HW05 馃搫

parent 86b5c9c4
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Assignment 5.1 (P): Getting Started\n",
"\n",
"Have a quick look at the following OCaml tools.\n",
"\n",
"## REPLs (Read Eval Print Loops)\n",
"\n",
"### ocaml\n",
"\n",
"Simply type your OCaml code. If you want the REPL to execute your code, type `;;`. Execute the content of a .ml file with `#use \"<filename>\"`. To exit the REPL enter `#quit` or type `Ctrl + D`. Note that `#use`, `quit` as well as `;;` are special sequences recognized by the REPL and are not part of OCaml (so do not use them in a .ml file).\n",
"\n",
"### utop\n",
"\n",
"utop works in the same way as ocaml, but is more comfortable as it comes with autocompletion, for example, and allows you to change or redo previously typed code much more easily.\n",
"\n",
"## IDEs\n",
"\n",
"### Visual Studio Code\n",
"\n",
"Visual Studio Code is a free full-fledged integrated development environment for a lot of programming languages, due to its great extensibility and rich set of existing extensions. The OCaml plugin together with merlin and ocp-indent provides great utilities\n",
"\n",
"* syntax highlighting\n",
"* autocompletion\n",
"* smart identation\n",
"* peek and goto definition\n",
"* type information \n",
"* immediate error reports\n",
"* integrated terminal\n",
"* and much more ...\n",
"\n",
"## Compilers\n",
"\n",
"### ocamlc\n",
"\n",
"ocamlc is a basic ocaml compiler. When invoked as `ocamlc <file-0> ... <file-n>` it compiles all files into an executable named `a.out`. During this process, `.cmi` and `.cmo` files are generated, representing the compiled interface and object files respectively. Check the documentation (http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual022.html#toc86) for a list of all command line options and more advanced build steps.\n",
"\n",
"### ocamlbuild\n",
"\n",
"ocamlbuild is a more convenient compilation utility, as it resolves and compiles dependencies automatically. In order to build your project with ocamlbuild, simply compile your main file `<file>.ml` with either `ocamlbuild <file>.d.byte` or `ocambuild <file>.native` to produce bytecode (with debug information) or native code, respectively. Check the documentation (https://ocaml.org/learn/tutorials/ocamlbuild/) for more details.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Assignment 5.2 (P) Expressions\n",
"\n",
"So far, you learned about the following types of expressions:\n",
"* Constants\n",
"* Variables\n",
"* Unary operators\n",
"* Binary operators\n",
"* Tuples\n",
"* Records\n",
"* Lists\n",
"* If-then-else\n",
"* Pattern matching\n",
"* Function definition\n",
"* Function application\n",
"* Variable binding\n",
"\n",
"Perform the following tasks:\n",
"1. For each of these expressions, give the general structure and two concrete examples with different subexpressions."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. For these expression, list all contained subexpressions and give their corresponding type. Then evaluate the expression:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(* a *) let a = fun x y -> x + 2 in a 3 8 :: []"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(* b *) ((fun x -> x::[]) (9 - 5), true, ('a', 7))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Assignment 5.3 (P): What's the point?\n",
"\n",
"In this assignment, you are supposted to implement some functionalities to compute with 3-dimensional vectors. \n",
"\n",
"1. Define a suitable data type for your point."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"type vector3 = (float*float*float);;\n",
"type vector3r = {x:int;y:int;z:int};;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Define three points `p1`, `p2` and `p3` with different values."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(* TODO *)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Implement a function `vector3_to_string : vector3 -> string` to convert a vector into a human-readable representation."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(* TODO *)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Write a function `vector3_add : vector3 -> vector3 -> vector3` to add two vectors."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"let vector3_add = (* TODO *)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5. Write a function `vector3_max : vector3 -> vector3 -> vector3` that returns the larger vector (the vector with the greater magnitude)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"let vector3_max = (* TODO *)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"6. Compute the result of adding `p1` to the larger of `p2` and `p3` and print the result as a string."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(* TODO *)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Assignment 5.4 (P) Student Database\n",
"\n",
"In this assignment, you have to manage the students of a university.\n",
"\n",
"1. Define a data type for a student. For every student the first name, last name, identification number, current semester as well as the grades received in different courses have to be stored. A course is simply represented by a number."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"type student = (* TODO *)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The collection of students is now defined as a list of students: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"type database = student list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Write a function `insert : student -> database -> database` that inserts a student into the database."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"let insert = (* TODO *)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Write a function `find_by_id : int -> db -> student list` that returns a list with all students with the given id (either a single student or an empty list, if no such student exists)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"let rec find_by_id = (* TODO *) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Implement a function `find_by_last_name : string -> database -> student list` to find all students with a given last name."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"let rec find_by_last_name = (* TODO *)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Homework Assignments\n",
"\n",
"The homework assignments can be found in a separate pdf file."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "OCaml default",
"language": "OCaml",
"name": "ocaml-jupyter"
},
"language_info": {
"codemirror_mode": "text/x-ocaml",
"file_extension": ".ml",
"mimetype": "text/x-ocaml",
"name": "OCaml",
"nbconverter_exporter": null,
"pygments_lexer": "OCaml",
"version": "4.07.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
......@@ -77,18 +77,40 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"- : int list = [5]\n"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(* a *) let a = fun x y -> x + 2 in a 3 8 :: []"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"- : int list * bool * (char * int) = ([4], true, ('a', 7))\n"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(* b *) ((fun x -> x::[]) (9 - 5), true, ('a', 7))"
]
......@@ -106,11 +128,33 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"type vector3 = float * float * float\n"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"type vector3r = { x : float; y : float; z : float; }\n"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type vector3 = (* TODO *)"
"type vector3 = (float*float*float);;\n",
"type vector3r = {x:float;y:float;z:float};;"
]
},
{
......@@ -122,11 +166,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"val p1 : vector3 = (1., 2., 3.)\n"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"val p2 : vector3r = {x = 3.; y = 4.; z = 5.}\n"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"val p3 : vector3 = (6., 7., 8.)\n"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(* TODO *)"
"let p1: vector3 = (1.0, 2.0, 3.0);;\n",
"let p2: vector3 = (3.0, 4.0, 5.0);;\n",
"let p3: vector3 = (6.0,7.0,8.0);;"
]
},
{
......@@ -138,11 +215,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"val vector3_to_string : float * float * float -> string = <fun>\n"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : string = \"(1., 2., 3.)\"\n"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(* TODO *)"
"let vector3_to_string (x,y,z) = \"(\" ^ string_of_float x ^ \", \" ^ string_of_float y ^ \", \" ^ string_of_float z ^ \")\";;\n",
"\n",
"vector3_to_string (p1);;"
]
},
{
......@@ -154,11 +254,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"val vector3_add :\n",
" float * float * float -> float * float * float -> float * float * float =\n",
" <fun>\n"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : string = \"(7., 9., 11.)\"\n"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"let vector3_add = (* TODO *)"
"let vector3_add (x0,y0,z0) (x1,y1,z1) = (x0+.x1,y0+.y1,z0+.z1);;\n",
"vector3_to_string (vector3_add p1 p3);;"
]
},
{
......@@ -170,11 +294,46 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"val length : float * float * float -> float = <fun>\n"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"val vector3_max :\n",
" float * float * float -> float * float * float -> float * float * float =\n",
" <fun>\n"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"- : string = \"(6., 7., 8.)\"\n"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"let vector3_max = (* TODO *)"
"let length (x,y,z) = (x**2.0 +. y**2.0 +. z**2.0)**0.5;;\n",
"let vector3_max a b = if length a > length b then a else b;;\n",
"vector3_to_string (vector3_max p1 p3);;"
]
},
{
......@@ -186,11 +345,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "error",
"evalue": "compile_error",
"output_type": "error",
"traceback": [
"\u001b[32mFile \"[23]\", line 1, characters 28-30:\n\u001b[31mError: This expression has type vector3r\n but an expression was expected of type float * float * float\n\u001b[36m 1: \u001b[30mvector3_add p1 (vector3_max \u001b[4mp2\u001b[0m\u001b[30m p3)\u001b[0m\n"
]
}
],
"source": [
"(* TODO *)"
"vector3_add p1 (vector3_max p2 p3)"
]
},
{
......@@ -206,11 +374,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 25,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"type student = {\n",
" first_name : string;\n",
" last_name : string;\n",
" id : int;\n",
" semester : int;\n",
" grades : (int * float) list;\n",
"}\n"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type student = (* TODO *)"
"type student = {first_name: string; last_name: string; id: int; semester: int; grades: (int*float) list};;"
]
},
{
......@@ -222,9 +407,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 26,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"type database = student list\n"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type database = student list"
]
......@@ -238,11 +434,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 27,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"val insert : 'a -> 'a list -> 'a list = <fun>\n"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"let insert = (* TODO *)"
"let insert e db = e::db;;"
]
},
{
......@@ -254,11 +461,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"val find_by_id : int -> student list -> student list = <fun>\n"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"let rec find_by_id = (* TODO *) "
"let rec find_by_id id db = match db with\n",
" | [] -> []\n",
" | h::tl -> if h.id=id then [h] else find_by_id id tl;;"
]
},
{
......@@ -270,11 +490,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 34,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"val find_by_last_name : string -> student list -> student list = <fun>\n"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"let rec find_by_last_name = (* TODO *)"
"let rec find_by_last_name name db = match db with\n",
" | [] -> []\n",
" | h::tl -> if h.last_name=name then h::(find_by_last_name name tl) else (find_by_last_name name tl);;"
]
},
{
......@@ -289,7 +522,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "OCaml 4.05.0",
"display_name": "OCaml default",
"language": "OCaml",
"name": "ocaml-jupyter"
},
......@@ -300,7 +533,7 @@
"name": "OCaml",
"nbconverter_exporter": null,
"pygments_lexer": "OCaml",
"version": "4.05.0"
"version": "4.07.1"
}
},
"nbformat": 4,
......
......@@ -28,35 +28,100 @@ let rec find_by_last_name name db = match db with [] -> []
(*****************************************************************************)
(* Assignment 5.5 [6 Points] *)
let remove_by_id id db = todo()
let count_in_semester sem db = todo()
let student_avg_grade id db = todo()
let course_avg_grade course db = todo()
let rec remove_by_id id db = match db with
| [] -> []
| h::tl -> if h.id==id
then tl
else h::remove_by_id id tl
let rec count_in_semester sem db = match db with
| [] -> 0