text
stringlengths
0
601k
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (_, value) = (convert_speed a b_unit) in (b_unit, value +. b_val) ;;
let passes_da_vinci t = let rec sum_of_squares l acc = match l with | [] -> acc | Leaf :: tl -> sum_of_squares tl acc | Branch (width, subtree) :: tl -> sum_of_squares tl (acc +. (width ** 2.)) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not (check subtree && ((width ** 2.) >= sum_of_squares subtree 0.)) then false else check tl | _ -> false in match t with | Leaf -> check [t] | Branch (width, subtree) -> check [t] | _ -> false ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with |[] -> if cur_num>max_num then cur_el else max_el |hd::tl -> if hd = cur_el then aux tl (hd,cur_num+1) (max_el, max_num) else if cur_num > max_num then aux tl (hd,1) (cur_el, cur_num) else aux tl (hd,1) (max_el, max_num) in let listsorted = List.sort compare l in let xs = List.nth listsorted ((List.length listsorted)-1) in aux listsorted (xs,0) (xs,0);;
let pair_mode (l: 'a list) : 'a * 'a = let l1 = List.tl l in let l2 = List.rev (List.tl (List.rev l)) in let l3 = List.combine l2 l1 in mode l3 ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match(from_unit,to_unit) with | Second,Hour -> (to_unit, (val_/.3600.)) | Hour,Second -> (to_unit, (val_*.3600.)) |_->(to_unit, val_);;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match(from_unit,to_unit) with |Foot,Meter -> (to_unit, (val_*.0.3048)) |Foot,Mile -> (to_unit, (val_/.5280.)) |Meter,Foot-> (to_unit, (val_/.0.3048)) |Meter, Mile-> (to_unit, (val_/.1609.344)) |Mile,Foot-> (to_unit, (val_*.5280.)) |Mile,Meter-> (to_unit, (val_*.1609.344)) |_->(to_unit, val_) ;; let second_element ((a, b) : 'a * 'b) : 'b = b let first_element (pair : 'a * 'b) : 'a = let (first, _) = pair in first;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((dist1,time1),(dist2,time2)) = (from_unit, to_unit) in ((dist2,time2), (second_element (convert_time (time2, (second_element (convert_dist (dist1, val_) dist2))) time1))) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let b = second_element(speed_unit) in let timeval = convert_time (time) b in (first_element(speed_unit), speed_val*.second_element(timeval)) ;; let rec passes_da_vinci t = let rec sum_ l acc= match l with |[] -> acc |Branch (width,_)::tl -> sum_ tl (acc+.(width*.width)) |Leaf::tl -> sum_ tl acc in let rec check t2 = match t2 with |[]-> true |Leaf::tl -> check tl |Branch (width,subtree)::tl -> if (List.for_all passes_da_vinci subtree) && ((sum_ subtree 0.) <= (width*.width)) then check tl else false in check [t];;
let invalidInput () = failwith "Invalid input type";;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num>max_num then cur_el else max_el | hd::tl -> if hd=cur_el then aux (tl) (cur_el,cur_num+1) (max_el, max_num) else (if cur_num>max_num then aux (tl) (hd,1) (cur_el, cur_num) else aux (tl) (hd,1) (max_el, max_num) ) in match l with | [] -> invalidInput() | f::_ -> aux (List.sort compare l) (f,0) (f,0) ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] | [_] -> invalidInput() | f::_ -> let l1=match l with | e1::r ->r | o->o in let l2=match List.rev l with | e1::r->List.rev r | o->o in let l3=List.combine l2 l1 in mode(l3); ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit,to_unit) with | (Second, Hour) -> (to_unit,val_ /. 3600.) | (Hour, Second) -> (to_unit,val_ *. 3600.) | (_,_) -> (to_unit,val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit,to_unit) with | (Foot, Meter) -> (to_unit,val_ *. 0.3048) | (Foot, Mile) -> (to_unit,val_ /. 5280.) | (Meter, Foot) -> (to_unit,val_ /. 0.3048) | (Meter, Mile) -> ( to_unit, (val_ /. 0.3048) /.5280. ) | (Mile, Foot) -> (to_unit,val_ *. 5280.) | (Mile, Meter) -> (to_unit,val_ *. 5280. *. 0.3048) | (_,_) -> (to_unit,val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (a,b,c,d) = ((fst from_unit), (snd from_unit), (fst to_unit), (snd to_unit)) in ((to_unit), snd(convert_dist (a, val_) c ) /. snd(convert_time (b,1.) d ) );;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (a,b) = (a, b_unit) in (b, snd(convert_speed (fst(a),snd(a)) b) +. b_val) ;;
let passes_da_vinci (t:tree) : bool= let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, st) :: tl -> if not (check st && ((sum_ st 0.) <= width*.width) ) then false else check tl in check [t];;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | x :: xs -> if x = cur_el then if cur_num + 1 > max_num then aux xs (x, cur_num + 1) (x, cur_num + 1) else aux xs (x, cur_num + 1) (max_el, max_num) else aux xs (x, 1) (max_el, max_num) in let sorted: 'a list = List.sort compare l in match sorted with | [] -> failwith "Undefined input" | x :: xs -> aux xs (x, 1) (x,1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rem (l: 'a list) : 'a list = match l with | [] -> failwith "Invalid input" | x :: xs -> if xs == [] then failwith "Invalid input" else xs in let l1 = rem l in let l2 = List.rev (rem (List.rev l)) in let l3 = List.combine l2 l1 in mode l3 ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let from: time_unit = fst (from_unit, val_) in let valu: float = snd (from_unit, val_) in if from = to_unit then (to_unit, valu) else match from with | Hour -> (Second, (valu *. 3600.0)) | Second -> (Hour, (valu /. 3600.0)) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let from: dist_unit = fst (from_unit, val_) in let valu: float = snd (from_unit, val_) in match from with | Foot -> if to_unit = Foot then (to_unit, valu) else if to_unit = Meter then (to_unit, valu *. 0.3048) else (to_unit, valu /. 5280.0) | Meter -> if to_unit = Foot then (to_unit, valu /. 0.3048) else if to_unit = Meter then (to_unit, valu) else (to_unit, valu /. 0.3048 /. 5280.0) | Mile -> if to_unit = Foot then (to_unit, valu *. 5280.0) else if to_unit = Meter then (to_unit, valu *. 5280.0 *. 0.3048) else (to_unit, valu) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from: speed_unit = fst (from_unit, val_) in let valu: float = snd (from_unit, val_) in let dist = convert_dist ((fst from), valu) (fst to_unit) in let time = convert_time ((snd from), 1.0) (snd to_unit) in (to_unit, (snd dist) /. (snd time)) ;;
let dist_traveled (time: time_unit value) ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let speed: speed_unit value = (speed_unit, speed_val) in let con_time: time_unit value = convert_time time (snd (fst (speed))) in let dist: float = (snd speed) *. (snd con_time) in ((fst (fst (speed))), dist) ;; let rec passes_da_vinci t = let rec sqr_sum (tree: tree list) sum : float = match tree with | [] -> sum | Leaf :: tl -> sqr_sum tl sum | Branch (wid, sub) :: tl -> sqr_sum tl (sum +. (wid ** 2.0)) in let rec sub_check (list: tree list) : bool = match list with | [] -> true | x :: rest -> (passes_da_vinci x) && (sub_check rest) in match t with | Leaf -> true | Branch (width, sub_t) -> (width ** 2.0 >= sqr_sum sub_t 0.0) && (sub_check sub_t) ;;
let mode (l: 'a list) : 'a = let rec loop maxelt maxcount elt count = function | [] -> if count > maxcount then elt else maxelt | x::xs -> if elt = x then loop maxelt maxcount elt (count + 1) xs else if count > maxcount then loop elt count x 1 xs else loop maxelt maxcount x 1 xs in match List.sort compare l with | x::xs -> loop x 0 x 1 xs ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec pair l= match l with | a:: b:: xs -> (a, b) :: pair (b:: xs) | _::[] -> pair [] |[] -> [] in mode (pair l) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit == to_unit then to_unit, val_ else if from_unit == Second then to_unit , val_ /.3600. else to_unit , val_ *.3600. ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit == to_unit then to_unit, val_ else match (from_unit, to_unit) with |(Foot, Meter) -> to_unit, val_ *. 0.3048 |(Meter, Foot) -> to_unit, val_ /. 0.3048 |(Foot, Mile) -> to_unit, val_ /.5280. |(Mile, Foot) -> to_unit, val_ *.5280. |(Meter, Mile) -> to_unit, val_ /.0.3048 /.5280. |(Mile, Meter) -> to_unit, val_ *.0.3048 *.5280. ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match (from_unit, to_unit) with |((a, b),(c, d)) -> let (e, f) = convert_dist(a, val_) c in let (g, h) = convert_time(b, 1.) d in to_unit, f /.h ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = match speed_unit with |(a, b) -> match time with |(c, d) -> let (e, f) = convert_time(c, d) b in a, speed_val *.f ;; let rec passes_da_vinci t = notimplemented (); ;;
let get_value xy = let (x,y) = xy in y;; let get_x xy = let (x,y) = xy in x;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | x :: xs -> if cur_num == 0 then aux xs (x, 1) (x, 1) else if cur_el <> x && cur_num <= max_num then aux xs (x, 1) (max_el, max_num) else if cur_el <> x && cur_num > max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, cur_num + 1) (max_el, max_num) in let b = List.sort compare l in let xs = List.nth b ((List.length b) -1) in aux b (xs, 0) (xs, 0) ;; let rec pair_mode_helper (l: 'a list) (h: ('a * 'a) list) = match l with | [] -> h | x :: xs -> match xs with | [] -> h | y :: ys -> pair_mode_helper xs ((x,y) :: h) ;;
let pair_mode (l: 'a list) : 'a * 'a = let bruh = pair_mode_helper l [] in mode (bruh) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with | Second, Hour -> (to_unit, val_ /. 3600.) | Hour, Second -> (to_unit, val_ *. 3600.) | _ -> (to_unit, val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | Foot, Meter -> (to_unit, val_ *. 0.3048) | Meter, Foot -> (to_unit, val_ /. 0.3048) | Meter, Mile -> (to_unit, val_ /. 0.3048 /. 5280.) | Mile, Meter -> (to_unit, val_ *. 5280. *. 0.3048) | Foot, Mile -> (to_unit, val_ /. 5280.) | Mile, Foot -> (to_unit, val_ *. 5280.) | _ -> (to_unit, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((a,b),(c,d)) = (from_unit, to_unit) in ((c,d), (get_value (convert_time (d, (get_value (convert_dist (a, val_) c))) b))) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = (get_x speed_unit, (get_value (convert_time time (get_value speed_unit))) *. speed_val) ;; let rec passes_da_vinci t = let get_weight t : float = match t with | Leaf -> 0. | Branch (x,y) -> x in let get_list t : tree list = match t with | Leaf -> [] | Branch (x,y) -> y in let rec calculate t b : float = match t with | [] -> b | x :: xs when x == Leaf -> calculate xs b | x :: xs -> calculate xs (b +. (get_weight x) ** 2.) in let check_branch_false t : float = match t with | Leaf -> 0. | Branch (x,y) -> if calculate y 0. > x ** 2. then 1. else 0. in let rec check_tree_false t b : float = match t with | [] -> b | x :: xs when x == Leaf -> check_tree_false xs b | x :: xs -> check_tree_false xs (b +. (check_branch_false x)) +. check_tree_false (get_list x) b in if check_branch_false t > 0. then false else not ((check_tree_false (get_list t) 0.) > 0.) ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if (cur_num > max_num) then cur_el else max_el | x :: xs -> if cur_el = x then aux xs (cur_el, cur_num+1) (max_el, max_num) else (if cur_num > max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 1) (max_el, max_num)) in let lst = (List.sort compare l) in match lst with | [] -> failwith "cannot find mode for empty list" | x :: [] -> x | x :: xs -> aux lst (x, 0) (x, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec pair_list (l: 'a list) (last_el: 'a) plist = match l with | [] -> failwith "Empty list" | x :: [] -> (last_el, x) :: plist | x :: xs -> (pair_list xs x ((last_el, x) :: plist)) in match l with | [] -> failwith "Cannot compute pair for empty list" | x :: [] -> (x, x) | x :: xs -> mode (pair_list xs x []) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = to_unit then (from_unit, val_) else match from_unit with | Second -> (Hour, (val_ /. 3600.)) | Hour -> (Second, (val_ *. 3600.)) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = to_unit then (from_unit, val_) else match from_unit with | Foot -> (match to_unit with | Meter -> (Meter, (val_ *. 0.3048)) | Mile -> (Mile, (val_ /. 5280.))) | Meter -> (match to_unit with | Foot -> (Foot, (val_ /. 0.3048)) | Mile -> (Mile, ((val_ /. 0.3048) /. 5280.))) | Mile -> (match to_unit with | Foot -> (Foot, (val_ *. 5280.)) | Meter -> (Mile, ((val_ *. 5280.) *. 0.3048))) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (from_dist, from_time) = from_unit in let (to_dist, to_time) = to_unit in let (_, new_speed) = convert_dist (from_dist, val_) to_dist in let (_, factor) = convert_time (from_time, 1.) to_time in (to_unit, new_speed /. factor) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (_, new_speed1) = convert_speed a b_unit in (b_unit, b_val +. new_speed1) ;;
let passes_da_vinci (t: tree) = let rec all_leaf_thiccness_v2 tree_lst curr_list: (float list) = match tree_lst with | [] -> curr_list | Leaf :: xs -> all_leaf_thiccness_v2 xs curr_list | Branch (x, new_tree) :: xs -> (all_leaf_thiccness_v2 xs (x::curr_list)) in let rec sum_of_square lst sum = match lst with | [] -> sum | x :: xs -> sum_of_square xs ((x*.x)+.sum) in let curr_branch_da_vinci curr_t = match curr_t with | Leaf -> true | Branch(x, tr_list) -> (x*.x) >= (sum_of_square (all_leaf_thiccness_v2 tr_list []) 0.) in let rec verify_all_branches tree_list curr_result = match tree_list with | [] -> curr_result | Leaf :: xs -> verify_all_branches xs curr_result | x :: xs -> let Branch(_, tr_list) = x in verify_all_branches xs (curr_result && curr_branch_da_vinci x && verify_all_branches tr_list true) in match t with | Leaf -> true | Branch(_, tree_list1) -> curr_branch_da_vinci t && verify_all_branches tree_list1 true ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | hd :: tl -> ( if hd = cur_el then (if cur_num + 1 > max_num then aux tl (cur_el, cur_num + 1) (cur_el, cur_num + 1) else aux tl (cur_el, cur_num + 1) (max_el, max_num)) else aux tl (hd, 1) (max_el, max_num)) in let l_sorted = List.sort compare l in match l_sorted with | [] -> failwith "Cannot find mode of an empty list" | hd :: _ -> aux l_sorted (hd, 0) (hd, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "List must have at least 2 elements" | _ :: [] -> failwith "List must have at least 2 elements" | _ :: tl -> mode (List.combine (List.rev @@ List.tl @@ List.rev l) tl) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let new_val = match (from_unit, to_unit) with | (Second, Hour) -> val_ /. 3600. | (Hour, Second) -> val_ *. 3600. | _ -> val_ in (to_unit, new_val) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let new_val = match (from_unit, to_unit) with | (Foot, Meter) -> val_ *. 0.3048 | (Foot, Mile) -> val_ /. 5280. | (Meter, Foot) -> val_ /. 0.3048 | (Meter, Mile) -> (val_ /. 0.3048) /. 5280. | (Mile, Foot) -> val_ *. 5280. | (Mile, Meter) -> (val_ *. 5280.) *. 0.3048 | _ -> val_ in (to_unit, new_val) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let new_val = let ((from_dist_unit,from_time_unit), (to_dist_unit,to_time_unit)) = (from_unit, to_unit) in (convert_dist (from_dist_unit, val_) to_dist_unit, convert_time (from_time_unit, 1.) to_time_unit) in let ((_,dist_val),(_,time_val)) = new_val in (to_unit, (dist_val /. time_val)) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = (fst speed_unit, snd (convert_time time (snd speed_unit)) *. speed_val) ;; let rec sum_of_squares_tr (trees : tree list) (acc : float) : float = match trees with | [] -> acc | hd :: tl -> let recurse = match hd with | Leaf -> sum_of_squares_tr tl acc | Branch (val_, _) -> sum_of_squares_tr tl (acc +. val_ ** 2.) in recurse ;; let sum_of_squares (trees : tree list) : float = sum_of_squares_tr trees 0. ;; let rec passes_da_vinci t = let rec check_for_children (trees : tree list) : bool = match trees with | [] -> true | hd :: tl -> (passes_da_vinci hd) && (check_for_children tl) in match t with | Leaf -> true | Branch (val_, children) -> if sum_of_squares children > val_ ** 2. then false else check_for_children children ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> failwith "Undefined input." | [_] -> max_el | h :: t -> if h <> (List.nth t 0) then aux t (List.nth t 0, 1) (max_el, max_num) else let cur_num' = cur_num + 1 in let max_el' = if cur_num' > max_num then cur_el else max_el in let max_num' = if cur_num' > max_num then cur_num' else max_num in aux t (cur_el, cur_num') (max_el', max_num') in let sortedList = List.sort compare l in aux sortedList (List.hd sortedList, 1) (List.hd sortedList, 1);;
let pair_mode (l: 'a list) : 'a * 'a = let rec recurse l acclist= match l with | [] -> mode (List.rev (acclist)) | [_] -> mode (List.rev (acclist)) | h :: t :: [] -> mode (List.rev ((h, t) :: acclist)) | h :: h2 :: t -> recurse (h2 :: t) ((h, h2) :: acclist) in if List.length l = 0 || List.length l = 1 then failwith "Undefined input: list length should be >= 2." else recurse l [];;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if val_ < 0. then failwith "Time values cannot be negative." else match (from_unit, val_) with | (Hour, _) -> (match to_unit with | Hour -> (Hour, val_) | Second -> (Second, val_ *. 3600.0)) | (Second, _) -> (match to_unit with | Hour -> (Hour, val_ /. 3600.0) | Second -> (Second, val_));;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if val_ < 0. then failwith "Distance values cannot be negative." else match (from_unit, val_) with | (Foot, _) -> (match to_unit with | Foot -> (Foot, val_) | Mile -> (Mile, (val_ /. 5280.0)) | Meter -> (Meter, (val_ *. 0.3048))) | (Mile, _) -> (match to_unit with | Foot -> (Foot, (val_ *. 5280.0) ) | Mile -> (Mile, val_) | Meter -> (Meter, (val_ *. 1609.344))) | (Meter, _) -> (match to_unit with | Foot -> (Foot, (val_ /. 0.3048)) | Mile -> (Mile, (val_ /. 1609.344)) | Meter -> (Meter, val_));;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if val_ < 0. then failwith "Speed values cannot be negative." else let distance' = (convert_dist (fst from_unit, val_) (fst to_unit)) in let time' = (convert_time (snd from_unit, 1.0) (snd to_unit)) in let timeMult = 1.0 /. (snd time') in let speed' = (fst distance', fst time') in (speed', (snd distance') *. timeMult);;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let speed' = convert_speed a b_unit in (b_unit, (snd speed') +. b_val);;
let passes_da_vinci t = let rec ttf (t: tree) = match t with | Leaf -> true | Branch (width, subtrees) -> if (width ** 2.) >= findSubtreeSum subtrees then ctf subtrees else false and ctf subtreeList = match subtreeList with | [] -> true | h :: t -> ttf h && ctf t in ttf t;;
let mode (l: 'a list) : 'a = let rec aux (l:'a list) ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | head :: body -> begin if head = cur_el then aux body (cur_el, cur_num + 1) (max_el, max_num) else ( if cur_num > max_num then aux body (head, 1) (cur_el, cur_num) else aux body (head, 1) (max_el, max_num) ) end in let sorted_l = List.sort compare l in match sorted_l with | [] -> failwith "Undefined Input" | first :: _ -> aux (sorted_l) (first, 0) (first, 0) ;; let remove_first_el (l: 'a list) : ('a list) = match l with | [] -> failwith "Undefined" | head :: tail -> tail ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Undefined Input" | [x] -> failwith "Invalid Input" | _ -> begin let list1 = remove_first_el l in let list2 = List.rev l in let list3 = remove_first_el list2 in let list4 = List.rev list3 in let list_tuples = List.combine list4 list1 in mode list_tuples end ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match to_unit with | Second -> begin match fst(from_unit, val_) with | Second -> (Second, val_) | Hour -> (Second, val_ *. 3600.) end | Hour -> begin match fst(from_unit, val_) with | Hour -> (Hour, val_) | Second -> (Hour, val_ /. 3600.) end ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match to_unit with | Foot -> begin match fst(from_unit, val_) with | Foot -> (Foot, val_) | Meter -> (Foot, val_ /. 0.3048) | Mile -> (Foot, val_ *. 5280.) end | Meter -> begin match fst(from_unit, val_) with | Meter -> (Meter, val_) | Foot -> (Meter, val_ *. 0.3048) | Mile -> (Meter, val_ *. 5280. *. 0.3048) end | Mile -> begin match fst(from_unit, val_) with | Mile -> (Mile, val_) | Foot -> (Mile, val_ /. 5280.) | Meter -> (Mile, val_ /. 0.3048 /. 5280.) end ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match fst(to_unit) with | Foot -> begin match snd(to_unit) with | Second -> begin match fst(from_unit) with | Foot -> begin match snd(from_unit) with | Second -> (to_unit, val_) | Hour -> (to_unit, val_ /. 3600.) end | Meter -> begin match snd(from_unit) with | Second -> (to_unit, val_ /. 0.3048) | Hour -> (to_unit, val_ /. 0.3048 /. 3600.) end | Mile -> begin match snd(from_unit) with | Second -> (to_unit, val_ *. 5280.) | Hour -> (to_unit, val_ *. 5280. /. 3600.) end end | Hour -> begin match fst(from_unit) with | Foot -> begin match snd(from_unit) with | Second -> (to_unit, val_ *. 3600.) | Hour -> (to_unit, val_) end | Meter -> begin match snd(from_unit) with | Second -> (to_unit, val_ /. 0.3048 *. 3600.) | Hour -> (to_unit, val_ /. 0.3048) end | Mile -> begin match snd(from_unit) with | Second -> (to_unit, val_ *. 5280. *. 3600.) | Hour -> (to_unit, val_ *. 5280.) end end end | Meter -> begin match snd(to_unit) with | Second -> begin match fst(from_unit) with | Foot -> begin match snd(from_unit) with | Second -> (to_unit, val_ *. 0.3048) | Hour -> (to_unit, val_ *. 0.3048 /. 3600.) end | Meter -> begin match snd(from_unit) with | Second -> (to_unit, val_) | Hour -> (to_unit, val_ /. 3600.) end | Mile -> begin match snd(from_unit) with | Second -> (to_unit, val_ *. 5280. *. 0.3048) | Hour -> (to_unit, val_ *. 5280. *. 0.3048 /. 3600.) end end | Hour -> begin match fst(from_unit) with | Foot -> begin match snd(from_unit) with | Second -> (to_unit, val_ *. 0.3048 *. 3600.) | Hour -> (to_unit, val_ *. 0.3048) end | Meter -> begin match snd(from_unit) with | Second -> (to_unit, val_ *. 3600.) | Hour -> (to_unit, val_) end | Mile -> begin match snd(from_unit) with | Second -> (to_unit, val_ *. 5280. *. 0.3048 *. 3600.) | Hour -> (to_unit, val_ *. 5280. *. 0.3048) end end end | Mile -> begin match snd(to_unit) with | Second -> begin match fst(from_unit) with | Foot -> begin match snd(from_unit) with | Second -> (to_unit, val_ /. 5280.) | Hour -> (to_unit, val_ /. 5280. /. 3600.) end | Meter -> begin match snd(from_unit) with | Second -> (to_unit, val_ /. 0.3048 /. 5280.) | Hour -> (to_unit, val_ /. 0.3048 /. 5280. /. 3600.) end | Mile -> begin match snd(from_unit) with | Second -> (to_unit, val_) | Hour -> (to_unit, val_ /. 3600.) end end | Hour -> begin match fst(from_unit) with | Foot -> begin match snd(from_unit) with | Second -> (to_unit, val_ /. 5280. *. 3600.) | Hour -> (to_unit, val_ /. 5280.) end | Meter -> begin match snd(from_unit) with | Second -> (to_unit, val_ /. 0.3048 /. 5280. *. 3600.) | Hour -> (to_unit, val_ /. 0.3048 /. 5280.) end | Mile -> begin match snd(from_unit) with | Second -> (to_unit, val_ *. 3600.) | Hour -> (to_unit, val_) end end end ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let converted_speed = convert_speed a b_unit in (b_unit, snd(converted_speed) +. b_val) ;;
let passes_da_vinci t = let rec sum_l l (acc: float) = match l with | [] -> acc | Leaf :: tl -> sum_l tl acc | Branch (width, _) :: tl -> sum_l tl (width**2. +. acc) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if ( not (check subtree) || sum_l subtree 0. > width**2.) then false else check tl in check [t] ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | hd::tl -> if cur_el = hd then aux tl (cur_el,cur_num+1) (max_el,max_num) else if cur_num > max_num then aux tl (hd,1) (cur_el,cur_num) else aux tl (hd,1) (max_el,max_num) in match l with | [] -> failwith "Undefined input." | _ -> aux (List.sort compare l) (List.hd (List.sort compare l),0) (List.hd (List.sort compare l),1) ;;
let pair_mode (l: 'a list) : ('a *'a) = if List.length l < 2 then failwith "Invalid list length." else let (_::new_list1) = l in let (_::new_list2) = List.rev l in let new_list2 = List.rev new_list2 in let new_list3 = List.combine new_list2 new_list1 in mode new_list3 ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (fst(from_unit,val_),to_unit) with | (Hour,Second) -> (Second, val_ *. 3600.) | (Second,Hour) -> (Hour, val_ /. 3600.) | (Hour,Hour) -> (Hour, val_) | (Second,Second) -> (Second, val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (fst(from_unit,val_),to_unit) with | (Foot,Meter) -> (Meter, val_ *. 0.3048) | (Foot,Mile) -> (Mile, val_ /. 5280.) | (Foot,Foot) -> (Foot, val_) | (Meter,Foot) -> (Foot, val_ /. 0.3048) | (Meter,Mile) -> (Mile, val_ /. 0.3048 /. 5280.) | (Meter,Meter) -> (Meter, val_) | (Mile,Foot) -> (Foot, val_ *. 5280.) | (Mile,Meter) -> (Meter, val_ *.5280. *. 0.3048) | (Mile,Mile) -> (Mile, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_unit_time = snd(fst(from_unit,val_))in let to_unit_time = snd(to_unit) in let time_conversion = convert_time (from_unit_time, 1.) to_unit_time in let time = snd(time_conversion) in let from_unit_dist = fst(fst(from_unit,val_)) in let to_unit_dist = fst(to_unit) in let dist_conversion = convert_dist (from_unit_dist, 1.) to_unit_dist in let dist = snd(dist_conversion) in (to_unit, val_ *. dist /. time) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let unit_for_dist = fst(fst(speed_unit,speed_val)) in let unit_for_time = snd(fst(speed_unit,speed_val)) in let convert_given_time = convert_time time unit_for_time in let magnitude_of_speed = snd(speed_unit, speed_val) in (unit_for_dist, magnitude_of_speed *. snd(convert_given_time)) ;; let rec passes_da_vinci t = notimplemented () ;;
let notimplemented () = failwith "This function is not yet implemented." type dist_unit = Foot | Meter | Mile type time_unit = Second | Hour type speed_unit = dist_unit * time_unit type 'a value = 'a * float type tree = Branch of float * tree list | Leaf let tree_example = Branch (5., [ Branch (3., [Leaf; Leaf; Leaf]); Leaf; Branch (4., []) ]) ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | h::t -> if cur_el = h then aux t (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux t (h, 1) (cur_el, cur_num) else aux t (h, 1) (max_el, max_num) in let lst = List.sort compare l in let hd = List.hd lst in let tl = List.tl lst in aux tl (hd, 1) (hd, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let hd = List.hd l in let tl = List.tl l in let f (pre, acc) cur = (cur, (pre, cur)::acc) in let pair_list = List.rev (snd (List.fold_left f (hd, []) tl)) in mode pair_list ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with | Hour, Second -> Second, val_ *. 3600. | Second, Hour -> Hour, val_ /. 3600. | _ -> from_unit, val_ ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | Mile, Meter -> Meter, val_ *. 5280. *. 0.3048 | Mile, Foot -> Foot, val_ *. 5280. | Meter, Mile -> Mile, val_ /. (5280. *. 0.3048) | Meter, Foot -> Foot, val_ /. 0.3048 | Foot, Mile -> Mile, val_ /. 5280. | Foot, Meter -> Meter, val_ *. 0.3048 | _ -> from_unit, val_ ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let fdu, ftu = from_unit in let tdu, ttu = to_unit in let _, dist = convert_dist (fdu, 1.) tdu in let _, time = convert_time (ttu, 1.) ftu in (tdu, ttu), val_ *. time *. dist ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let du, tu = speed_unit in let _, t = convert_time time tu in du, speed_val *. t ;; let rec passes_da_vinci t = let map_all f = List.fold_left (fun r e -> r && f e) true in let rec sum_tree = function | [] -> 0. | Leaf::ts -> sum_tree ts | (Branch (v, _))::ts -> (v *. v) +. (sum_tree ts) in match t with | Leaf -> true | Branch (v, children) -> map_all passes_da_vinci children && (sum_tree children <= v *. v) ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | x :: xs -> if cur_el = x then aux xs (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 1) (max_el, max_num) in let l = List.sort compare l in match l with | [] -> failwith "Undefined input." | x :: xs -> aux xs (x, 1) (x, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = if (List.length l) >= 2 then let l1 = List.tl l in let l2 = List.rev l in let l3 = List.tl l2 in let l4 = List.rev l3 in let l5 = List.combine l4 l1 in mode l5 else failwith "Undefined input." ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let init_unit = fst(from_unit, val_) in let value = snd(from_unit, val_) in if init_unit = to_unit then (to_unit, value) else match init_unit with | Hour -> (Second, (value *. 3600.)) | Second -> (Hour, (value /. 3600.)) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let init_unit = fst(from_unit, val_) in let value = snd(from_unit, val_) in match init_unit, to_unit with | Foot, Foot -> (Foot, value) | Meter, Meter -> (Meter, value) | Mile, Mile -> (Mile, value) | Foot, Meter -> (Meter, (value *. 0.3048)) | Foot, Mile -> (Mile, (value /. 5280.)) | Meter, Foot -> (Foot, (value /. 0.3048)) | Meter, Mile -> (Mile, (value /. 1609.344)) | Mile, Meter -> (Meter, (value *. 1609.344)) | Mile, Foot -> (Foot, (value *. 5280.)) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_unit_dist = fst(from_unit) in let from_unit_time = snd(from_unit) in let from_value = snd(from_unit, val_) in let to_unit_dist = fst(to_unit) in let to_unit_time = snd(to_unit) in if (fst(from_unit, val_)) = to_unit then (to_unit, from_value) else if from_unit_time = to_unit_time then let result = convert_dist (from_unit_dist, 1.0) to_unit_dist in ((fst(result), to_unit_time), (from_value *. snd(result))) else if from_unit_dist = to_unit_dist then let result = convert_time (from_unit_time, 1.0) to_unit_time in ((from_unit_dist, fst(result)), (from_value /. snd(result))) else let (_ ,_ ,_ ,_) = (from_unit_dist, to_unit_dist, from_unit_time, to_unit_time) in let result_dist = convert_dist (from_unit_dist, 1.0) to_unit_dist in let result_time = convert_time (from_unit_time, 1.0) to_unit_time in ((fst(result_dist), fst(result_time)), (from_value *. (snd(result_dist) /. snd(result_time)))) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_unit = fst(a) in let a_value = snd(a) in let unit_b = fst(b_unit, b_val) in let value_b = snd(b_unit, b_val) in if a_unit = unit_b then (a_unit, a_value +. value_b) else let result = convert_speed (a_unit, a_value) unit_b in (fst(result), (snd(result) +. value_b)) ;;
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, _) :: tl -> sum_ tl (acc + int_of_float(width *. width)) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if ((check subtree) && (sum_ subtree 0 <= int_of_float(width *. width))) then check tl else false in check [t] ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) : 'a = match l with | [] -> max_el | x :: xs -> if (compare x cur_el) = 0 then if cur_num + 1 > max_num then aux xs (cur_el, (cur_num + 1)) (cur_el, (cur_num + 1)) else aux xs (cur_el, (cur_num + 1)) (max_el, max_num) else aux xs (x, 1) (max_el, max_num) in let l = List.sort compare l in match l with | [] -> failwith "The input list is empty." | x :: _ -> aux l (x, 0) (x, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = mode (List.combine (List.rev (List.tl (List.rev l))) (List.tl l)) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let secs_in_hr = 3600. in match (from_unit, to_unit) with | (Second, Hour) -> (Hour, val_ /. secs_in_hr) | (Hour, Second) -> (Second, val_ *. secs_in_hr) | _ -> (from_unit, val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let fts_in_mi = 5280. in let mts_in_ft = 0.3048 in match (from_unit, to_unit) with | (Foot, Mile) -> (Mile, val_ /. fts_in_mi) | (Mile, Foot) -> (Foot, val_ *. fts_in_mi) | (Foot, Meter) -> (Meter, val_ *. mts_in_ft) | (Meter, Foot) -> (Foot, val_ /. mts_in_ft) | (Meter, Mile) -> (Mile, val_ /. ( mts_in_ft *. fts_in_mi)) | (Mile, Meter) -> (Meter, val_ *. mts_in_ft *. fts_in_mi) | _ -> (from_unit, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (from_dist_unit, from_time_unit) = from_unit in let (to_dist_unit, to_time_unit) = to_unit in let (dist_unit, dist_value) = convert_dist (from_dist_unit, val_) to_dist_unit in let (time_unit, time_value) = convert_time (from_time_unit, 1.) to_time_unit in ((dist_unit, time_unit), dist_value /. time_value) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (dist_unit, time_unit) = speed_unit in let (_, time_val) = convert_time time time_unit in (dist_unit, speed_val *. time_val) ;; let tree1 = Branch (5., [ Branch (3., [Leaf; Leaf; Leaf]); Leaf; Branch (4., []) ]) ;; let tree2 = Branch (5., [ Branch (3., [Leaf; Leaf; Leaf]); Leaf; Branch (5., []) ]) ;; let tree3 = Branch (5., []) ;; let tree4 = Branch (5., [ Branch (3., [Leaf; Branch (3., []); Leaf]); Leaf; Branch (4., []) ]) ;; let tree5 = Branch (5., [ Branch (3., [Leaf; Branch (5., []); Leaf]); Leaf; Branch (4., []) ]) ;; let tree6 = Branch (-5., [ Branch (3., [Leaf; Leaf; Leaf]); Leaf; Branch (4., [Branch (5., [])]) ]) ;; let tree7 = Leaf ;; let tree8 = Branch (-5., [ Leaf ]) ;; let rec sum_of_squares t : float = match t with |[] -> 0. |x :: xs -> match x with | Branch (width, sub_tree) -> sum_of_squares xs +. (width *. width) | Leaf -> sum_of_squares xs ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch (width, subtree) -> width *. width >= sum_of_squares subtree && let rec helper subtree = match subtree with |[] -> true |x :: xs -> passes_da_vinci x && helper xs in helper subtree ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | hd::tl -> if hd = cur_el then if (cur_num + 1) > max_num then aux tl (cur_el, cur_num +1) (cur_el, cur_num +1) else aux tl (cur_el, cur_num +1) (max_el, max_num) else aux tl (hd, 1) (max_el, max_num) in let l_sorted = List.sort compare l in aux l_sorted (List.hd l_sorted, 0) (List.hd l_sorted, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec gen_pair_list (l: 'a list) p_list = match l with | hd::[] -> p_list | hd::tl -> gen_pair_list tl ((hd, List.hd tl) :: p_list) in mode (gen_pair_list l []) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let hour_to_second = 3600. in let second_to_hour = 1. /. hour_to_second in match (from_unit, val_), to_unit with | (Second, val_), Second -> (Second, val_) | (Second, val_), Hour -> (Hour, val_ *. second_to_hour) | (Hour, val_), Hour -> (Hour, val_) | (Hour, val_), Second -> (Second, val_ *. hour_to_second) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let foot_to_meter = 0.3048 in let meter_to_foot = 1. /. foot_to_meter in let mile_to_foot = 5280. in let foot_to_mile = 1. /. mile_to_foot in let meter_to_mile = meter_to_foot *. foot_to_mile in let mile_to_meter = 1. /. meter_to_mile in match (from_unit, val_), to_unit with | (Foot, val_), Foot -> (Foot, val_) | (Foot, val_), Meter -> (Meter, val_ *. foot_to_meter) | (Foot, val_), Mile -> (Mile, val_ *. foot_to_mile) | (Meter, val_), Meter -> (Meter, val_) | (Meter, val_), Foot -> (Foot, val_ *. meter_to_foot) | (Meter, val_), Mile -> (Mile, val_ *. meter_to_mile) | (Mile, val_), Mile -> (Mile, val_) | (Mile, val_), Foot -> (Foot, val_ *. mile_to_foot) | (Mile, val_), Meter -> (Meter, val_ *. mile_to_meter) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (((dist_from_unit, time_from_unit), val_), (dist_to_unit, time_to_unit)) = ((from_unit, val_), to_unit) in let new_dist = convert_dist (dist_from_unit, val_) dist_to_unit in let new_time = convert_time (time_from_unit, 1.0) time_to_unit in let (_, new_dist_val) = new_dist in let (_, new_time_val) = new_time in let new_val = new_dist_val /. new_time_val in ((dist_to_unit, time_to_unit), new_val) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (speed_dist_unit, speed_time_unit) = speed_unit in let time = convert_time time speed_time_unit in let (_, t_val) = time in (speed_dist_unit, speed_val *. t_val) ;; let branch_width t = match t with | Leaf -> 0. | Branch(w, _) -> w let sum_of_squares (treeList: tree list) = let rec tr_sum_of_squares (treeList, acc) = match treeList with | [] -> acc | hd::tl -> tr_sum_of_squares (tl, acc +. (branch_width(hd) ** 2.)) in tr_sum_of_squares (treeList, 0.) ;; let rec check_list (property_check, list) = match list with | [] -> true | hd::tl -> if property_check hd then check_list (property_check, tl) else false ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch(_, []) -> true | Branch(root_width, children_list) -> if root_width ** 2. < sum_of_squares children_list then false else check_list(passes_da_vinci, children_list) ;;
let mode (l: 'a list) : 'a = let l = List.sort compare l in match l with | [] -> failwith "Input cannot be empty" | x::_ -> let rec aux l ((cur_el,cur_num) : 'a * int) ((max_el,max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | x::xs -> if x = cur_el && cur_num + 1 > max_num then aux xs (x, cur_num + 1) (x, cur_num + 1) else if x = cur_el && cur_num + 1 <= max_num then aux xs (x, cur_num + 1) (max_el, max_num) else aux xs (x, 1) (max_el, max_num) in aux l (List.hd l, 0) (List.hd l, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = if (List.length l) < 2 then failwith "Input must have at least 2 values" else let l = List.combine (List.rev (List.tl (List.rev l))) (List.tl l) in mode l ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> if to_unit = Second then (to_unit, val_) else (to_unit, val_ /. 3600.) | Hour -> if to_unit = Hour then (to_unit, val_) else (to_unit, val_ *. 3600.) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> if to_unit = Foot then (to_unit, val_) else if to_unit = Mile then (to_unit, val_ /. 5280.) else (to_unit, val_ *. 0.3048) | Meter -> if to_unit = Meter then (to_unit, val_) else if to_unit = Foot then (to_unit, val_ /. 0.3048) else (to_unit, val_ /. 1609.344) | Mile -> if to_unit = Mile then (to_unit, val_) else if to_unit = Meter then (to_unit, val_ *. 1609.344) else (to_unit, val_ *. 5280.) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if from_unit = to_unit then ((to_unit), val_) else if fst(from_unit) = fst(to_unit) && snd(from_unit) != snd(to_unit) then ((to_unit), snd(convert_time (snd(to_unit), val_) (snd(from_unit)))) else if fst(from_unit) != fst(to_unit) && snd(from_unit) = snd(to_unit) then ((to_unit), snd(convert_dist (fst(from_unit), val_) (fst(to_unit)))) else ((to_unit), snd(convert_time (snd(to_unit), snd(convert_dist (fst(from_unit), val_) (fst(to_unit)))) (snd(from_unit)))) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let newspeed = convert_speed a b_unit in ((b_unit), snd(newspeed) +. b_val) ;;