diff --git a/.gitignore b/.gitignore index 0021882..b8c1b96 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ *.zip *.native */_build/* - +_build/* diff --git a/lab02/lab2.ml b/lab02/lab2.ml index c063b5f..7513714 100644 --- a/lab02/lab2.ml +++ b/lab02/lab2.ml @@ -99,7 +99,7 @@ let test_filter () = let every n l = filteri (fun i _ -> (i + 1) mod n = 0) l;; (**/**) -let test_every() = +let test_every () = assert (every 1 [] = []); assert (every 2 [1] = []); assert (every 3 [1;2;3;4;5;6;7;8;9;10] = [3;6;9]); diff --git a/lab04/data.txt b/lab04/data.txt new file mode 100644 index 0000000..b671750 --- /dev/null +++ b/lab04/data.txt @@ -0,0 +1,5 @@ +homer simpson 25 # bad at nuclear engineering + ned flanders 12abc !! invalid + waylon smithers 100 ! high score + gary chalmers 5 +monty burns diff --git a/lab04/records.ml b/lab04/records.ml new file mode 100644 index 0000000..6c02ff7 --- /dev/null +++ b/lab04/records.ml @@ -0,0 +1,47 @@ +type record = {firstname: string; lastname: string; score: int};; + +let new_record f l s = {firstname = f; lastname = l; score = s};; + +let rec print_records r = + match r with + | [] -> () + | x :: xs -> Printf.printf "%3d %s %s\n" x.score x.lastname x.firstname; print_records xs;; + +(** [records_insert x l] inserts a record element [x] into + * list of records [l] + * Requires: [l] is in ascending order. *) +let rec records_insert x l = + match l with + | y :: ys when x.score < y.score -> y :: records_insert x ys + | _ -> x :: l + +(** [records_insertion_sort l] sorts list of records [l] in ascending order *) +let rec records_insertion_sort l = + match l with + | [] -> [] + | x :: xs -> records_insert x (records_insertion_sort xs) + +let rec parse l = + try + Scanf.sscanf l " %s %s %s" (fun f l s -> + match int_of_string_opt s with + | Some v when v >= 0 && v <= 100 -> Some {firstname = f; lastname = l; score = v} + | _ -> None + ) + with + | Scanf.Scan_failure _ -> None + +let rec read_file acc ic = + try + match parse @@ input_line ic with + | None -> read_file acc ic + | Some v -> read_file (v :: acc) ic + with + | End_of_file -> close_in ic; acc + +let () = + if Array.length Sys.argv = 1 then + Printf.printf "%s: expects 1 file input.\nUsage: \"%s \"\n" Sys.argv.(0) Sys.argv.(0) + else + let ic = open_in Sys.argv.(1) in + print_records @@ records_insertion_sort @@ read_file [] ic;;