text
stringlengths 0
601k
|
---|
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 -> if to_unit = Meter then (Meter,val_*.0.3048) else (Mile, val_/.5280.) | Meter-> if to_unit = Foot then (Foot,val_/.0.3048) else (Mile, val_/.1609.344) | Mile -> if to_unit = Foot then (Foot, val_*.5280.) else (Meter,val_*.1609.344) ;; |
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 (_,dist_val) = convert_dist (from_dist,val_) to_dist in let (_,time_ratio) = convert_time (from_time,1.) to_time in ((to_dist,to_time),dist_val/.time_ratio) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (from_time_unit, time_val) = time in let (dist_unit, to_time_unit) = speed_unit in let (_,converted_time) = convert_time (from_time_unit,time_val) to_time_unit in dist_unit,(speed_val *. converted_time) ;; let width_subtree tree_list = let rec findSum tree_list acc = match tree_list with | [] -> acc | h::t ->( match h with | Leaf -> findSum t acc | Branch (width,_) -> findSum t (acc+.(width*.width)) ) in findSum tree_list 0. ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch (width, subTree) -> if (width_subtree subTree) <= (width*.width) then match subTree with | [] -> true | h::_ -> passes_da_vinci h else 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 | x::xs -> if cur_num = -1 then aux xs (x,1)(x,1) else if x<>cur_el && cur_num>max_num then aux xs (x,1)(cur_el,cur_num) else if x<>cur_el && cur_num<=max_num then aux xs (x,1)(max_el,max_num) else aux xs (x,cur_num+1)(max_el,max_num) in let newList = List.sort compare l in let head = match newList with | []-> failwith "Invalid input" | h::_ -> h in aux (newList) (head,-1) (head,0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec create_tuples l acc = let get_tail l = match l with | _::t -> t | [] -> failwith "Was not supposed to happend" in match l with | a::b::_ -> create_tuples (get_tail l) ((a,b)::acc) | _ -> acc in mode (create_tuples l []) ;; |
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 = Hour then (Second, val_*.3600.) else (Hour, 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 -> if to_unit = Meter then (Meter,val_*.0.3048) else (Mile, val_/.5280.) | Meter-> if to_unit = Foot then (Foot,val_/.0.3048) else (Mile, val_/.1609.344) | Mile -> if to_unit = Foot then (Foot, val_*.5280.) else (Meter,val_*.1609.344) ;; |
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 (_,dist_val) = convert_dist (from_dist,val_) to_dist in let (_,time_ratio) = convert_time (from_time,1.) to_time in ((to_dist,to_time),dist_val/.time_ratio) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (from_time_unit, time_val) = time in let (dist_unit, to_time_unit) = speed_unit in let (_,converted_time) = convert_time (from_time_unit,time_val) to_time_unit in dist_unit,(speed_val *. converted_time) ;; let width_subtree tree_list = let rec findSum tree_list acc = match tree_list with | [] -> acc | h::t ->( match h with | Leaf -> findSum t acc | Branch (width,_) -> findSum t (acc+.(width*.width)) ) in findSum tree_list 0. ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch (width, subTree) -> if (width_subtree subTree) <= (width*.width) then match subTree with | [] -> true | h::_ -> passes_da_vinci h else false ;; |
let mode (l: 'a list) : 'a = if l = [] then notimplemented () 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 |x::xs -> if x = max_el || (x = cur_el && cur_num+1 > max_num) then aux (xs) (x, cur_num+1) (x, cur_num+1) else if x = cur_el then aux (xs) (x, cur_num+1) (max_el, max_num) else aux (xs) (x,1) (max_el, max_num) in 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 notimplemented () else ( let l1 = List.tl (l) in let l2 = List.rev (List.tl (List.rev (l))) in let pairs = List.combine (l2) (l1) in mode (pairs) ) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if val_ < 0. then notimplemented() else match from_unit with |Second -> if to_unit = Second then (Second, val_) else (Hour, val_/.3600.) |Hour -> if to_unit = Hour then (Hour, val_) else (Second, val_*.3600.) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if val_ < 0. then notimplemented() else match from_unit with |Foot -> if to_unit = Foot then (Foot, val_) else (if to_unit = Meter then (Meter, val_*.0.3048) else (Mile, val_/.5280.)) |Meter -> if to_unit = Meter then (Meter, val_) else (if to_unit = Foot then (Foot, val_/.0.3048) else (Mile, val_/.1609.344)) |Mile -> if to_unit = Mile then (Mile, val_) else (if to_unit = Foot then (Foot, val_*.5280.) else (Meter, val_*.1609.344)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if val_ < 0. then notimplemented() else let to_dist_val = snd (convert_dist (fst from_unit, val_) (fst to_unit)) in let to_time_val = snd (convert_time (snd from_unit, 1.) (snd to_unit)) in ((fst to_unit, snd to_unit), to_dist_val/.to_time_val) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = if snd a < 0. || b_val < 0. then notimplemented () else let to_b_unit_val = snd (convert_speed (a) (b_unit)) in ((b_unit), b_val +. to_b_unit_val) ;; |
let passes_da_vinci t = iterate [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 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 match List.sort compare l with | [] -> failwith "Undefined input." | x::xs -> aux xs (x,1) (x,0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec duplicate l = match l with | x::xs::[] -> (x,xs) :: [] | x::y::ys -> (x,y) :: duplicate (y::ys) in let l = duplicate l in mode l ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit,val_) with | (Second,t) -> (match to_unit with | Second -> (Second,t) | Hour -> (Second, t /. 3600.0) ) | (Hour,t) -> match to_unit with | Hour -> (Hour,t) | Second -> (Second, t *. 3600.0) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit,val_) with | (Foot,d) -> (match to_unit with | Foot -> (Foot,d) | Meter -> (Meter, d *. 0.3048) | Mile -> (Mile, d /. 5280.0 )) | (Meter,d) -> (match to_unit with | Meter -> (Meter, d) | Mile -> (Mile, (d /. 5280.0) /. 0.3048) | Foot -> (Foot, (d /. 0.3048 ))) | (Mile,d) -> (match to_unit with | Mile -> (Mile, d) | Foot -> (Foot, d *. 5280.0 ) | Meter -> (Meter, d *. 5280.0 *. 0.3048 )) ;; |
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 to_unit),(snd dist)) (snd from_unit) in (to_unit, (snd time)) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let ctime = convert_time ((fst time),(snd time)) (snd speed_unit) in ((fst speed_unit), ((snd ctime) *. speed_val)) ;; let sum_sq t = match t with | Leaf -> 0. | Branch (_,subtree) -> let rec children st = match st with | [] -> 0. | Leaf :: tl -> children tl | Branch (w,_) :: tl -> w *. w +. children tl in children subtree let rec passes_da_vinci t = let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width,subtree) :: tl -> if (width ** 2.0 >= (sum_sq (Branch(width,subtree)))) && (check subtree) 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 List.sort compare 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 List.sort compare l with | [] -> failwith "Inappropriate input." | x :: xs -> aux xs (x, 1) (x, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec temp l pairs= match l with | x :: y :: z -> temp (y::z) ((x,y)::pairs) | _ -> pairs in if List.length l < 2 then failwith "Inappropriate input." else mode (temp 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=Hour && to_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 with | Meter -> if to_unit=Foot then (to_unit,val_ /. 0.3048) else (to_unit,val_ /. 0.3048 /. 5280.) | Foot -> if to_unit=Mile then(to_unit,val_ /. 5280.) else (to_unit,val_ *. 0.3048) | Mile -> if to_unit=Foot then (to_unit,val_ *. 5280.) else (to_unit,val_ *. 5280. *. 0.3048) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if from_unit = to_unit then (to_unit,val_) else (to_unit,snd(convert_dist ((fst(from_unit)),val_) (fst(to_unit))) /. snd(convert_time (snd(from_unit),1.0) (snd(to_unit)))) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = if fst(a) = b_unit then (b_unit,b_val+.snd(a)) else (b_unit,b_val+.snd(convert_speed(fst(a),snd(a)) (b_unit))) ;; |
let passes_da_vinci t= let rec temp l b acc width= match l with | [] -> acc <= width **2. | Leaf::tl -> temp tl b acc width | Branch (width1, subtree)::tl -> if temp subtree true 0. width1 then temp tl (b && (temp subtree true 0. width1)) (acc +. width1 ** 2.) width else false in match t with | Leaf -> true | Branch(width, subtree) -> temp subtree true 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 if (cur_num+1 > max_num) then aux (tl) (hd,cur_num+1) (cur_el,cur_num+1) else aux (tl) (hd,cur_num+1) (max_el,max_num) else aux (tl) (hd,1) (max_el,max_num) in if (List.length l) = 0 then failwith "invalid input" else let l = List.sort compare l in aux (List.tl l) (List.hd l,1) (List.hd l,1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "invalid input" else let copy1 = (List.tl l) in let copy2 = List.rev (List.tl (List.rev l)) in mode(List.combine copy2 copy1) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit,to_unit) with | (Second, Second) | (Hour, Hour) -> to_unit,val_ | (Hour, Second) -> to_unit, val_ *. 3600. | (Second, Hour) -> to_unit, val_ /. 3600. ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match(from_unit, to_unit) with | (Foot, Foot) | (Meter, Meter) | (Mile, Mile)-> to_unit, val_ | (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, Meter)-> to_unit, val_ *. 5280. *. 0.3048 | (Mile, Foot)-> to_unit, val_ *. 5280. ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let converted_distance = snd(convert_dist (fst(from_unit), val_) (fst(to_unit))) in let converted_time = snd(convert_time (snd(from_unit), 1.) (snd(to_unit))) in to_unit, converted_distance /. converted_time ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let copy = convert_speed (fst(a), snd(a)) (b_unit) in let sum = snd(copy) +. b_val in b_unit,sum ;; |
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 max_el else cur_el | x :: xs -> if x = cur_el then aux xs (x,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 (List.sort compare(l)) with | [] -> failwith " The list is empty." | x :: xs -> aux xs (x,1) (x,0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec pair_list l (lp: ('a * 'a) list) = match l with | [] -> failwith "emptyList" | [x] -> lp | x :: xs -> pair_list xs (List.append lp [(x,List.hd xs)]) in mode (pair_list l []) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit , to_unit with | Hour , Second -> (to_unit, val_ *. 3600.) | Second , Hour -> (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 (_, dist_converted_val) = convert_dist (fst from_unit, val_) (fst to_unit) in let (_, inv_factor) = convert_time (snd from_unit, 1.) (snd to_unit) in (to_unit, dist_converted_val /. inv_factor) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (dist_in_unit, time_speed_unit) = speed_unit in let (_, time_in_secunit) = convert_time time time_speed_unit in (dist_in_unit, speed_val *. time_in_secunit) ;; let rec passes_da_vinci t = let rec sum_sub li sum = match li with |[] -> sum |x :: xs -> if x = Leaf then sum_sub xs sum else let Branch(width , li) = x in sum_sub xs (sum_sub li sum +. width ** 2.) in match t with | Leaf -> true | Branch( f ,li ) -> f ** 2. >= sum_sub li 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 |l :: ls -> if cur_el = l then aux ls (cur_el, cur_num + 1) (max_el , max_num) else if cur_num > max_num then aux ls (l,1) (cur_el,cur_num) else aux ls (l,1)(max_el,max_num) in let sl = List.sort compare l in match sl with |[] -> failwith "Undefined input." |l :: ls -> aux ls (l, 1) (l, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec helper l x = match l with |[] -> failwith "Undefined input." |l1 :: [] -> failwith "Undefined input." |l1 :: l2 :: [] -> (l1,l2) :: x |l1 :: l2 :: ls -> helper (l2 :: ls) ((l1,l2) :: x) in let li = helper l [] in mode(li) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit != to_unit then match from_unit with |Second -> (to_unit, val_ /. 3600.) |Hour -> (to_unit , val_ *. 3600.) else (from_unit, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit != to_unit then match from_unit with |Foot -> if to_unit == Meter then (to_unit, val_ *. 0.3048) else (to_unit , val_ /. 5280.) |Meter -> if to_unit == Foot then (to_unit, val_ /. 0.3048) else (to_unit , val_ /. 0.3048 /. 5280.) |Mile -> if to_unit == Foot then (to_unit, val_ *. 5280.) else (to_unit , val_ *. 5280. *. 0.3048) else (from_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (du, tu) = from_unit in let (tdu, ttu) = to_unit in let (tdu, v) = convert_dist (du, val_) tdu in let (tu, v1) = convert_time (ttu, v) tu in (to_unit, v1) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let(au,avl) = a in let(bu,v) = convert_speed (au,avl) b_unit in (b_unit, v +. b_val) ;; |
let passes_da_vinci t = let rec check l = match l with |[] -> true |Leaf :: ts -> true && check ts |Branch (v,ls) :: ts -> ((v *. v) >= (childsum ls)) && (check ls) && (check ts) in if t == Leaf then true else let Branch(v,l) = t in ((v *. v) >= (childsum l)) && check l ;; |
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 l with | [] -> notimplemented () | _ -> aux (List.sort compare (l)) ((List.nth l 0), 0) ((List.nth l 0), 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then notimplemented () else let copy1 = l in let copy2 = l in let _::t = copy1 in let l1 = t in let _::t = List.rev copy2 in let l2 = List.rev t in mode (List.combine l2 l1) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_), 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, val_), 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 ((_, _), _), _ = (from_unit, val_), to_unit in (to_unit, snd (convert_dist ((fst (from_unit)), val_) (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 ((fst a), (snd a)) b_unit) +. b_val) ;; |
let passes_da_vinci t = let rec summation l acc = match l with | [] -> acc | Leaf :: tail -> summation tail acc | Branch (width, subtree) :: tail -> summation tail (acc +. (width *. width)) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if (check subtree && summation 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 List.sort compare 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 List.sort compare l with | [] -> failwith "Undefined input." | x :: xs -> aux xs (x, 1) (x, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec helper l pairs= match l with | x :: y :: z -> helper (y::z) ((x,y)::pairs) | _ -> pairs in if List.length l < 2 then failwith "Invalid inputs." else mode (helper l []) ;; |
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 | Hour -> (Second, val_*.3600.) | Second -> (Hour, 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 -> if to_unit = Meter then (Meter, val_*.0.3048) else (Mile, val_/.5280.) | Meter -> if to_unit = Foot then (Foot, val_/.0.3048) else (Mile, val_/.0.3048/.5280.) | Mile -> if to_unit = Foot then (Foot, val_*.5280.) else (Meter, val_*.5280.*.0.3048) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if from_unit = to_unit then (from_unit,val_) else if fst(from_unit) = fst(to_unit) then (to_unit, snd(convert_time (snd(to_unit), val_) (snd(from_unit)))) else if snd(from_unit) = snd(to_unit) then (to_unit, snd(convert_dist (fst(from_unit), val_) (fst(to_unit)))) else (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 = if fst(a) = b_unit then (b_unit, (snd(a)+.b_val)) else (b_unit, (snd(convert_speed (fst(a),snd(a)) (b_unit))+.b_val)) ;; |
let passes_da_vinci t= let rec check l acc w= match l with | [] -> acc <= (w ** 2.) | Leaf :: tl -> check tl acc w | Branch (width, subtree) :: tl -> if not (check subtree 0. width) then false else check tl (acc+.width**2.) w in match t with | Leaf -> true | Branch(width, subtree) -> check 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 List.sort compare 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 List.sort compare l with | [] -> failwith "Undefined input." | x :: xs -> aux xs (x, 1) (x, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec helper l pairs= match l with | x :: y :: z -> helper (y::z) ((x,y)::pairs) | _ -> pairs in if List.length l < 2 then failwith "Invalid inputs." else mode (helper l []) ;; |
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 | Hour -> (Second, val_*.3600.) | Second -> (Hour, 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 -> if to_unit = Meter then (Meter, val_*.0.3048) else (Mile, val_/.5280.) | Meter -> if to_unit = Foot then (Foot, val_/.0.3048) else (Mile, val_/.0.3048/.5280.) | Mile -> if to_unit = Foot then (Foot, val_*.5280.) else (Meter, val_*.5280.*.0.3048) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if from_unit = to_unit then (from_unit,val_) else if fst(from_unit) = fst(to_unit) then (to_unit, snd(convert_time (snd(to_unit), val_) (snd(from_unit)))) else if snd(from_unit) = snd(to_unit) then (to_unit, snd(convert_dist (fst(from_unit), val_) (fst(to_unit)))) else (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 = if fst(a) = b_unit then (b_unit, (snd(a)+.b_val)) else (b_unit, (snd(convert_speed (fst(a),snd(a)) (b_unit))+.b_val)) ;; |
let passes_da_vinci t= let rec check l acc w= match l with | [] -> acc <= (w**2.) | Leaf :: tl -> check tl acc w | Branch (width, subtree) :: tl -> if not (check subtree 0. width) then false else check tl (acc+.width**2.) w in match t with | Leaf -> true | Branch (width, subtree) -> check subtree 0. width ;; |
let mode (l: 'a list) : 'a = match List.sort compare l with | [] -> failwith "Error" | x::xs -> 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 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 aux xs (x, 1) (x, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec helper l acc = match l with | x::y::xs -> helper (y::xs) ((x,y)::acc) | _ -> acc in mode (helper 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.) | (Second, Second) -> (Second, val_) | (Hour, Hour) -> (Hour, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let helper from_unit val_ = match from_unit with | Foot -> val_ | Meter -> val_/.0.3048 | Mile -> val_*.5280. in match to_unit with | Foot -> (Foot, (helper from_unit val_)) | Meter -> (Meter, (helper from_unit val_)*.0.3048) | Mile -> (Mile, (helper from_unit val_)/.5280.) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (d1, t1) = from_unit in let (d2, t2) = to_unit in let (d2, dval) = convert_dist (d1, val_) d2 in let (t2, fval) = convert_time (t2, dval) t1 in ((d2,t2), fval) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (s_dist, s_time) = speed_unit in let (t_unit, t_val) = convert_time time s_time in (s_dist, t_val *. speed_val) ;; let tree_example2 = Branch (5., []);; let tree_example3 = Branch (5., [ Branch (3., [Leaf; Leaf; Leaf]); Leaf; Branch (7., []) ]) ;; let tree_example4 = Branch (5., [ Branch (3., [Leaf; Leaf; Leaf]); Leaf; Branch (4., [ Branch (2., [Leaf; Leaf])]) ]) ;; let tree_example5 = Leaf;; let rec passes_da_vinci t = let rec sum_squares root tree_list acc : bool = match tree_list with | [] -> not (acc > (root*.root)) | Leaf::tail -> sum_squares root tail acc | Branch (a, b)::tail -> sum_squares root tail (acc+.(a*.a)) in let rec traverse t = match t with | [] -> true | Leaf::tail -> traverse tail | Branch (a, b)::tail -> if sum_squares a b 0. then (traverse b) && (traverse tail) else false in traverse [t] ;; |
let mode (l: 'a list) : 'a = let emptylist() = failwith "The list is empty, there is no most common element" 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 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 let l = List.sort compare (l) in match l with | [] -> emptylist(); | x::xs -> aux xs (x,1) (x, 0); ;; |
let pair_mode (l: 'a list) : 'a * 'a = let error() = failwith "input should be a list of at least 2 elements" in let rec createpairs l = match l with |x::y::[] -> (x,y):: []; |x::y::xs -> (x,y) :: (createpairs (y::xs)); in match l with |[] -> error(); |[x] -> error(); |x::xs -> mode (createpairs l) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let error() = failwith "not correct input" in match (from_unit, val_) with |Second, y -> (match to_unit with |Hour -> to_unit, y /. 3600.; |Second -> to_unit, y;) |Hour, y -> (match to_unit with |Second -> to_unit, y *. 3600.; |Hour -> to_unit, y;) |_,_ -> error(); ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let error() = failwith "not correct input" in match (from_unit, val_) with |Foot, y -> (match to_unit with |Meter -> to_unit, y *. 0.3048; |Mile -> to_unit, y /. 5280.; |Foot -> to_unit, y;) |Meter, y -> (match to_unit with |Foot -> to_unit, y /. 0.3048; |Mile -> to_unit, y /. 1609.344; |Meter -> to_unit, y;) |Mile, y -> (match to_unit with |Foot -> to_unit, y *. 5280.; |Meter -> to_unit, y *. 1609.344; |Mile -> to_unit, y;) |_,_ -> error(); ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let error() = failwith "not correct input" in match (from_unit, val_) with |(Foot,Hour), y-> (match to_unit with |(Meter,Hour) -> to_unit, y *. 0.3048; |(Meter,Second) -> to_unit, y *. 0.3048 /. 3600.; |(Mile, Hour) -> to_unit, y /. 5280.; |(Mile, Second) -> to_unit, y /. 5280. /. 3600.; |(Foot, Second) -> to_unit, y /. 3600.; |(Foot, Hour) -> to_unit, y;) |(Foot,Second), y -> (match to_unit with |(Meter,Hour) -> to_unit, y *. 0.3048 *. 3600.; |(Meter,Second) -> to_unit, y *. 0.3048; |(Mile, Hour) -> to_unit, y /. 5280. *. 3600.; |(Mile, Second) -> to_unit, y /. 5280.; |(Foot, Hour) -> to_unit, y *. 3600.; |(Foot, Second) -> to_unit, y;) |(Meter, Hour), y -> (match to_unit with |(Mile, Hour) -> to_unit, y /. 1609.344; |(Mile, Second) -> to_unit, y /. 1609.344 /. 3600.; |(Foot, Second) -> to_unit, y /. 0.3048 /. 3600.; |(Foot, Hour) -> to_unit, y /. 0.3048; |(Meter,Second) -> to_unit, y /. 3600.; |(Meter,Hour) -> to_unit, y;) |(Meter, Second), y -> (match to_unit with |(Mile, Hour) -> to_unit, y /. 1609.344 *. 3600.; |(Mile, Second) -> to_unit, y /. 1609.344; |(Foot, Second) -> to_unit, y /. 0.3048; |(Foot, Hour) -> to_unit, y /. 0.3048 *. 3600.; |(Meter,Hour) -> to_unit, y *. 3600.; |(Meter,Second) -> to_unit, y;) |(Mile, Hour), y -> (match to_unit with |(Meter,Hour) -> to_unit, y *. 1609.344; |(Meter,Second) -> to_unit, y *. 1609.344 /. 3600.; |(Foot, Hour) -> to_unit, y *. 5280.; |(Foot, Second) -> to_unit, y *. 5280. /. 3600.; |(Mile, Second) -> to_unit, y /. 3600.; |(Mile, Hour) -> to_unit, y;) |(Mile, Second), y -> (match to_unit with |(Foot, Hour) -> to_unit, y *. 5280. *. 3600.; |(Foot, Second) -> to_unit, y *. 5280.; |(Meter,Hour) -> to_unit, y *. 1609.344 *. 3600.; |(Meter,Second) -> to_unit, y *. 1609.344; |(Mile, Hour) -> to_unit, y *. 3600.; |(Mile, Second) -> to_unit, y;) |_,_ -> error(); ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = match (speed_unit, speed_val) with |((_ ,Second),y) -> (let time = convert_time time Second in match speed_unit, time with |(Mile ,Second ), (Second, x)-> (Mile, (y *. x)) |(Foot ,Second ), (Second, x)-> (Foot, (y *. x)) |(Meter ,Second ), (Second, x)-> (Meter, (y *. x));) |((_ ,Hour),y) -> (let time = convert_time time Hour in match speed_unit, time with |(Mile ,Hour ), (Hour, x)-> (Mile, (y *. x)) |(Foot ,Hour ), (Hour, x)-> (Foot, (y *. x)) |(Meter ,Hour ), (Hour, x)-> (Meter, (y *. x));) ;; let rec passes_da_vinci t = let error() = failwith "tree has no branches" in let rec sum_square (t1 : tree list): float = match t1 with |[]-> 0. |x::xs-> match x with |Leaf -> 0. +. sum_square(xs) |Branch(y,l1)-> y*.y +. sum_square(xs) in let rec aux t w c = sum_square(c) <= w*.w && match c with |[]-> true |x::xs -> match x with |Leaf -> true |Branch(z,c2) -> aux (Branch(z,c2)) z c2 in if t = Leaf then true else let Branch (y,c1) = t in aux t y c1 ;; |
let mode (l: 'a list) : 'a = let emptylist() = failwith "The list is empty, there is no most common element" 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 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 let l = List.sort compare (l) in match l with | [] -> emptylist(); | x::xs -> aux xs (x,1) (x, 0); ;; |
let pair_mode (l: 'a list) : 'a * 'a = let error() = failwith "input should be a list of at least 2 elements" in let rec createpairs l = match l with |x::y::[] -> (x,y):: []; |x::y::xs -> (x,y) :: (createpairs (y::xs)); in match l with |[] -> error(); |[x] -> error(); |x::xs -> mode (createpairs l) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let error() = failwith "not correct input" in match (from_unit, val_) with |Second, y -> (match to_unit with |Hour -> to_unit, y /. 3600.; |Second -> to_unit, y;) |Hour, y -> (match to_unit with |Second -> to_unit, y *. 3600.; |Hour -> to_unit, y;) |_,_ -> error(); ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let error() = failwith "not correct input" in match (from_unit, val_) with |Foot, y -> (match to_unit with |Meter -> to_unit, y *. 0.3048; |Mile -> to_unit, y /. 5280.; |Foot -> to_unit, y;) |Meter, y -> (match to_unit with |Foot -> to_unit, y /. 0.3048; |Mile -> to_unit, y /. 1609.344; |Meter -> to_unit, y;) |Mile, y -> (match to_unit with |Foot -> to_unit, y *. 5280.; |Meter -> to_unit, y *. 1609.344; |Mile -> to_unit, y;) |_,_ -> error(); ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let error() = failwith "not correct input" in match (from_unit, val_) with |(Foot,Hour), y-> (match to_unit with |(Meter,Hour) -> to_unit, y *. 0.3048; |(Meter,Second) -> to_unit, y *. 0.3048 /. 3600.; |(Mile, Hour) -> to_unit, y /. 5280.; |(Mile, Second) -> to_unit, y /. 5280. /. 3600.; |(Foot, Second) -> to_unit, y /. 3600.; |(Foot, Hour) -> to_unit, y;) |(Foot,Second), y -> (match to_unit with |(Meter,Hour) -> to_unit, y *. 0.3048 *. 3600.; |(Meter,Second) -> to_unit, y *. 0.3048; |(Mile, Hour) -> to_unit, y /. 5280. *. 3600.; |(Mile, Second) -> to_unit, y /. 5280.; |(Foot, Hour) -> to_unit, y *. 3600.; |(Foot, Second) -> to_unit, y;) |(Meter, Hour), y -> (match to_unit with |(Mile, Hour) -> to_unit, y /. 1609.344; |(Mile, Second) -> to_unit, y /. 1609.344 /. 3600.; |(Foot, Second) -> to_unit, y /. 0.3048 /. 3600.; |(Foot, Hour) -> to_unit, y /. 0.3048; |(Meter,Second) -> to_unit, y /. 3600.; |(Meter,Hour) -> to_unit, y;) |(Meter, Second), y -> (match to_unit with |(Mile, Hour) -> to_unit, y /. 1609.344 *. 3600.; |(Mile, Second) -> to_unit, y /. 1609.344; |(Foot, Second) -> to_unit, y /. 0.3048; |(Foot, Hour) -> to_unit, y /. 0.3048 *. 3600.; |(Meter,Hour) -> to_unit, y *. 3600.; |(Meter,Second) -> to_unit, y;) |(Mile, Hour), y -> (match to_unit with |(Meter,Hour) -> to_unit, y *. 1609.344; |(Meter,Second) -> to_unit, y *. 1609.344 /. 3600.; |(Foot, Hour) -> to_unit, y *. 5280.; |(Foot, Second) -> to_unit, y *. 5280. /. 3600.; |(Mile, Second) -> to_unit, y /. 3600.; |(Mile, Hour) -> to_unit, y;) |(Mile, Second), y -> (match to_unit with |(Foot, Hour) -> to_unit, y *. 5280. *. 3600.; |(Foot, Second) -> to_unit, y *. 5280.; |(Meter,Hour) -> to_unit, y *. 1609.344 *. 3600.; |(Meter,Second) -> to_unit, y *. 1609.344; |(Mile, Hour) -> to_unit, y *. 3600.; |(Mile, Second) -> to_unit, y;) |_,_ -> error(); ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = match (speed_unit, speed_val) with |((_ ,Second),y) -> (let time = convert_time time Second in match speed_unit, time with |(Mile ,Second ), (Second, x)-> (Mile, (y *. x)) |(Foot ,Second ), (Second, x)-> (Foot, (y *. x)) |(Meter ,Second ), (Second, x)-> (Meter, (y *. x));) |((_ ,Hour),y) -> (let time = convert_time time Hour in match speed_unit, time with |(Mile ,Hour ), (Hour, x)-> (Mile, (y *. x)) |(Foot ,Hour ), (Hour, x)-> (Foot, (y *. x)) |(Meter ,Hour ), (Hour, x)-> (Meter, (y *. x));) ;; let rec passes_da_vinci t = let rec sum_square (t1 : tree list): float = match t1 with |[]-> 0. |x::xs-> match x with |Leaf -> 0. +. sum_square(xs) |Branch(y,l1)-> y*.y +. sum_square(xs) in let rec aux t w c = sum_square(c) <= w*.w && match c with |[]-> true |x::xs -> match x with |Leaf -> true |Branch(z,c2) -> aux (Branch(z,c2)) z c2 in if t = Leaf then true else let Branch (y,c1) = t in aux t y c1 ;; |
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 when hd = cur_el -> aux tl (cur_el, cur_num + 1) (max_el, max_num) | hd::tl -> 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 "list is empty" | _ -> aux (List.sort compare l) (List.hd l, 0) (List.hd l, 0) ;; let to_tuples (l: 'a list) : ('a * 'a) list = let rec tuples_helper (l: 'a list) (l1: ('a * 'a) list) = match l with [] -> l1 | x::[] -> l1 | a::b::tl -> tuples_helper tl (l1 @ [(a,b)]) in tuples_helper l [] ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with [] -> failwith "list is empty" | x::[] -> failwith "list must have at least two elements" | _ -> (mode ((to_tuples l) @ (to_tuples (List.tl l)))) ;; |
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.)) | _ -> (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) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Mile, Foot) -> (Foot, val_ *. 5280.) | (Foot, Mile) -> (Mile, val_ /. 5280.) | (Meter, Mile) -> (Mile, (val_ /. 0.3048) /. 5280.) | (Mile, Meter) -> (Meter, (val_ *. 5280.) *. 0.3048) | _ -> (to_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if from_unit = to_unit then (to_unit, val_) else let speed_val = convert_dist (fst from_unit, val_) (fst to_unit) in let time_val = convert_time (snd from_unit, 1.) (snd to_unit) in (to_unit, snd speed_val /. snd time_val) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let new_speed = convert_speed (speed_unit, speed_val) (fst speed_unit, fst time) in (fst speed_unit, snd new_speed *. (snd time)) ;; let sum_squares (l: tree list) : float = let rec helper_sum (l: tree list) (sum_so_far: float) : float = match l with [] -> sum_so_far | (Branch (x, _))::tl -> helper_sum tl (sum_so_far +. (x *. x)) | Leaf::tl -> helper_sum tl sum_so_far in helper_sum l 0.0 ;; let rec passes_da_vinci t = let rec check_list (l: tree list) : bool = match l with [] -> true | x::xs -> passes_da_vinci x && check_list xs in match t with | Leaf -> true | Branch (_, []) -> true | Branch (x, l) -> if sum_squares l <= x *. x then check_list l else 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 when hd = cur_el -> aux tl (cur_el, cur_num + 1) (max_el, max_num) | hd::tl -> if cur_num > max_num then aux tl (hd, 1) (cur_el, cur_num) else aux tl (hd, 1) (max_el, max_num) in aux (List.sort compare l) (List.hd l, 0) (List.hd l, 0) ;; let to_tuples (l: 'a list) : ('a * 'a) list = let rec tuples_helper (l: 'a list) (l1: ('a * 'a) list) = match l with [] -> l1 | x::[] -> l1 | a::b::tl -> tuples_helper tl (l1 @ [(a,b)]) in tuples_helper l [] ;; |
let pair_mode (l: 'a list) : 'a * 'a = (mode ((to_tuples l) @ (to_tuples (List.tl l)))) ;; |
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.)) | _ -> (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) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Mile, Foot) -> (Foot, val_ *. 5280.) | (Foot, Mile) -> (Mile, val_ /. 5280.) | (Meter, Mile) -> (Mile, (val_ /. 0.3048) /. 5280.) | (Mile, Meter) -> (Meter, (val_ *. 5280.) *. 0.3048) | _ -> (to_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if from_unit = to_unit then (to_unit, val_) else let speed_val = convert_dist (fst from_unit, val_) (fst to_unit) in let time_val = convert_time (snd from_unit, 1.) (snd to_unit) in (to_unit, snd speed_val /. snd time_val) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let new_speed = convert_speed (speed_unit, speed_val) (fst speed_unit, fst time) in (fst speed_unit, snd new_speed *. (snd time)) ;; let sum_squares (l: tree list) : float = let rec helper_sum (l: tree list) (sum_so_far: float) : float = match l with [] -> sum_so_far | (Branch (x, _))::tl -> helper_sum tl (sum_so_far +. (x *. x)) | Leaf::tl -> helper_sum tl sum_so_far in helper_sum l 0.0 ;; let rec passes_da_vinci t = let rec check_list (l: tree list) : bool = match l with [] -> true | x::xs -> passes_da_vinci x && check_list xs in match t with | Leaf -> true | Branch (_, []) -> true | Branch (x, l) -> if sum_squares l <= x *. x then check_list l else false ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.