text
stringlengths 0
601k
|
---|
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_ tl (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.)) <= width**2.)) 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 if cur_num = max_num && max_el > cur_el then cur_el else max_el | h :: t -> if h = cur_el 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 match List.sort compare l with | [] -> failwith "Undefined input." | h::t -> aux t (h, 1) (h, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let remove_first l = match l with | [] -> [] | h :: t -> t in match l with | [] -> failwith "Undefined input." | _ -> let l3 = remove_first l in let l4 = remove_first (List.rev l) in let l5 = List.rev l4 in let list_tuple = List.combine l5 l3 in mode list_tuple ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match to_unit with | Second -> if from_unit = Second then (from_unit, val_) else (from_unit, val_*.3600.) | Hour -> if from_unit = Second then (from_unit, val_/.3600.) else (from_unit, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match to_unit with | Foot -> if from_unit = Meter then (from_unit, val_/.0.3048) else if from_unit = Mile then (from_unit, val_*.5280.) else (from_unit, val_) | Meter -> if from_unit = Foot then (from_unit, val_*.0.3048) else if from_unit = Mile then (from_unit, val_*.5280.*.0.3048) else (from_unit, val_) | Mile -> if from_unit = Foot then (from_unit, val_/.5280.) else if from_unit = Meter then (from_unit, val_/.5280./.0.3048) else (from_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let speed_no_time = convert_dist ((fst from_unit), val_) (fst to_unit) in let time_no_speed = convert_time ((snd from_unit), 1.) (snd to_unit) in (from_unit, snd speed_no_time/. snd time_no_speed) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = (fst speed_unit, snd time*. snd (convert_speed (speed_unit, speed_val) (fst speed_unit, fst time))) ;; let tree4 = Branch (5., [ Branch (3., [Leaf; Leaf; Leaf]); Leaf; Branch (4., [])]);; let tree5 = Branch(4., [Leaf]);; let tree6 = Branch(5., [Branch(6., []); Leaf; Branch(4., [])]);; let tree7 = Branch(7., [Branch(5., [Branch(6., []); Leaf; Branch(4., [])]); Leaf]);; let rec passes_da_vinci t = let rec sum_subtree sub acc = match sub with | [] -> acc | Leaf::tl -> sum_subtree tl acc | Branch (width, subtree)::tl -> sum_subtree tl acc +. width*.width in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not (check subtree && (sum_subtree subtree 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 |hd :: tl -> if hd = cur_el && cur_num+1 <= max_num then aux tl (hd, cur_num+1) (max_el, max_num) else if hd = cur_el && cur_num+1 > max_num then aux tl (hd,cur_num+1) (hd,cur_num+1) else aux tl (hd,1) (max_el, max_num) in match l with | [] -> failwith "invalid input" | _ -> aux (List.sort compare(l)) (List.nth (List.sort compare(l)) 0, 0) (List.nth (List.sort compare(l)) 0, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if (List.length l) < 2 then failwith "unvalid input" else if (List.length l)=2 then ((List.nth l 0),(List.nth l 1)) else let original_l = l in mode (List.combine (List.rev (List.tl (List.rev original_l))) (List.tl l)) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if val_ < 0. then failwith "unvalid input" else match from_unit, to_unit with |Second, Second | Hour, Hour -> to_unit, val_ |Second, Hour -> Hour, val_/.3600. |Hour, Second -> Second, val_*.3600. ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if val_ < 0. then failwith "unvalid input" else match (from_unit, to_unit) with |Foot, Foot | Meter, Meter | Mile, Mile -> to_unit, val_ |Foot, Meter -> Meter, val_*.0.3048 |Meter, Foot -> Foot, val_/.0.3048 |Foot, Mile -> Mile, val_/.5280. |Mile, Foot -> Foot, val_*.5280. |Meter, Mile -> Mile, val_/.(0.3048*.5280.) |Mile, Meter -> Meter, val_*.(0.3048*.5280.) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = (to_unit,snd(convert_dist (fst from_unit, snd (convert_time (snd to_unit, val_) (snd from_unit))) (fst 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 = let rec check l = match l with |[] -> true |Leaf :: tl -> check tl |Branch (val_, sub_tree) :: tl -> if (not (check sub_tree) || (val_**2.) < (summation sub_tree 0.)) then false else check tl in check [t] ;; |
let mode (l: 'a list) : 'a = let l = List.sort compare l in match l with | [] -> failwith "Empty list" | hd:: _ -> let rec aux l ((cur_el,cur_num) : ('a * int)) ((max_el,max_num) : ('a * int)) = match l with | 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))) | [] -> max_el in aux l (hd, 0) (hd, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Invalid list" else match l with |_ -> mode (List.combine (List.rev (List.tl (List.rev l))) (List.tl l) ) | [] -> failwith "Empty list" ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Hour -> ( match to_unit with | Second -> from_unit, val_ *. 3600. | Hour -> from_unit , val_ ) | Second -> ( match to_unit with | Second -> from_unit, val_ | Hour -> from_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, val_ /. 1609.344 | Meter -> to_unit, val_) | Mile -> ( match to_unit with | Foot -> to_unit, val_ *. 5280. | Meter -> to_unit, val_ *. 1609.344 | Mile -> to_unit, val_) ;; let getTime(a,b) = b let getDist(a,b) = b let getUnit((a,b), c) = (a,b) let getFirst(a,b) = a;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit with | (_, Second) -> (match to_unit with |(_, Second) ->from_unit, snd( convert_dist (fst(from_unit) , (val_)) (fst(to_unit))) |( _, Hour) -> from_unit, snd(convert_dist (fst(from_unit), val_) (fst(to_unit)) )*. getTime(convert_time (Hour,1.) Second) ) | (_,Hour) -> (match to_unit with |(_, Second) ->from_unit, snd(convert_dist (fst(from_unit), val_) (fst(to_unit)) )*. getTime(convert_time (Second,1.) Hour) |( _, Hour) -> from_unit, snd( convert_dist (fst(from_unit) , (val_)) (fst(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 = let rec summation l current = (match l with | [] -> current | Leaf :: tl -> summation tl current | Branch (width, subtree) :: tl -> summation tl (current +. (width *. width))) in let rec check t = match t with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> (if (not (check (subtree))) or not ( summation subtree 0. <= width *. width) then false else check tl) in check [t] ;; |
let mode (l: 'a list) : 'a = let l = List.sort compare l in match l with | [] -> failwith "Empty list" | hd:: _ -> let rec aux l ((cur_el,cur_num) : ('a * int)) ((max_el,max_num) : ('a * int)) = match l with | 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))) | [] -> max_el in aux l (hd, 0) (hd, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Invalid list" else match l with |_ -> mode (List.combine (List.rev (List.tl (List.rev l))) (List.tl l) ) | [] -> failwith "Empty list" ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Hour -> ( match to_unit with | Second -> from_unit, val_ *. 3600. | Hour -> from_unit , val_ ) | Second -> ( match to_unit with | Second -> from_unit, val_ | Hour -> from_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, val_ /. 1609.344 | Meter -> to_unit, val_) | Mile -> ( match to_unit with | Foot -> to_unit, val_ *. 5280. | Meter -> to_unit, val_ *. 1609.344 | Mile -> to_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit with | (_, Second) -> (match to_unit with |(_, Second) ->from_unit, snd( convert_dist (fst(from_unit) , (val_)) (fst(to_unit))) |( _, Hour) -> from_unit, snd(convert_dist (fst(from_unit), val_) (fst(to_unit)) )*. snd(convert_time (Hour,1.) Second) ) | (_,Hour) -> (match to_unit with |(_, Second) ->from_unit, snd(convert_dist (fst(from_unit), val_) (fst(to_unit)) )*. snd(convert_time (Second,1.) Hour) |( _, Hour) -> from_unit, snd( convert_dist (fst(from_unit) , (val_)) (fst(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 = let rec summation l current = (match l with | [] -> current | Leaf :: tl -> summation tl current | Branch (width, subtree) :: tl -> summation tl (current +. (width *. width))) in let rec check t = match t with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> (if (not (check (subtree))) or not ( summation subtree 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 | [] -> if (cur_num > max_num) then cur_el else max_el | x :: xs -> if (x = cur_el) 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 match l with | [] -> failwith "empty list, no mode" | _ -> aux (List.sort compare l) (List.hd (List.sort compare l), 0) (List.hd (List.sort compare l), 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "empty list, no pair_mode" | _ -> 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, 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, Mile) -> (to_unit, (val_ /. 5280.)) | (Foot, Meter) -> (to_unit, (val_ *. 0.3048)) | (Mile, Foot) -> (to_unit, val_ *. 5280.) | (Mile, Meter) -> (to_unit, val_ *. 1609.344) | (Meter, Mile) -> (to_unit, val_ /. 1609.344) | (Meter, Foot) -> (to_unit, val_ /. 0.3048) | _ -> (to_unit, val_) ;; |
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 traverse t = match t with | [] -> true | x :: xs -> ( match x with | Leaf -> traverse xs | Branch (width, children) -> if (width *. width) < (recurse children) then false else traverse children ) in match t with | Leaf -> true | Branch (width, children) -> if (width *. width) < (recurse children) then false else traverse 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 | [] -> max_el | h::t -> if (h=cur_el) then if (cur_num+1 > max_num) then aux t (cur_el, cur_num+1) (cur_el, cur_num+1) else aux t (cur_el, cur_num+1) (max_el, max_num) else aux t (h, 1) (max_el, max_num) in match l with | [] -> failwith "list cannot be empty" | _ -> aux (List.sort compare l) (List.hd (List.sort compare l), 0) (List.hd (List.sort compare l), 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 = match from_unit with | Second -> (match to_unit with | Second -> (Second, val_) | Hour -> (Hour, val_/.3600.)) | 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 -> (Foot, val_) | Meter -> (Meter, val_*.0.3048) | Mile -> (Mile, val_/.5280.)) | Meter -> (match to_unit with | Foot -> (Foot, val_/.0.3048) | Meter -> (Meter, val_) | Mile -> (Mile, val_/.1609.344)) | Mile -> (match to_unit with | Foot -> (Foot, val_*.5280.) | Meter -> (Meter, val_*.1609.344) | Mile -> (Mile, val_)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit with | (Meter, Second) -> (match to_unit with | (Meter, Second) -> ((Meter, Second), val_) | (Meter, Hour) -> ((Meter, Hour), val_*.3600.) | (Foot, Second) -> ((Foot, Second), val_*.(1./.0.3048)) | (Mile, Second) -> ((Mile, Second), val_/.1609.344) | (Foot, Hour) -> ((Foot, Hour), val_*.3600.*.(1./.0.3048)) | (Mile, Hour) -> ((Mile, Hour), val_*.3600./.1609.344) ) | (Meter, Hour) -> (match to_unit with | (Meter, Second) -> ((Meter, Second), val_/.3600.) | (Meter, Hour) -> ((Meter, Hour), val_) | (Foot, Second) -> ((Foot, Second), val_/.3600.*.(1./.0.3048)) | (Mile, Second) -> ((Mile, Second), val_/.3600./.1609.344) | (Foot, Hour) -> ((Foot, Hour), val_*.(1./.0.3048)) | (Mile, Hour) -> ((Mile, Hour), val_/.1609.344) ) | (Foot, Second) -> (match to_unit with | (Meter, Second) -> ((Meter, Second), val_/.(1./.0.3048)) | (Meter, Hour) -> ((Meter, Hour), val_*.3600./.(1./.0.3048)) | (Foot, Second) -> ((Foot, Second), val_) | (Mile, Second) -> ((Mile, Second), val_*.0.000189394) | (Foot, Hour) -> ((Foot, Hour), val_*.3600.) | (Mile, Hour) -> ((Mile, Hour), val_*.3600./.5280.) ) | (Foot, Hour) -> (match to_unit with | (Meter, Second) -> ((Meter, Second), val_/.3600./.(1./.0.3048)) | (Meter, Hour) -> ((Meter, Hour), val_/.(1./.0.3048)) | (Foot, Second) -> ((Foot, Second), val_/.3600.) | (Mile, Second) -> ((Mile, Second), val_/.3600.*.0.000189394) | (Foot, Hour) -> ((Foot, Hour), val_) | (Mile, Hour) -> ((Mile, Hour), val_*.0.681818/.3600.) ) | (Mile, Second) -> (match to_unit with | (Meter, Second) -> ((Meter, Second), val_*.5280.*.0.3048) | (Meter, Hour) -> ((Meter, Hour), val_*.5793638.4) | (Foot, Second) -> ((Foot, Second), val_*.5280.) | (Mile, Second) -> ((Mile, Second), val_) | (Foot, Hour) -> ((Foot, Hour), val_*.5280.*.3600.) | (Mile, Hour) -> ((Mile, Hour), val_*.3600.) ) | (Mile, Hour) -> (match to_unit with | (Meter, Second) -> ((Meter, Second), val_*.5280.*.0.3048/.3600.) | (Meter, Hour) -> ((Meter, Hour), val_*.5793638.4/.3600.) | (Foot, Second) -> ((Foot, Second), val_*.5280./.3600.) | (Mile, Second) -> ((Mile, Second), val_/.3600.) | (Foot, Hour) -> ((Foot, Hour), val_*.5280.) | (Mile, Hour) -> ((Mile, Hour), val_) ) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (x, y) = time in let (a, b) = speed_unit in let (_, lu) = convert_time (x, y) b in (a, (speed_val *. lu)) ;; let rec helper l acc = match l with | [] -> acc | Leaf::t -> helper t acc | Branch (value, list)::t -> helper t ((value*.value) +. acc) let rec passes_da_vinci t = match t with | Leaf -> true | Branch (value, list) -> ((helper list 0.0) <= (value*.value)) ;; |
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 (cur_el, cur_num+1) (cur_el, cur_num+1) else if (cur_num+1 = max_num && cur_el < max_el) 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 if (max_num = 1 && x < max_el) then aux xs (x, 1) (x, 1) else aux xs (x, 1) (max_el, max_num) in match l with |[] -> failwith "Undefined Input" |head :: tail -> aux (List.sort compare l) (List.hd (List.sort compare l), 0) (List.hd (List.sort compare l), 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Undefined Input" else let rec tupleListBuilder (l: 'a list) (newList: ('a *'a) list) (cur_el: 'a) = match l with |[] -> newList |head :: tail -> tupleListBuilder tail (newList @ [(cur_el, head)]) head in match l with | [] -> failwith "Undefined Input" | head :: tail -> mode(tupleListBuilder tail [] head) ;; |
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.) |_ -> failwith "Undefined Input" ) |Hour -> (match to_unit with |Second -> (to_unit, (val_*.3600.)) |Hour -> (to_unit, val_) |_ -> failwith "Undefined Input" ) |_ -> failwith "Undefined Input" ;; |
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 |Meter -> (to_unit, val_) |Foot -> (to_unit, val_/.0.3048) |Mile -> (to_unit, val_/.0.3048/.5280.) |_ -> failwith "Undefined Input" ) |Mile -> (match to_unit with |Mile -> (to_unit, val_) |Foot -> (to_unit, val_*.5280.) |Meter -> (to_unit, val_*.5280.*.0.3048) |_ -> failwith "Undefined Input" ) |_ -> failwith "Undefined Input" ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit with |(Meter, Second) -> (match to_unit with |(Meter, Second) -> (Meter, Second), snd(convert_dist (Meter, val_) Meter) /. snd(convert_time (Second, 1.) Second) |(Meter, Hour) -> (Meter, Hour), snd(convert_dist (Meter, val_) Meter) /. snd(convert_time (Second, 1.) Hour) |(Foot, Second) -> (Foot, Second), snd(convert_dist (Meter, val_) Foot) /. snd(convert_time (Second, 1.) Second) |(Foot, Hour) -> (Foot, Hour), snd(convert_dist (Meter, val_) Foot) /. snd(convert_time (Second, 1.) Hour) |(Mile, Second) -> (Mile, Second), snd(convert_dist (Meter, val_) Mile) /. snd(convert_time (Second, 1.) Second) |(Mile, Hour) -> (Mile, Hour), snd(convert_dist (Meter, val_) Mile) /. snd(convert_time (Second, 1.) Hour)) |(Meter, Hour) -> (match to_unit with |(Meter, Second) -> (Meter, Second), snd(convert_dist (Meter, val_) Meter) /. snd(convert_time (Hour, 1.) Second) |(Meter, Hour) -> (Meter, Hour), snd(convert_dist (Meter, val_) Meter) /. snd(convert_time (Hour, 1.) Hour) |(Foot, Second) -> (Foot, Second), snd(convert_dist (Meter, val_) Foot) /. snd(convert_time (Hour, 1.) Second) |(Foot, Hour) -> (Foot, Hour), snd(convert_dist (Meter, val_) Foot) /. snd(convert_time (Hour, 1.) Hour) |(Mile, Second) -> (Mile, Second), snd(convert_dist (Meter, val_) Mile) /. snd(convert_time (Hour, 1.) Second) |(Mile, Hour) -> (Mile, Hour), snd(convert_dist (Meter, val_) Mile) /. snd(convert_time (Hour, 1.) Hour) ) |(Foot, Second) -> (match to_unit with |(Meter, Second) -> (Meter, Second), snd(convert_dist (Foot, val_) Meter) /. snd(convert_time (Second, 1.) Second) |(Meter, Hour) -> (Meter, Hour), snd(convert_dist (Foot, val_) Meter) /. snd(convert_time (Second, 1.) Hour) |(Foot, Second) -> (Foot, Second), snd(convert_dist (Foot, val_) Foot) /. snd(convert_time (Second, 1.) Second) |(Foot, Hour) -> (Foot, Hour), snd(convert_dist (Foot, val_) Foot) /. snd(convert_time (Second, 1.) Hour) |(Mile, Second) -> (Mile, Second), snd(convert_dist (Foot, val_) Mile) /. snd(convert_time (Second, 1.) Second) |(Mile, Hour) -> (Mile, Hour), snd(convert_dist (Foot, val_) Mile) /. snd(convert_time (Second, 1.) Hour) ) |(Foot, Hour) -> (match to_unit with |(Meter, Second) -> (Meter, Second), snd(convert_dist (Foot, val_) Meter) /. snd(convert_time (Hour, 1.) Second) |(Meter, Hour) -> (Meter, Hour), snd(convert_dist (Foot, val_) Meter) /. snd(convert_time (Hour, 1.) Hour) |(Foot, Second) -> (Foot, Second), snd(convert_dist (Foot, val_) Foot) /. snd(convert_time (Hour, 1.) Second) |(Foot, Hour) -> (Foot, Hour), snd(convert_dist (Foot, val_) Foot) /. snd(convert_time (Hour, 1.) Hour) |(Mile, Second) -> (Mile, Second), snd(convert_dist (Foot, val_) Mile) /. snd(convert_time (Hour, 1.) Second) |(Mile, Hour) -> (Mile, Hour), snd(convert_dist (Foot, val_) Mile) /. snd(convert_time (Hour, 1.) Hour) ) |(Mile, Second) -> (match to_unit with |(Meter, Second) -> (Meter, Second), snd(convert_dist (Mile, val_) Meter) /. snd(convert_time (Second, 1.) Second) |(Meter, Hour) -> (Meter, Hour), snd(convert_dist (Mile, val_) Meter) /. snd(convert_time (Second, 1.) Hour) |(Foot, Second) -> (Foot, Second), snd(convert_dist (Mile, val_) Foot) /. snd(convert_time (Second, 1.) Second) |(Foot, Hour) -> (Foot, Hour), snd(convert_dist (Mile, val_) Foot) /. snd(convert_time (Second, 1.) Hour) |(Mile, Second) -> (Mile, Second), snd(convert_dist (Mile, val_) Mile) /. snd(convert_time (Second, 1.) Second) |(Mile, Hour) -> (Mile, Hour), snd(convert_dist (Mile, val_) Mile) /. snd(convert_time (Second, 1.) Hour) ) |(Mile, Hour) -> (match to_unit with |(Meter, Second) -> (Meter, Second), snd(convert_dist (Mile, val_) Meter) /. snd(convert_time (Hour, 1.) Second) |(Meter, Hour) -> (Meter, Hour), snd(convert_dist (Mile, val_) Meter) /. snd(convert_time (Hour, 1.) Hour) |(Foot, Second) -> (Foot, Second), snd(convert_dist (Mile, val_) Foot) /. snd(convert_time (Hour, 1.) Second) |(Foot, Hour) -> (Foot, Hour), snd(convert_dist (Mile, val_) Foot) /. snd(convert_time (Hour, 1.) Hour) |(Mile, Second) -> (Mile, Second), snd(convert_dist (Mile, val_) Mile) /. snd(convert_time (Hour, 1.) Second) |(Mile, Hour) -> (Mile, Hour), snd(convert_dist (Mile, val_) Mile) /. snd(convert_time (Hour, 1.) Hour) ) |_ -> failwith "Undefined Input" ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = ((b_unit), (b_val +. snd(convert_speed ((fst (fst a), snd (fst a)), snd a) (b_unit)))) ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> acc +. sum_ tl acc | Branch (width, subtree) :: tl -> acc +. (width*.width) +. sum_ tl acc in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if((check subtree) && (sum_ 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 | head :: tail -> if head = cur_el then aux tail (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux tail (head, 1) (cur_el, cur_num) else aux tail (head, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "Invalid list" | head :: tail -> aux tail (head, 1) (head, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "error" else let (_ :: no_front) = l in let (_ :: no_back) = List.rev l in let l1 = List.rev no_back in let tupleList = List.combine l1 no_front in mode tupleList ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with | (Hour, Hour) -> (from_unit, val_) | (Hour, Second) -> (Second, val_ *. 3600.) | (Second, Hour) -> (Hour, val_ /. 3600.) | (Second, Second) -> (from_unit, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | (Foot, Foot) -> (from_unit, val_) | (Meter, Meter) -> (from_unit, val_) | (Mile, Mile) -> (from_unit, val_) | (Foot, Meter) -> (Meter, val_ *. 0.3048) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Meter, Mile) -> (Mile, val_ /. 1609.344) | (Mile, Meter) -> (Meter, val_ *. 1609.344) | (Mile, Foot) -> (Foot, val_ *. 5280.) | (Foot, Mile) -> (Mile, val_ /. 5280.) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let firstDistance = fst(from_unit) in let secondDistance = fst(to_unit) in let firstTime = snd(from_unit) in let secondTime = snd(to_unit) in let (_,distance) = convert_dist (firstDistance, val_) secondDistance in let (_,time) = convert_time (firstTime, 1.) secondTime in ((secondDistance, secondTime), distance/.time) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (a_unit, a_val) = a in let (_,speed) = convert_speed (a_unit, a_val) b_unit in (b_unit, b_val +. speed) ;; |
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 ((check subtree) && (sum_ subtree 0. <= width ** 2.)) then check tl else false in check [t] ;; |
let mode (l: 'a list) : 'a = let rec aux (lst: 'a list) ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match lst with | [] -> if cur_num > max_num then cur_el else max_el | head::tail when head = cur_el -> aux tail (cur_el, cur_num + 1) (max_el, max_num) | head::tail when cur_num > max_num -> aux tail (head, 1) (cur_el, cur_num) | head::tail -> aux tail (head, 1) (max_el, max_num) in match (List.sort compare l) with | [] -> failwith "fail - too short" | head::tail -> aux (List.sort compare l) (head, 0) (head, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec squash l : (('a * 'a) list) = match l with | [] -> [] | x::xs when xs = [] -> [] | x::xs -> (List.nth l 0, List.nth l 1) :: squash xs in match l with | [] -> failwith "List is too short!" | x :: xs when xs = [] -> failwith "List is too short!" | _ -> mode (squash l) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second when to_unit = Hour -> (to_unit, (val_/.3600.)) | Hour when to_unit = 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 with | Foot when to_unit = Meter -> (to_unit, 0.3048 *. val_) | Foot when to_unit = Mile -> (to_unit, (1./.5280.) *. val_) | Meter when to_unit = Foot -> (to_unit, (1./.0.3048) *. val_) | Meter when to_unit = Mile -> (to_unit, ((1./.0.3048) *. (1./.5280.)) *. val_) | Mile when to_unit = Foot -> (to_unit, 5280. *.val_) | Mile when to_unit = Meter -> (to_unit, (5280. *. 0.3048 *. val_)) | _ -> (to_unit, val_) ;; let opposite (time: time_unit) : time_unit = match time with | Hour -> Second | Second -> Hour ;; |
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 (u1, value1) = convert_dist (from_dist, val_) to_dist in ( let (u2, value2) = convert_time ((opposite from_time), value1) (opposite to_time) in (to_unit, value2)))) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let ((a_dist_unit, a_time_unit), a_val) = a in ( let ((b_dist_unit, b_time_unit), converted_a_val) = convert_speed ((a_dist_unit, a_time_unit), a_val) b_unit in ( (b_unit, converted_a_val +. b_val) ) ) ;; |
let passes_da_vinci t = match t with | Leaf -> true | Branch (thickness, branches) -> thickness ** 2. >= add_branch_thickness branches ;; |
let mode (l: 'a list) : 'a = let rec aux (lst: 'a list) ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match lst with | [] -> if cur_num > max_num then cur_el else max_el | head::tail when head = cur_el -> aux tail (cur_el, cur_num + 1) (max_el, max_num) | head::tail when cur_num > max_num -> aux tail (head, 1) (cur_el, cur_num) | head::tail -> aux tail (head, 1) (max_el, max_num) in match (List.sort compare l) with | [] -> failwith "fail - too short" | head::tail -> aux (List.sort compare l) (head, 0) (head, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec squash l : (('a * 'a) list) = match l with | [] -> [] | x::xs when xs = [] -> [] | x::xs -> (List.nth l 0, List.nth l 1) :: squash xs in match l with | [] -> failwith "List is too short!" | x :: xs when xs = [] -> failwith "List is too short!" | _ -> mode (squash l) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second when to_unit = Hour -> (to_unit, (val_/.3600.)) | Hour when to_unit = 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 with | Foot when to_unit = Meter -> (to_unit, 0.3048 *. val_) | Foot when to_unit = Mile -> (to_unit, (1./.5280.) *. val_) | Meter when to_unit = Foot -> (to_unit, (1./.0.3048) *. val_) | Meter when to_unit = Mile -> (to_unit, ((1./.0.3048) *. (1./.5280.)) *. val_) | Mile when to_unit = Foot -> (to_unit, 5280. *.val_) | Mile when to_unit = Meter -> (to_unit, (5280. *. 0.3048 *. val_)) | _ -> (to_unit, val_) ;; let opposite (time: time_unit) : time_unit = match time with | Hour -> Second | Second -> Hour ;; |
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 (u1, value1) = convert_dist (from_dist, val_) to_dist in ( let (u2, value2) = convert_time ((opposite from_time), value1) (opposite to_time) in (to_unit, value2)))) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let ((a_dist_unit, a_time_unit), a_val) = a in ( let ((b_dist_unit, b_time_unit), converted_a_val) = convert_speed ((a_dist_unit, a_time_unit), a_val) b_unit in ( (b_unit, converted_a_val +. b_val) ) ) ;; |
let passes_da_vinci t = match t with | Leaf -> true | Branch (thickness, branches) -> if thickness ** 2. >= add_branch_thickness branches then passes_da_vinci_children branches else false ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = if (List.length l) = 0 then max_el else ( let next_el = List.hd l in let next_num = if cur_el = next_el then cur_num + 1 else 1 in if next_num > max_num then aux (List.tl l) (next_el, next_num) (next_el, next_num) else aux (List.tl l) (next_el, next_num) (max_el, max_num) ) in if (List.length l) = 0 then failwith "Empty list" else let sorted_l = List.sort compare l in aux sorted_l (List.hd sorted_l, 0) (List.hd sorted_l, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let get_pairs (i: int) (a: 'a) : 'a * 'a = if i == (List.length l) - 1 then (a, a) else let snd = List.nth l (i+1) in (a, snd) in if (List.length l) <= 1 then failwith "List less or equal to 1 element" else mode (List.tl (List.rev (List.mapi get_pairs l))) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = (to_unit, (match (from_unit, to_unit) with | (Second, Hour) -> val_ /. 3600. | (Hour, Second) -> val_ *. 3600. | _ -> val_)) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = (to_unit, (match (from_unit, to_unit) with | (Foot, Meter) -> val_ *. 0.3048 | (Mile, Foot) -> val_ *. 5280. | (Meter, Foot) -> val_ /. 0.3048 | (Foot, Mile) -> val_ /. 5280. | (Mile, Meter) -> val_ *. 5280. *. 0.3048 | (Meter, Mile) -> val_ /. 5280. /. 0.3048 | _ -> val_)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_dist_unit = fst from_unit in let from_time_unit = snd from_unit in let to_dist_unit = fst to_unit in let to_time_unit = snd to_unit in if from_unit = to_unit then (to_unit, val_) else (to_unit, 1. /. (snd (convert_time (from_time_unit, (1. /. (snd (convert_dist (from_dist_unit, val_) to_dist_unit)))) to_time_unit))) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let converted_a_val = snd (convert_speed a b_unit) in (b_unit, converted_a_val +. b_val) ;; |
let passes_da_vinci t = let rec sum_sq l = match l with | [] -> 0. | Leaf :: _ -> sum_sq (List.tl l) | Branch (width, _) :: _ -> width *. width +. sum_sq (List.tl l) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> width *. width >= (sum_sq subtree) && check subtree && check tl in check [t] ;; |
let getfirst(a, _) = a;; let getsecond(_, b) = b;; |
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) > 0 then cur_el else max_el | x::xs -> if (compare cur_el x) = 0 then aux xs (cur_el, cur_num + 1) (max_el, max_num) else if (compare cur_num max_num) > 0 && (compare cur_el x) = 0 then aux xs (cur_el, cur_num + 1) (cur_el, cur_num) else if (compare cur_num max_num) > 0 && (compare cur_el x) != 0 then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 1) (max_el, max_num) in let value = List.sort compare l in match value with | [] -> failwith "" | x::xs -> aux xs (x, 1) (x, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec aux (x: 'a list) (y: 'a list) l = match l with |[] -> failwith "" | b::[] -> mode (List.combine (x) (y @ [b])) | b::bs -> aux (x @ [b]) (y @ [b]) bs in match l with | [] -> failwith "" | x::xs -> aux [x] [] xs ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let first = getfirst (from_unit, val_) in let second = getsecond (from_unit, val_) in if first = to_unit then (from_unit, second) else if to_unit = Hour then (Hour, second *. (1.0/.3600.0)) else (Second, second *. 3600.0) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let first = getfirst (from_unit, val_) in let second = getsecond (from_unit, val_) in if first = to_unit then (from_unit, second) else if first = Foot then match to_unit with | Meter -> (Meter, second *. 0.3048) | Mile -> (Mile, second *. 1.0/.5280.0) | _ -> failwith "" else if first = Mile then match to_unit with | Foot -> (Foot, second *. 5280.0) | Meter -> (Meter, second *. 5280.0 *. 0.3048) | _ -> failwith "" else match to_unit with | Foot -> (Foot, second /. 0.3048) | Mile -> (Mile, (second /.0.3048) *. (1.0/.5280.0)) | _ -> failwith "" ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((from_dist,from_time),(to_dist,to_time)) = (from_unit, to_unit) in let (dist, dist_val) = convert_dist (from_dist, val_) to_dist in let (time, time_val) = convert_time (from_time, 1.) to_time in ((dist, time), (dist_val /. time_val)) ;; |
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, b_val +. getsecond value) ;; |
let passes_da_vinci (t : tree) : bool = let rec auxu (x: float) (y: 'a list) = match y with | [] -> true | Leaf :: ys -> auxu (x) (ys) | Branch (widthx, subtree) :: ys -> let width = x *. x in let children = (getneighbours (y) 0.0) in if (compare width children) < 0 then false else let first = auxu (widthx) (subtree) in first && (auxu x ys) in match t with | Leaf -> true | Branch( _ , _ ) -> auxu (getfirstele t) (getsecondele t) ;; |
let getfirst(a, _) = a;; let getsecond(_, b) = b;; |
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) > 0 then cur_el else max_el | x::xs -> if (compare cur_el x) = 0 then aux xs (cur_el, cur_num + 1) (max_el, max_num) else if (compare cur_num max_num) > 0 && (compare cur_el x) = 0 then aux xs (cur_el, cur_num + 1) (cur_el, cur_num) else if (compare cur_num max_num) > 0 && (compare cur_el x) != 0 then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 1) (max_el, max_num) in let value = List.sort compare l in match value with | [] -> failwith "Input Error" | x::xs -> aux xs (x, 1) (x, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec aux (x: 'a list) (y: 'a list) l = match l with |[] -> failwith "Input Error" | b::[] -> mode (List.combine (x) (y @ [b])) | b::bs -> aux (x @ [b]) (y @ [b]) bs in match l with | [] -> failwith "Input Error" | x::xs -> aux [x] [] xs ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let first = getfirst (from_unit, val_) in let second = getsecond (from_unit, val_) in if first = to_unit then (from_unit, second) else if to_unit = Hour then (Hour, second *. (1.0/.3600.0)) else (Second, second *. 3600.0) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let first = getfirst (from_unit, val_) in let second = getsecond (from_unit, val_) in if first = to_unit then (from_unit, second) else if first = Foot then match to_unit with | Meter -> (Meter, second *. 0.3048) | Mile -> (Mile, second *. 1.0/.5280.0) | _ -> failwith "Input Error" else if first = Mile then match to_unit with | Foot -> (Foot, second *. 5280.0) | Meter -> (Meter, second *. 5280.0 *. 0.3048) | _ -> failwith "Input Error" else match to_unit with | Foot -> (Foot, second /. 0.3048) | Mile -> (Mile, (second /.0.3048) *. (1.0/.5280.0)) | _ -> failwith "Input Error" ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((from_distance, from_time),(to_distance, to_time)) = (from_unit, to_unit) in let (distance, dist_value) = convert_dist (from_distance, val_) to_distance in let (time, time_value) = convert_time (from_time, 1.) to_time in ((distance, time), (dist_value /. time_value)) ;; |
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, b_val +. getsecond value) ;; |
let passes_da_vinci (t : tree) : bool = let rec auxu (x: float) (y: 'a list) = match y with | [] -> true | Leaf :: ys -> auxu (x) (ys) | Branch (widthx, subtree) :: ys -> let width = x *. x in let children = (getneighbours (y) 0.0) in if (compare width children) < 0 then false else let first = auxu (widthx) (subtree) in first && (auxu x ys) in match t with | Leaf -> true | Branch( _ , _ ) -> auxu (getfirstele t) (getsecondele t) ;; |
let getfirst(a, _) = a;; let getsecond(_, b) = b;; |
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) > 0 then cur_el else max_el | x::xs -> if (compare cur_el x) = 0 then aux xs (cur_el, cur_num + 1) (max_el, max_num) else if (compare cur_num max_num) > 0 && (compare cur_el x) = 0 then aux xs (cur_el, cur_num + 1) (cur_el, cur_num) else if (compare cur_num max_num) > 0 && (compare cur_el x) != 0 then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 1) (max_el, max_num) in let value = List.sort compare l in match value with | [] -> failwith "Input Error" | x::xs -> aux xs (x, 1) (x, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec aux (x: 'a list) (y: 'a list) l = match l with |[] -> failwith "Input Error" | b::[] -> mode (List.combine (x) (y @ [b])) | b::bs -> aux (x @ [b]) (y @ [b]) bs in match l with | [] -> failwith "Input Error" | x::xs -> aux [x] [] xs ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let first = getfirst (from_unit, val_) in let second = getsecond (from_unit, val_) in if first = to_unit then (from_unit, second) else if to_unit = Hour then (Hour, second *. (1.0/.3600.0)) else (Second, second *. 3600.0) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let first = getfirst (from_unit, val_) in let second = getsecond (from_unit, val_) in if first = to_unit then (from_unit, second) else if first = Foot then match to_unit with | Meter -> (Meter, second *. 0.3048) | Mile -> (Mile, second *. 1.0/.5280.0) | _ -> failwith "Input Error" else if first = Mile then match to_unit with | Foot -> (Foot, second *. 5280.0) | Meter -> (Meter, second *. 5280.0 *. 0.3048) | _ -> failwith "Input Error" else match to_unit with | Foot -> (Foot, second /. 0.3048) | Mile -> (Mile, (second /.0.3048) *. (1.0/.5280.0)) | _ -> failwith "Input Error" ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((from_distance, from_time),(to_distance, to_time)) = (from_unit, to_unit) in let (distance, dist_value) = convert_dist (from_distance, val_) to_distance in let (time, time_value) = convert_time (from_time, 1.) to_time in ((distance, time), (dist_value /. time_value)) ;; |
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, b_val +. getsecond value) ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.