text
stringlengths
0
601k
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 ...
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Undefined input." | hd::tl -> let length = List.length l in if length=1 then failwith "Undefined input." else( let l1 = tl in let l2temp = List.rev l in let l2temp2 = List.tl l2temp in let l2 = List.rev l2temp2 in let lmerged = List.combine l2 l1 in ...
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit=Hour then (if to_unit=Hour then (to_unit,val_) else ((to_unit,(val_ *. 3600.)))) else(if to_unit=Second then (to_unit,val_) else ((to_unit,(val_ /. 3600.)))) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit=Mile then (if to_unit=Mile then (to_unit,val_) else (if to_unit=Meter then (to_unit,(val_ *. 5280. *. 0.3048 )) else( (to_unit,(val_ *. 5280.))))) else(if from_unit=Meter then (if to_unit=Meter then (to_unit,val_) else (if t...
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dist = fst(from_unit) in let time = snd(from_unit) in let distTo = fst(to_unit) in let timeTo = snd(to_unit) in let distChange = convert_dist (dist,val_) distTo in let distChangeValue = snd(distChange) in let timeChange = convert_...
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_speed = convert_speed a b_unit in let a_val = snd(a_speed) in let speed_val = a_val +. b_val in (b_unit, speed_val) ;;
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, subtree) :: tl -> sum_ 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_ subtree 0.) <= (w...
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 max_el else cur_el | h :: t -> if (h = cur_el && cur_num + 1 > max_num) then aux t (cur_el,cur_num +1) (cur_el,cur_num + 1) else if (h = cur_el && cur_num + 1 <= ma...
let pair_mode (l: 'a list) : 'a * 'a = if (List.length l < 2) then failwith "Error"; let (_ :: no_head1) = l in let (_ :: no_head2) = List.rev l in mode(List.combine (List.rev no_head2) no_head1 ) ;;
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.) | Hour,Hour -> (Hour,val_ ) | Second,Hour-> (Hour, val_ /. 3600.) | Second,Second -> (Second,val_ ) | _,_ -> failwith "Error" ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit,to_unit with | Foot,Mile -> (Mile,val_ /. 5280.) | Foot,Foot -> (Foot,val_ ) | Mile,Foot-> (Foot, val_ *. 5280.) | Mile,Mile -> (Mile,val_ ) | Meter,Foot-> (Foot, val_ /. 0.3048) | Foot,Meter -> (Meter,val_ *. 0.3048) | M...
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = (to_unit,val_*. snd(convert_dist (fst(from_unit), 1.) (fst(to_unit))) /.snd(convert_time(snd(from_unit), 1.) (snd(to_unit)))) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit,b_val +. snd(convert_speed a b_unit)) ;;
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, subtree) :: tl -> sum_ subtree acc+. width ** 2. in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if (((check subtree) && (sum_ subtree 0. <= (widt...
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 | el :: x -> if el = cur_el then aux x (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux x (el, 1) (cur_el, cur_num) else au...
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Undefined input: the list is empty" | _ :: [] -> failwith "Undefined input: the list should be of length >= 2" | _ -> let rec remove_last (l: 'a list) lfinal = match l with | [] -> List.rev(lfinal) | _ :: [] -> List.rev(lfinal) | x :: y -> remove_las...
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 to_unit with | Second -> (to_unit, val_ *. 3600.) | Hour -> (to_unit, val_ /. 3600.) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> ( match to_unit with | Foot -> (to_unit, val_) | Meter -> (to_unit, val_ *. 0.3048) | Mile -> (to_unit, val_ /. 5280.) ) | Meter -> ( match to_unit with | Meter -> (to_unit, val_) | Foot -> (to_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 converted_dist = let (_, new_val) = convert_dist (from_dist_unit, 1.) to_dist_unit in new_val *. val_ in let converted_speed = let...
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let converted_speed_a = convert_speed a b_unit in let (_, a_val) = converted_speed_a in (b_unit, a_val +. b_val) ;;
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, subtree) :: tl -> sum_ tl (acc +. (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. <= (wi...
let mode (l: 'a list) : 'a = if l == [] then failwith "Undefined input" else let sorted_list = List.sort compare(l) in 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 if cur_num = max_num then if cur_el <= max_el then cur_el else m...
let pair_mode (l: 'a list) : 'a * 'a = let (l1 : 'a list) = match l with | [] -> [] | x :: xs -> xs in let (l2 : 'a list) = match (List.rev l) with | [] -> [] | x :: xs -> List.rev xs in let (l3: ('a * 'a) list) = List.combine l2 l1 in mode l3 ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> (match to_unit with | Second -> (Second, val_) | Hour -> (Hour, val_ /. 3600.) | _ -> failwith "Undefined Input" ) | Hour -> (match to_unit with | Second -> (Second, val_ *. 3600.) | Hour -> (Hour, val_) ...
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> (match to_unit with | Foot -> (to_unit, val_) | Meter -> (to_unit, val_ *. 0.3048) | Mile -> (to_unit, val_ /. 5280.) | _ -> failwith "Undefined Input" ) | Meter -> (match to_unit with | Foot -> (to_unit, v...
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 to_unit_dist = fst(to_unit) in let to_unit_time = snd(to_unit) in let fin_dist = snd(convert_dist (from_unit_dist, 1.) to_unit_dist) in let fin_time = s...
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_val = snd(convert_speed (fst(a), snd(a)) b_unit) in (b_unit, a_val +. b_val) ;;
let passes_da_vinci t = let rec check_branch l = match l with | [] -> true | Leaf :: tl -> check_branch tl | Branch (width, subtree) :: tl -> let self_width_sq = width *. width in let subtrees_sum_sq = subtrees_sum_of_square subtree in if (check_branch subtree && subtrees_sum_sq <= self_width_sq) == false then false el...
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 (cur_el = x && cur_num + 1 > max_num) then aux (xs) (x, cur_num+1) (x, cur_num+1) else if (cur_el = x && cur_num + 1 <= max_num) then aux (xs) (cur_el,cur_num+1) (max_el...
let pair_mode (l: 'a list) : 'a * 'a = let rec helper (li: 'a list) (l1: 'a) (l2: 'a) (l3: ('a * 'a) list) (count: int) = match li with |[] -> l3 |x :: xs -> if (count = 2) then helper (xs) (x) (l2) (l3) (count + 1) else if (count mod 2 = 0) then helper (xs) (x) (l2) ((l2, x) :: l3) (count + 1) else helper (xs) (l1) (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 if (from_unit = Second && to_unit = Hour) then (Hour, val_ /. 3600.) else (Second, 3600. *. val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if (from_unit = Foot) then if (to_unit = Meter) then (Meter, val_ *. 0.3048) else if (to_unit = Foot) then (from_unit, val_) else (Mile, val_ /. 5280.) else if (from_unit = Meter) then if (to_unit = Foot) then (Foot, val_ /. 0.3048) else...
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = ((fst (to_unit), snd (to_unit)) , val_ *. snd (convert_dist (fst (from_unit), 1.) (fst (to_unit))) /. snd (convert_time (snd (from_unit),1.) (snd (to_unit))) ) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit, snd (convert_speed (a) (b_unit) ) +. b_val) ;;
let passes_da_vinci t = helper_method t ;;
let mode (l: 'a list) : 'a = let l = List.sort compare l in match l with |[]->notimplemented() |h::t -> let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el |h::t -> if compare cur_el h = 0 && cur_num = max_num then aux t (cur_el, cur_num + 1) (cur_el, cur_num + 1) e...
let pair_mode (l: 'a list) : 'a * 'a = let rec pair_list_creator (l: 'a list) : ('a * 'a) list = match l with |[] -> [] |h:: [] -> [] |h::t -> (h,List.nth l 1) :: pair_list_creator t in let l = pair_list_creator l in mode l ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with |(Second, Hour) -> (Hour, val_ /. 3600.) |(Hour, Second) ->(Second,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 |(Foot, Meter) -> (Meter, val_ *. 0.3048) |(Foot, Mile) -> (Mile, val_ /. 5280.) |(Meter, Foot) -> (Foot, val_ /. 0.3048) |(Meter, Mile) -> (Mile, val_ /. 1609.344) |(Mile, Foot) -> (Foot, val_ *. 5280.) |(Mi...
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((init_dist,init_time),(converted_dist,converted_time)) = (from_unit, to_unit) in let dist = convert_dist (init_dist, 1.) converted_dist in let time = convert_time (init_time, 1.) converted_time in let ((dist_unit,dist_value),(tim...
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (a_unit, a_value) = a in let (_, a_new_value) = convert_speed (a_unit, a_value) b_unit in (b_unit, a_new_value +. b_val) ;;
let passes_da_vinci t : bool = match t with |Leaf -> true |Branch(max_weight, tree_list) -> if square_rule (max_weight *. max_weight) 0. tree_list then iterate_tree tree_list else false ;;
let mode (l: 'a list) : 'a = let l = List.sort compare l in match l with |[]->failwith "list is empty" |h::t -> let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el |h::t -> if compare cur_el h = 0 && cur_num = max_num then aux t (cur_el, cur_num + 1) (cur_el, cur_nu...
let pair_mode (l: 'a list) : 'a * 'a = let rec pair_list_creator (l: 'a list) : ('a * 'a) list = match l with |[] -> [] |h:: [] -> [] |h::t -> (h,List.nth l 1) :: pair_list_creator t in let l = pair_list_creator l in mode l ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with |(Second, Hour) -> (Hour, val_ /. 3600.) |(Hour, Second) ->(Second,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 |(Foot, Meter) -> (Meter, val_ *. 0.3048) |(Foot, Mile) -> (Mile, val_ /. 5280.) |(Meter, Foot) -> (Foot, val_ /. 0.3048) |(Meter, Mile) -> (Mile, val_ /. 1609.344) |(Mile, Foot) -> (Foot, val_ *. 5280.) |(Mi...
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((init_dist,init_time),(converted_dist,converted_time)) = (from_unit, to_unit) in let dist = convert_dist (init_dist, 1.) converted_dist in let time = convert_time (init_time, 1.) converted_time in let ((dist_unit,dist_value),(tim...
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (a_unit, a_value) = a in let (_, a_new_value) = convert_speed (a_unit, a_value) b_unit in (b_unit, a_new_value +. b_val) ;;
let passes_da_vinci t : bool = match t with |Leaf -> true |Branch(max_weight, tree_list) -> if square_rule (max_weight *. max_weight) 0. tree_list then iterate_tree tree_list else false ;;
let domain () = failwith "REMINDER: You should not be writing tests for undefined values." let rec count (lst:'a list) (element:'a) (num:int) = match lst with | [] -> num | head :: body -> count body element (if head=element then num+1 else num) ;;
let mode (l: 'a list) : 'a = if (List.length l) <1 then domain() else let l = List.sort compare(l) in let rec aux newl ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match newl with | [] -> max_el | head :: body -> aux body (head, (count l head 0)) (if max_num < cur_num then (cur_el, cur_num)else (max_...
let pair_mode (l: 'a list) : 'a * 'a = if (List.length l) <2 then domain() else mode (change_to_pair l) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if val_ < 0. then domain() else 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 = if val_ < 0. then domain() else match from_unit, to_unit with | Foot, Mile -> (to_unit,val_ /. 5280.) | Foot, Meter -> (to_unit,val_ *. 0.3048) | Meter, Foot -> (to_unit,val_ /. 0.3048) | Meter, Mile -> (to_unit,val_ *. 0.000621371192) |...
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if val_ < 0. then domain() else let from_dist, from_time = from_unit in let to_dist, to_time = to_unit in let time_unit,time_val = convert_time (from_time,1.) to_time in let dist_unit,dist_val = convert_dist (from_dist,1.) to_dist in ...
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let _,a_val = a in if b_val < 0. || a_val < 0. then domain() else let _, a_convert_value = convert_speed a b_unit in b_unit, b_val +. a_convert_value ;;
let passes_da_vinci (t:tree) : bool = let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if (check subtree) && ((sum_square subtree 0.) <= (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) = 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...
let pair_mode (l: 'a list) : 'a * 'a = let l3 = let l1 = List.tl l in let l2_temp = List.rev l in let l2_temp1 = List.tl l2_temp in let l2 = List.rev l2_temp1 in 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 -> Hour, val_/.3600. |Hour, Second -> Second, val_*.3600. |_, to_unit -> 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 -> Meter, val_*.0.3048 |Foot, Mile -> Mile, val_/.5280. |Meter, Foot -> Foot, val_/.0.3048 |Meter, Mile -> Mile, val_/.1609.344 |Mile, Meter -> Meter, val_*.1609.344 |Mile, Foot -> Foot, val_*.5...
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (_, to_unit) = (from_unit, to_unit) in to_unit, val_*.(snd (convert_dist ((fst from_unit),1.) (fst to_unit)))/.(snd (convert_time ((snd from_unit),1.) (snd to_unit))) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_converted: speed_unit value = convert_speed a (b_unit) in (fst a_converted), (snd a_converted) +. b_val ;;
let passes_da_vinci t = let rec sum_ l acc = match l with |[] -> 0. |Leaf::_ -> 0. |Branch (width, _)::tl -> (width ** 2.) +. (sum_ tl 0.) in let rec check l = match l with |[] -> true |Leaf::_ -> true |Branch (width, subtree)::tl -> if not ((check subtree) && (sum_ subtree 0.) <= (width **2.)) then false else check tl...
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 compare cur_num max_num = 1 then cur_el else max_el | h :: t -> if compare h cur_el = 0 then aux t (h, cur_num+1) (max_el, max_num) else if compare cur_num max_num = 1 then aux t (h, 1) (cu...
let pair_mode (l: 'a list) : 'a * 'a = if compare l [] = 0 then failwith "Undefined input." else if List.length l < 2 then failwith "Undefined input." else 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 = match from_unit with | Second -> (match to_unit with | Second -> (to_unit, val_) | Hour -> (to_unit, val_ /. 3600.)) | Hour -> (match to_unit with | Hour -> (to_unit, val_) | Second -> (to_unit, val_ *. 3600.)) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> (match to_unit with | Meter -> (to_unit, val_ *. 0.3048) | Mile -> (to_unit, val_ /. 5280.) | Foot -> (to_unit, val_)) | Meter -> (match to_unit with | Foot -> (to_unit, val_ /. 0.3048) | Mile -> (to_unit, ...
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = (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 = (b_unit, snd(convert_speed a b_unit) +. b_val) ;;
let passes_da_vinci t = let rec sum_ l = match l with | [] -> 0. | Leaf :: tl -> (sum_ tl) | Branch (width, subtree) :: tl -> width *.width +. (sum_ tl) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not(check subtree) || not(sum_ subtree <= width *. width)...
let mode (l: 'a list) : 'a = let rec aux ls ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match ls with | [] -> max_el | x :: xs -> if cur_el = x then if cur_num = 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...
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 comb_l = List.combine l2 l1 in mode comb_l ;; let list = [1;2;3;4;5;1;2;34;4;534;4] ;; pair_mode list;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> if to_unit = Hour then (Hour, (val_ /. 3600.0)) else if to_unit = Second then (Second, val_) else failwith "Invalid unit type" | Hour -> if to_unit = Hour then (Hour, val_) else if to_unit = Second then (...
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> if to_unit = Foot then (Foot, val_) else if to_unit = Meter then (Meter, (val_ *. 0.3048)) else if to_unit = Mile then (Mile, (val_ /. 5280.0)) else failwith "Invalid unit type" | Meter -> if to_unit = Foot...
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dist = convert_dist ((fst(from_unit)), val_) (fst(to_unit)) in let time = convert_time (snd(from_unit), 1.0) (snd(to_unit)) in ((fst(dist), fst(time)), snd(dist) /. snd(time)) ;; convert_speed ((Meter, Second), 10.0) (Mile, Hour);...
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let first_speed = convert_speed a b_unit in (b_unit, (b_val +. snd(first_speed))) ;; add_speed ((Meter, Second), 10.0) ((Mile, Hour), 10.0) ;;
let passes_da_vinci t = helper [t] ;; passes_da_vinci tree_example ;;
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 (compare x cur_el = 0 && compare max_el cur_el = 0) then aux xs (x, cur_num + 1) (max_el, max_num + 1) else if (compare x cur_el = 0 && (cur_num + 1) > max_num) then aux x...
let pair_mode (l: 'a list) = let rec helper l (index : int) (elem : 'a) (temp: ('a * 'a) list) = match l with | [] -> mode temp | x::xs -> helper xs (index + 1) x (List.append [(elem, x)] temp) in if List.length l < 2 then failwith "No pairs are possible" else helper (List.tl l) 1 (List.hd l) [] ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> if to_unit = Hour then (to_unit,(val_ /. 3600.)) else (from_unit,val_) | Hour -> if to_unit = Second then (to_unit,(val_ *. 3600.)) else (from_unit,val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> ( match to_unit with | Meter -> (to_unit,(val_ *. 0.3048)) | Mile -> (to_unit,(val_ /. 5280.)) | _ -> (from_unit,val_)) | Meter -> ( match to_unit with | Foot -> (to_unit,(val_ /. 0.3048)) | Mile -> (to_uni...
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dist,time) = from_unit in let (new_dist,new_time) = to_unit in let (_,convert_dist) = convert_dist (dist, val_) (new_dist) in let (_,convert_time) = convert_time (time, 1.) (new_time) in (to_unit, (convert_dist /. convert_time)) ...
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (_, first_value) = convert_speed a b_unit in (b_unit, (first_value +. b_val)) ;;
let passes_da_vinci t = let rec traverse_list (l : tree list) = match l with | [] -> true | x::xs -> (tree_traversal x ) && (traverse_list xs) and compute_sum (sum : float) (l : tree list) = match l with | [] -> sum | Leaf::xs -> compute_sum sum xs | Branch (width,_)::xs -> compute_sum (sum +. (width ** 2.)) xs and tre...
let mode (l: 'a list) : 'a = if (List.length l = 0) then failwith "Undefined input" else (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 ...
let pair_mode (l: 'a list) : 'a * 'a = if (List.length l < 2) then failwith "Undefined input" else 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 l = (from_unit, val_) in match (fst l, to_unit) with | (Hour, Second) -> (Second, (snd l) *. 3600.) | (Second, Hour) -> (Hour, (snd l) /. 3600.) | _ -> l ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let l = (from_unit, val_) in match (fst l, to_unit) with | (Foot, Mile) -> (Mile, (snd l) /. 5280.) | (Mile, Foot) -> (Foot, (snd l) *. 5280.) | (Meter, Foot) -> (Foot, (snd l) /. 0.3048) | (Foot, Meter) -> (Meter, (snd l) *. 0.3048) | (...
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let l = (from_unit, val_) in if (fst l = to_unit) then l else if (snd (fst l) = snd to_unit) then (to_unit, snd (convert_dist (fst (fst l), snd l) (fst to_unit))) else if (fst (fst l) = fst to_unit) then (to_unit, snd (convert_time (s...
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let l = (b_unit, b_val) in (fst l, snd (convert_speed a (fst l)) +. snd l) ;;
let passes_da_vinci t = let rec sum l acc = match l with | [] -> acc | Leaf :: tl -> sum tl acc | Branch (width, subtree) :: tl -> sum tl (acc +. 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. <= width *. ...
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 if cur_num<max_num then max_el else if cur_el<max_el then cur_el else max_el | hd :: tl -> if hd = cur_el then aux tl (cur_el,cur_num+1) (max_el,max_num) el...
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "empty list!" | hd :: tl -> let l1=List.tl l in let l2=List.rev (List.tl (List.rev l)) in mode (List.combine l2 l1) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match to_unit with | Second -> (match from_unit with | Hour -> (to_unit, val_ *. 3600.) | Second -> (to_unit, val_) ) | Hour -> (match from_unit with | Second -> (to_unit, val_ /. 3600.) | Hour -> (to_unit, val_) ) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match to_unit with | Foot -> (match from_unit with | Meter -> (to_unit, val_ /. 0.3048) | Mile -> (to_unit, val_ *. 5280. ) | Foot -> (to_unit, val_) ) | Meter -> (match from_unit with | Foot -> (to_unit, val_ *. 0.3048) | Mile -> (to_un...
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dist_converted = convert_dist (fst from_unit,val_) (fst to_unit) in let time_converted = convert_time (snd from_unit,1.) (snd to_unit) in (to_unit, (snd dist_converted) /. (snd time_converted)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let spd_a = convert_speed (fst a, snd a) b_unit in (b_unit, snd(spd_a)+. b_val) ;;
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> acc | Branch (width, childtree) :: tl -> sum_ tl ((width** 2.)+. acc) in let rec check l = match l with | [] -> true | Leaf :: tl -> true | Branch (width, subtree) :: tl -> let a=check subtree in let b= (sum_ subtree 0.) <= (width ** ...
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,...