diff --git a/hw1/bin/hellocaml.ml b/hw1/bin/hellocaml.ml index b2de40a..1687cca 100644 --- a/hw1/bin/hellocaml.ml +++ b/hw1/bin/hellocaml.ml @@ -742,7 +742,12 @@ let rev_t (l : 'a list) : 'a list = evaluates to true or false. *) 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 @@ -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. *) 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) (******************************************************************************) (* *)