;; Aufgabe 1 fun a b c -> if a <= b then if a <= c then a else c else if b <= c then b else c;; (fun a b c -> if a <= b then if a <= c then a else c else if b <= c then b else c) 5 3 4;; fun ab -> if (a && not b) or (b && not a) then true else false;; (fun a b -> if (a && not b) or (b && not a) then true else false) true false;; ;; Aufgabe 2 type ratio = {num: int; denum: int};; let mult_ratio r1 r2 = {num = r1.num * r2.num; denum = r1.denum * r2.denum};; mult_ratio {num = 2; denum = 3} {num = 1; denum = 2};; exception DivisionByZero;; let div_ratio r1 r2 = if r2.num = 0 then raise DivisionByZero else {num = r1.num * r2.denum; denum = r1.denum * r2.num};; div_ratio {num = 2; denum = 3} {num = 1; denum = 2};; div_ratio {num = 2; denum = 3} {num = 0; denum = 2};; ;; Aufgabe 3 let rec iter f lst = match lst with [] -> () | hd :: tl -> f hd; iter f tl;; iter (fun x -> x + 1) [1; 2; 3];; let rec rev2 accu lst = match lst with [] -> accu | hd :: tl -> rev2 (hd :: accu) tl;; let rev lst = match lst with [] -> [] | hd :: tl -> rev2 [hd] tl;; rev [1; 2; 3; 4];; let rec append2 l1 l2 = match l1 with [] -> l2 | hd :: tl -> append2 tl (hd :: l2) let append l1 l2 = let l1r = rev l1 in append2 l1r l2;; append [1; 2] [3; 4; 5];; let rec flatten lst = match lst with [] -> [] | hd :: tl -> append hd (flatten tl);; flatten [[1; 2]; [3; 4]; [5; 6]];; flatten [[1; 2; 3]; []; [4; 5]; [6]];; exception Not_found;; let rec find pred lst = match lst with [] -> raise Not_found | hd :: tl -> if pred hd then hd else find pred tl;; find (fun x -> if x mod 2 = 0 then true else false) [1; 2; 3; 4; 5];; ;; Aufgabe 4 let alist_cons key value alist = (key, value) :: alist;; let rec assoc searchkey alist = match alist with [] -> raise Not_found | hd :: tl -> match hd with (key, value) -> if key = searchkey then value else assoc searchkey tl;; let rec alist_delete2 searchkey accu alist = match alist with [] -> accu | hd :: tl -> match hd with (key, value) -> if key = searchkey then alist_delete2 searchkey accu tl else alist_delete2 searchkey (hd :: accu) tl;; let alist_delete searchkey alist = alist_delete2 searchkey [] alist;; alist_delete 2 [(1, 'a'); (2, 'b'); (17, 'd'); (2, 'x'); (4, 'y')];; ;; Aufgabe 5 type 'a cell = {mutable cell: 'a};; let set value cell = cell.cell <- value;; let get cell = cell.cell;; ;; Aufgabe 6 type season = Spring of int | Summer of int | Fall of int | Winter of int;; let begin_of_spring = 61;; let end_of_spring = 150;; let begin_of_summer = 151;; let end_of_summer =240;; let begin_of_fall =241;; let end_of_fall = 330;; let begin_of_winter = 331;; let end_of_winter = 60;; exception Not_a_day_of_year;; let int_to_season day_of_year = if day_of_year >= begin_of_spring && day_of_year < end_of_spring then Spring(day_of_year) else if day_of_year >= begin_of_summer && day_of_year < end_of_summer then Summer(day_of_year) else if day_of_year >= begin_of_fall && day_of_year < end_of_fall then Fall(day_of_year) else if day_of_year >= begin_of_winter or day_of_year < begin_of_spring then Winter(day_of_year) else raise Not_a_day_of_year;; let season_to_int season = match season with Spring(d) -> d | Summer(d) -> d | Fall(d) -> d | Winter(d) -> d;; season_to_int (int_to_season 33);; ;; Aufgabe 7 type 'a btree = Leaf of 'a | Node of 'a btree * 'a btree;; let rec depth btree = match btree with Leaf(_) -> 0 | Node(left, right) -> 1 + max (depth left) (depth right);; let rec flat btree = match btree with Leaf(a) -> [a] | Node(left, right) -> append (flat left) (flat right);; ;; Aufgabe 8 ;; vbtree ist der Datentyp fuer den Vorlesungs-btree type 'a vbtree = Empty | Node of 'a * 'a vbtree * 'a vbtree;; let rec mapTree f vbtree = match vbtree with Empty -> Empty | Node(a, left, right) -> Node(f a, mapTree f left, mapTree f right);; ;; Aufgabe 9 type 'a liste = Leereliste | Zusammensetzung of 'a * 'a liste;; let rec listenlaenge lst = match lst with Leereliste -> 0 | Zusammensetzung(a,rest) -> 1 + listenlaenge rest;; let eineListe = Zusammensetzung(1, Zusammensetzung(2, Leereliste));; listenlaenge eineListe;; ;; Aufgabe 10 let rec fold_right f l u = match l with [] -> u | hd :: tl -> f hd (fold_right f tl u);; fold_right (fun x y -> x + y) [1; 2; 3] 0;; fold_right (fun x y -> x :: y) ['a'; 'b'; 'c'] [];; ;; Aufgabe 11 ;; Aufgabe 12