;; Aufgabe 1 ;; --------- let min3 a b c = if a <= b then if a <= c then a else c else if b <= c then b else c;; min3 4 5 3;; ;; Aufgabe 2 ;; --------- let rec minList lst = match lst with [elt] -> elt | hd :: tl -> if hd <= minList tl then hd else minList tl;; minList [3;5;2;4];; minList [];; minList [4];; ;; Aufgabe 3 ;; --------- let xor a b = if (a && not b) or (b && not a) then true else false;; xor false false;; xor false true;; xor true false;; xor true true;; ;; Aufgabe 4 ;; --------- let mitternacht a b c = (((sqrt (b *. b -. (4.0 *. (a *. c))) -. b) /. (2.0 *. a)) , (((-1.0) *. (sqrt (b *. b -. (4.0 *. (a *. c)))) -. b) /. (2.0 *. a)) );; mitternacht 3. 1. 1.;; mitternacht 1. 0. (-1.0);; ;; Aufgaben 5 und 6 ;; --------- let rec print_string_list strlst = match strlst with [] -> () | hd ::tl -> print_string hd ; print_newline () ; print_string_list tl;; print_string_list [ "sieht" ; "das" ; "gut" ; "aus?" ];; ;; Aufgabe 7 ;; --------- let rec fac n = if n = 0 then 0 else if n = 1 then 1 else n * fac (n-1);; fac 3;; ;; Aufgabe 8 ;; --------- let rec power a n = if n = 0 then 1 else a * (power a (n - 1));; power 3 2;; ;; Aufgabe 9 ;; --------- let fst (a,b) = a;; fst (3,5);; let snd (a,b) = b;; snd (3,5);; ;; Aufgabe 10 ;; ---------- let tripelfst (a,b,c) = a;; let tripelsnd (a,b,c) = b;; let tripelthrd (a,b,c) = c;; tripelfst (3,4,5);; tripelsnd (3,4,5);; tripelthrd (3,4,5);; ;; Aufgabe 11 ;; ---------- ;; Aufgabe 12 ;; ---------- let rec duplicate l = match l with [] -> [] | hd :: tl -> hd::hd::(duplicate tl);; duplicate [ 1 ; 3 ; 2 ; 4 ; 3 ; 2 ];; let rec adjoin el lst = match lst with [] -> [el] | hd::tl -> if hd = el then lst else hd::(adjoin el tl);; adjoin 3 [ 4 ; 3 ; 5 ];; adjoin 3 [ 4 ; 2 ; 5 ];; ;; Hilfsfunktion rev dreht Liste um 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];; ;; Hilfsfunktion append haengt zwei Listen aneinander 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 splitAt2 accu1 accu2 n = if n = 0 then (accu1, accu2) else splitAt2 (append accu1 [List.hd accu2]) (List.tl accu2) (n - 1);; let splitAt lst n = if n = 0 then ([],lst) else splitAt2 ([List.hd lst]) (List.tl lst) (n - 1);; splitAt [1; 2; 3; 4; 5; 6; 7] 3;; ;; Aufgabe 13 ;; ---------- let rec merge l1 l2 = match l1 with [] -> l2 | hd1 :: tl1 -> match l2 with [] -> l1 | hd2 :: tl2 -> if hd1 < hd2 then hd1 :: hd2 :: (merge tl1 tl2) else hd2 :: hd1 :: (merge tl1 tl2);; let rec mergeSort lst = match lst with [] -> [] | [elt] -> [elt] | hd :: tl -> let (l1, l2) = splitAt lst ((List.length lst) / 2) in merge (mergeSort l1) (mergeSort l2);; mergeSort [2; 4; 3; 5; 1; 6];;