complete lab04

This commit is contained in:
SowinskiBraeden committed 2026-02-05 16:11:52 -08:00
1 parent 2da8e0df6c
commit 386b6f658a
4 files changed
+54 -2

No files matched your search

+1 -1
View File
@@ -3,4 +3,4 @@
*.zip
*.native
*/_build/*
_build/*
+1 -1
View File
@@ -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]);
+5
View File
@@ -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
+47
View File
@@ -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 <filename>\"\n" Sys.argv.(0) Sys.argv.(0)
else
let ic = open_in Sys.argv.(1) in
print_records @@ records_insertion_sort @@ read_file [] ic;;