Done with 3-5 and 3-6 from hw1

Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
Mariano Uvalle 2025-01-27 13:48:06 -08:00
parent 226956072a
commit bd50dad69b

View file

@ -742,7 +742,12 @@ let rev_t (l : 'a list) : 'a list =
evaluates to true or false. evaluates to true or false.
*) *)
let rec insert (x : 'a) (l : 'a list) : 'a list = let rec insert (x : 'a) (l : 'a list) : 'a list =
failwith "insert unimplemented" match l with
| [] -> [x]
| h :: hs ->
if x < h then x :: l
else if x = h then l
else h :: insert x hs
(* (*
Problem 3-6 Problem 3-6
@ -752,7 +757,10 @@ let rec insert (x : 'a) (l : 'a list) : 'a list =
Hint: you might want to use the insert function that you just defined. Hint: you might want to use the insert function that you just defined.
*) *)
let rec union (l1 : 'a list) (l2 : 'a list) : 'a list = let rec union (l1 : 'a list) (l2 : 'a list) : 'a list =
failwith "union unimplemented" (* Ideally you'd insert the smallest list into the larger one, but I'll go with the simplest implementation here *)
match l1 with
| [] -> l2
| x :: xs -> union xs (insert x l2)
(******************************************************************************) (******************************************************************************)
(* *) (* *)