text
stringlengths 0
601k
|
---|
let fib (max : int) : int list = if max <= 1 then [] else unfold (fun b -> (fib_aux b 0 1,(b+1))) (fun b -> max <= fib_aux b 0 1) 0 ;; let rec lists l acc= match l with |[] -> acc |g :: [] -> acc |g::xs -> lists (xs) ((g+(List.nth xs 0))::acc) ;; let listb l = match l with | [] -> [1] | [1] -> [1;1] | g -> (List.append [1] (List.rev (List.append [1] (lists g []))));; |
let pascal (max : int) : int list list = if max = 0 then [] else unfold (fun b -> (listb b, listb b)) (fun b -> max <= (List.length b)) [] let zips (l1,l2)= match l1, l2 with | [], _ -> domain() | _, [] -> domain() | x::xs, y::ys -> ((x, y), (xs,ys)) ;; let listcheck l1 l2 = match l1, l2 with | [], _ -> true | _, [] -> true | x::xs, y::ys -> false let tail l1 = match l1 with | [] -> [] |x::xs -> xs::[];; |
let allergy_free (allergens : ingredient list) (cupcakes : cupcake list) : cupcake list = let rec exist1 x c1 = match x, c1 with |[], _ -> false |(g::gs, (Cupcake (x,y,z,e)))-> if (not (List.exists (fun e -> e != g) e)) then exist1 gs c1 else true in let existall l c = match l,c with |[], _ -> false |_, [] -> false |(g, cups) -> List.for_all (exist1 g) cups in let existmaybe l c = match l,c with |[], _ -> false |_, [] -> false |(g, cups) -> List.exists (exist1 g) cups in let existing l c = if not (existall l c) then not (existmaybe l c) else true in if not (existing allergens cupcakes) then cupcakes else match allergens, cupcakes with | ([],_) -> cupcakes | (_,[]) -> [] | (v_1),(v_2) -> let rec exist2 x c1 = match x, c1 with |[], _ -> true |(g::gs, (Cupcake (x,y,z,e)))-> if (List.for_all (fun e -> e != g) e) then (exist2 gs c1) else false in List.filter (exist2 v_1) v_2;; |
let invalid_arg () = failwith "Invalid Argument";; let get = String.get ;; let append = (^);; let length = String.length ;; |
let string_explode (s : string) : char list = if length s < 1 then [] else tabulate (get s) (String.length s);; |
let string_implode (l : char list) : string = match l with |[] -> "" |g -> List.fold_left append "" (List.map Char.escaped l) let even b = if b mod 2 = 0 then (b, b+2) else (b+1, b+3);; |
let evens (max : int) : int list = if max < 0 then domain() else unfold (even) ((<=) max) 0 let rec fib_aux n a b = if n = 0 then b else fib_aux (n-1) (b) (a+b);; |
let fib (max : int) : int list = if max <= 1 then [] else unfold (fun b -> (fib_aux b 0 1,(b+1))) (fun b -> max <= fib_aux b 0 1) 0 ;; let rec lists l acc= match l with |[] -> acc |g :: [] -> acc |g::xs -> lists (xs) ((g+(List.nth xs 0))::acc) ;; let listb l = match l with | [] -> [1] | [1] -> [1;1] | g -> (List.append [1] (List.rev (List.append [1] (lists g []))));; |
let pascal (max : int) : int list list = if max = 0 then [] else unfold (fun b -> (listb b, listb b)) (fun b -> max <= (List.length b)) [] let zips (l1,l2)= match l1, l2 with | [], _ -> domain() | _, [] -> domain() | x::xs, y::ys -> ((x, y), (xs,ys)) ;; let listcheck l1 l2 = match l1, l2 with | [], _ -> true | _, [] -> true | x::xs, y::ys -> false let tail l1 = match l1 with | [] -> [] |x::xs -> xs::[];; |
let allergy_free (allergens : ingredient list) (cupcakes : cupcake list) : cupcake list = let rec exist1 x c1 = match x, c1 with |[], _ -> false |(g::gs, (Cupcake (x,y,z,e)))-> if (not(List.exists (fun e -> e != g) e)) then exist1 gs c1 else true in let existall l c = match l,c with |[], _ -> false |_, [] -> false |(g, cups) -> List.for_all (exist1 g) cups in let existmaybe l c = match l,c with |[], _ -> false |_, [] -> false |(g, cups) -> List.exists (exist1 g) cups in let existing l c = if not (existall l c) then (existmaybe l c) else true in if not (existing allergens cupcakes) then cupcakes else match allergens, cupcakes with | ([],_) -> cupcakes | (_,[]) -> [] | (v_1),(v_2) -> let rec exist2 x c1 = match x, c1 with |[], _ -> true |(g::gs, (Cupcake (x,y,z,e)))-> if (List.for_all (fun e -> e != g) e) then (exist2 gs c1) else false in List.filter (exist2 v_1) v_2;; |
let string_explode (s : string) : char list = tabulate (String.get s) (String.length s);; |
let string_implode (l : char list) : string = List.fold_left (fun s c2 -> (s ^ (Char.escaped c2))) "" l;; |
let evens (max : int) : int list = unfold (fun en -> (en,en+2)) ((<=) max) 0;; |
let fib (max : int) : int list= unfold (fun (a,b)->((a+b),(b,a+b))) (fun b->max<=fst b + snd b) (1,0);; let cut=function |[]->[] |ls->(List.rev (List.tl (List.rev ls)));; let findNextRow ls= match (List.length ls) with |0->[1] |1->[1;1] |_->1::(List.map2 (+) (cut ls) (List.tl ls)) @ [1];; |
let pascal (max : int) : int list list = unfold (fun ls->let nextRow=findNextRow ls in (nextRow,nextRow)) (fun ls->List.length ls >= max) [];; |
let allergy_free (allergens : ingredient list) (cupcakes : cupcake list) : cupcake list = let checkElement ins=not ( List.exists ((=)ins) allergens) in let checkCupcake cupCake= List.for_all (checkElement) (let Cupcake(_,_,_,ls)= cupCake in ls) in List.filter (checkCupcake) cupcakes;; |
let string_explode (s : string) : char list = tabulate (fun x -> String.get s x) (String.length s) ;; |
let string_implode (l : char list) : string = let k = List.map (fun x -> Char.escaped x) l in List.fold_left (^) "" k;; |
let evens (max : int) : int list = unfold (fun b -> (b, b+2)) (fun b -> max <= b) 0;; |
let fib (max : int) : int list = unfold (fun (a,b) -> (b, (b,a+b))) (fun (a,b) -> max <= b) (0,1);; |
let pascal (max : int) : int list list = raise NotImplemented;; |
let allergy_free (allergens : ingredient list) (cupcakes : cupcake list) : cupcake list = raise NotImplemented;; |
let string_explode (s : string) : char list = tabulate (String.get s) (String.length s);; |
let string_implode (l : char list) : string = let add_char_to_string (s : string) (c : char) : string = s ^ (Char.escaped c) in List.fold_left (add_char_to_string) "" l;; |
let evens (max : int) : int list = let evens_list = unfold (fun b -> (b, b+2)) ((<=) max) 0 in evens_list;; |
let fib (max : int) : int list = let fibonacci_add (b : (int*int)) = let (b1,b2) = b in b1+b2, (b2, b1+b2) in let will_reach_max (b: (int*int)) = let (b1,b2) = b in max <= b2 + b1 in unfold (fibonacci_add) (will_reach_max) (1,0);; |
let pascal (max : int) : int list list = let generate_next_row (row : int list) = if List.length row >= 1 then let new_row = List.map2 (+) (0 :: row) (row @ [0]) in new_row, new_row else [1], [1] in let check_if_stop (row : int list) = List.length row + 1 > max in unfold (generate_next_row) (check_if_stop) [];; |
let allergy_free (allergens : ingredient list) (cupcakes : cupcake list) : cupcake list = let is_allergen (ing : ingredient) = not (List.exists (fun allergen -> allergen == ing) allergens) in let check_cupcake_ingredients (cup : cupcake) = let Cupcake (p,w,c,ings) = cup in List.for_all (is_allergen) ings in List.filter (check_cupcake_ingredients) cupcakes;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in { update_pass = ( fun oldP newP -> match oldP with | s when (s = !password) -> (attempts :=0 ; password := newP) | _ -> (attempts := !attempts +1 ; raise wrong_pass ) ) ; deposit = ( fun pass amount -> match pass, amount with | (_,_) when (!attempts >= 5) -> raise too_many_failures | (p,_) when (not (pass = !password)) -> (attempts := !attempts +1 ; raise wrong_pass) | (_,a) -> attempts :=0 ; match a with | i when (i<0) -> raise negative_amount | _ -> balance := (!balance + amount) ) ; retrieve = ( fun pass amount -> match pass, amount with | (_,_) when (!attempts >= 5) -> raise too_many_failures | (p,_) when (not (pass = !password)) -> (attempts := !attempts +1 ; raise wrong_pass) | (_,a) -> attempts :=0 ; match a with | i when (i<0) -> raise negative_amount | i when (i > !balance) -> raise not_enough_balance | _ -> balance := (!balance - amount) ) ; show_balance = ( fun pass -> match pass with | _ when (!attempts >= 5) -> raise too_many_failures | s when (s = !password) -> (attempts :=0 ; !balance) | _ -> (attempts := !attempts +1 ; raise wrong_pass ) ) ; } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let helper (( (v1 : 'a) , (v2 : 'a) , (w : weight) ) as e) = match e with | (v,_,_) when (v=vertex) -> (@) [(v2, w)] | _ -> (@) [] in let e = g.edges in List.fold_right (helper) e [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (n, w) = node in if (List.mem n visited) then raise Fail else ( match aux_list (neighbours g n) (visited@[n]) with | (a1, w1) -> (a1, w1 + w) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (a,w)::t -> if (a=b) then ((visited@[b], w)) else (try (aux_node (a,w) visited) with Fail -> aux_list t visited) in aux_node (a,0) [] ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (a0, w0) = node in if (List.mem a0 visited) then (fc ()) else ( aux_list (neighbours g a0) (visited@[a0]) fc (fun (rv, r) -> sc (rv, (w0 + r) ) ) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> (fc ()) | (a2,w2)::t -> if (a2=b) then (sc (visited@[b], w2)) else let fail2 = (fun () -> aux_list t visited fc sc) in aux_node (a2,w2) (visited) (fail2) sc in aux_node (a,0) [] (fun () -> raise Fail) (fun (xv,x) -> (xv,x)) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in { update_pass = ( fun oldP newP -> match oldP with | s when (s = !password) -> (attempts :=0 ; password := newP) | _ -> (attempts := !attempts +1 ; raise wrong_pass ) ) ; deposit = ( fun pass amount -> match pass, amount with | (_,_) when (!attempts >= 5) -> raise too_many_failures | (p,_) when (not (pass = !password)) -> (attempts := !attempts +1 ; raise wrong_pass) | (_,a) -> attempts :=0 ; match a with | i when (i<0) -> raise negative_amount | _ -> balance := (!balance + amount) ) ; retrieve = ( fun pass amount -> match pass, amount with | (_,_) when (!attempts >= 5) -> raise too_many_failures | (p,_) when (not (pass = !password)) -> (attempts := !attempts +1 ; raise wrong_pass) | (_,a) -> attempts :=0 ; match a with | i when (i<0) -> raise negative_amount | i when (i > !balance) -> raise not_enough_balance | _ -> balance := (!balance - amount) ) ; show_balance = ( fun pass -> match pass with | _ when (!attempts >= 5) -> raise too_many_failures | s when (s = !password) -> (attempts :=0 ; !balance) | _ -> (attempts := !attempts +1 ; raise wrong_pass ) ) ; } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let helper (( (v1 : 'a) , (v2 : 'a) , (w : weight) ) as e) = match e with | (v,_,_) when (v=vertex) -> (@) [(v2, w)] | _ -> (@) [] in let e = g.edges in List.fold_right (helper) e [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (n, w) = node in if (List.mem n visited) then raise Fail else ( match aux_list (neighbours g n) (visited@[n]) with | (a1, w1) -> (a1, w1 + w) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (a,w)::t -> if (a=b) then ((visited@[b], w)) else (try (aux_node (a,w) visited) with Fail -> aux_list t visited) in aux_node (a,0) [] ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (a0, w0) = node in if (List.mem a0 visited) then (fc ()) else ( aux_list (neighbours g a0) (visited@[a0]) fc (fun (rv, r) -> sc (rv, (w0 + r) ) ) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> (fc ()) | (a2,w2)::t -> if (a2=b) then (sc (visited@[b], w2)) else let fail2 = (fun () -> aux_list t visited fc sc) in aux_node (a2,w2) (visited) (fail2) sc in aux_node (a,0) [] (fun () -> raise Fail) (fun (xv,x) -> (xv,x)) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper (all_paths: ('a list * weight) list) (acc_path: ('a list * weight) option) (acc_w: int) : (('a list * weight) option) = match all_paths with | [] -> acc_path | (path, w)::t when (w>acc_w)-> helper t (Some(path, w)) w | (path, w)::t when (w<=acc_w)-> helper t acc_path acc_w in helper (find_all_paths g a b) None 0 ;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in { update_pass = ( fun oldP newP -> match oldP with | s when (s = !password) -> (attempts :=0 ; password := newP) | _ -> (attempts := !attempts +1 ; raise wrong_pass ) ) ; deposit = ( fun pass amount -> match pass, amount with | (_,_) when (!attempts >= 5) -> raise too_many_failures | (p,_) when (not (pass = !password)) -> (attempts := !attempts +1 ; raise wrong_pass) | (_,a) -> attempts :=0 ; match a with | i when (i<0) -> raise negative_amount | _ -> balance := (!balance + amount) ) ; retrieve = ( fun pass amount -> match pass, amount with | (_,_) when (!attempts >= 5) -> raise too_many_failures | (p,_) when (not (pass = !password)) -> (attempts := !attempts +1 ; raise wrong_pass) | (_,a) -> attempts :=0 ; match a with | i when (i<0) -> raise negative_amount | i when (i > !balance) -> raise not_enough_balance | _ -> balance := (!balance - amount) ) ; show_balance = ( fun pass -> match pass with | _ when (!attempts >= 5) -> raise too_many_failures | s when (s = !password) -> (attempts :=0 ; !balance) | _ -> (attempts := !attempts +1 ; raise wrong_pass ) ) ; } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let helper (( (v1 : 'a) , (v2 : 'a) , (w : weight) ) as e) = match e with | (v,_,_) when (v=vertex) -> (@) [(v2, w)] | _ -> (@) [] in let e = g.edges in List.fold_right (helper) e [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (n, w) = node in if (List.mem n visited) then raise Fail else let (a1,w1) = aux_list (neighbours g n) (visited @ [n]) in (a1, (w1 + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (a,w)::t -> if (a=b) then ((visited@[b], w)) else (try (aux_node (a,w) visited) with Fail -> aux_list t visited) in aux_node (a,0) [] ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (a0, w0) = node in if (List.mem a0 visited) then (fc ()) else ( aux_list (neighbours g a0) (visited@[a0]) fc (fun (rv, r) -> sc (rv, (w0 + r) ) ) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> (fc ()) | (a2,w2)::t -> if (a2=b) then (sc (visited@[b], w2)) else let fail2 = (fun () -> aux_list t visited fc sc) in aux_node (a2,w2) (visited) (fail2) sc in aux_node (a,0) [] (fun () -> raise Fail) (fun (xv,x) -> (xv,x)) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper (all_paths: ('a list * weight) list) (acc_path: ('a list * weight) option) (acc_w: int) : (('a list * weight) option) = match all_paths with | [] -> acc_path | (path, w)::t when (w>acc_w)-> helper t (Some(path, w)) w | (path, w)::t when (w<=acc_w)-> helper t acc_path acc_w in helper (find_all_paths g a b) None 0 ;; |
let open_account (initial_pass: passwd) : bank_account = let pw = ref initial_pass in let balance = ref 0 in let count = ref 0 in { update_pass = (fun (old_pw:passwd) (new_pw:passwd) -> if ((!pw) = old_pw) then let _ = count := 0 in pw := new_pw else let _ = count := !count+1 in raise wrong_pass ); retrieve = (fun cur_pw y -> if !count >= 5 then raise too_many_failures else if ((!pw) = cur_pw) then let _ = count := 0 in if (y < 0) then raise negative_amount else if (!balance < y) then raise not_enough_balance else balance := !balance - y else let _ = count := !count+1 in raise wrong_pass ); deposit = (fun cur_pw y -> if !count >= 5 then raise too_many_failures else if ((!pw) = cur_pw) then let _ = count := 0 in if (y < 0) then raise negative_amount else balance := !balance + y else let _ = count := !count+1 in raise wrong_pass ); show_balance = (fun cur_pw -> if !count >= 5 then raise too_many_failures else if ((!pw) = cur_pw) then let _ = count := 0 in !balance else let _ = count := !count+1 in raise wrong_pass ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper edges v = match edges with | [] -> [] | (cur, tgt, weight)::tl -> if cur = v then (tgt, weight)::(helper tl v) else helper tl v in helper g.edges vertex ;; let rec contains l elt = match l with | [] -> false | hd::tl -> let (node, _) = elt in (hd = node) || (contains tl elt) ;; let rec get_out_targets g elt = match g with | [] -> [] | (cur, tgt, weight)::tl -> if (cur = elt) then (tgt,weight)::get_out_targets tl elt else get_out_targets tl elt;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (elt, weight) = node in if (elt = b) then ([b], weight) else let tgt = get_out_targets g.edges elt in let (res, wt) = (aux_list tgt visited) in (elt::res, wt+weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | hd::tl -> if (contains visited hd) then aux_list tl visited else try let (elt, _) = hd in aux_node hd (elt::visited) with Fail -> aux_list tl visited in aux_node (a,0) [a];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (elt, weight) = node in if (elt = b) then sc ([elt], weight) else let tgt = get_out_targets g.edges elt in aux_list tgt visited fc (fun (res, wt) -> sc (elt::res, wt + weight)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd::tl -> if (contains visited hd) then aux_list tl visited fc sc else let (elt, _) = hd in let fail2 = fun () -> aux_node hd (elt::visited) fc sc in let suc2 = sc in aux_list tl visited fail2 suc2 in aux_node (a,0) [a] (fun () -> raise Fail) (fun (elt, wt) -> (elt, wt));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let max = ref None in let rec helper list = match list with | [] -> !max | (res, wt)::tl -> match !max with | None -> let _ = max := Some (res, wt) in helper tl | Some (_, weight) -> if (wt > weight) then let _ = max := Some (res, wt) in helper tl else let _ = max := !max in helper tl in helper (find_all_paths g a b);; |
let open_account (initial_pass: passwd) : bank_account = let acc_locked = ref false in let balance = ref 0 in let bad_attempts = ref 0 in let cur_pwd = ref initial_pass in { update_pass = (fun (old_pwd) (new_pwd) -> if old_pwd = !cur_pwd then let good_pwd = new_pwd in cur_pwd := good_pwd; bad_attempts := 0; acc_locked := false else let new_bad_attempts = !bad_attempts + 1 in bad_attempts := new_bad_attempts; if new_bad_attempts >= 5 then acc_locked := true; raise wrong_pass ); retrieve = (fun (pwd) (amt) -> if not !acc_locked then if pwd <> !cur_pwd then let new_bad_attempts = !bad_attempts + 1 in bad_attempts := new_bad_attempts; if new_bad_attempts >= 5 then acc_locked := true; raise wrong_pass else let amt_to_get = amt in bad_attempts := 0; if amt_to_get > !balance then raise not_enough_balance else if amt_to_get < 0 then raise negative_amount else let new_balance = !balance - amt_to_get in balance := new_balance else raise too_many_failures ); deposit = (fun (pwd) (amt) -> if not !acc_locked then if pwd <> !cur_pwd then let new_bad_attempts = !bad_attempts + 1 in bad_attempts := new_bad_attempts; if new_bad_attempts >= 5 then acc_locked := true; raise wrong_pass else let amt_to_put = amt in bad_attempts := 0; if amt_to_put >= 0 then let new_balance = !balance + amt_to_put in balance := new_balance else raise negative_amount else raise too_many_failures ); show_balance = (fun (pwd) -> if not !acc_locked then if pwd <> !cur_pwd then let new_bad_attempts = !bad_attempts + 1 in bad_attempts := new_bad_attempts; if new_bad_attempts >= 5 then acc_locked := true; raise wrong_pass else let your_balance = !balance in bad_attempts := 0; your_balance else raise too_many_failures ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let filtered_edges = List.filter (fun ((src:'a), _, _) -> src = vertex) g.edges in List.map (fun (_, (sink:'a), weight) -> (sink, weight)) filtered_edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((node_val, w):('a * weight)) (visited : 'a list) : ('a list * weight) = if List.mem node_val visited then raise Fail else if node_val = b then ([node_val], w) else let (lst, cur_w) = aux_list (neighbours g node_val) (node_val::visited) in (node_val::lst, w + cur_w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try aux_node x visited with Fail -> aux_list xs visited in aux_node (a, 0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((node_val, w):('a * weight)) (visited : 'a list) fc sc : ('a list * weight) = if List.mem node_val visited then fc () else if node_val = b then (sc ([node_val], w)) else let succ = (fun (node_cont, w_cont) -> let (lst_sc, w_sc) = sc ([node_val], w) in (lst_sc@node_cont, w_cont + w_sc)) in let fail = fc in aux_list (neighbours g node_val) (node_val::visited) fail succ and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> let succ = sc in let fail = (fun () -> aux_list xs visited fc succ) in aux_node x visited fail succ in aux_node (a,0) [] (fun () -> raise Fail) (fun (lst, w) -> (lst, w));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let x = (find_all_paths g a b) in match x with | [] -> None | _ -> let n = List.hd (List.rev (List.sort (fun (_, w1) (_, w2) -> compare w1 w2) x)) in Some n;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let fail_auth_count = ref 0 in let check_pass auth updating = if (!fail_auth_count >= 5) && not updating then (fail_auth_count := !fail_auth_count + 1; raise too_many_failures) else if not (auth = !password) then (fail_auth_count := !fail_auth_count + 1; raise wrong_pass) else fail_auth_count := 0 in let check_amount amount retrieving = if amount < 0 then raise negative_amount else if retrieving && (amount > !balance) then raise not_enough_balance in { update_pass = (fun old_pass new_pass -> check_pass old_pass true; password := new_pass); retrieve = (fun pass amount -> check_pass pass false; check_amount amount true; balance := !balance - amount); deposit = (fun pass amount -> check_pass pass false; check_amount amount false; balance := !balance + amount); show_balance = (fun pass -> check_pass pass false; !balance); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let proj (_, y, z) = (y, z) in let is_adjacent v (u, _, _) = v = u in List.map proj (List.filter (is_adjacent vertex) g.edges);; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((u, w): 'a * weight) (visited : 'a list) : ('a list * weight)= if List.mem u visited then raise Fail else if u = b then ([u], w) else let recursion = aux_list (neighbours g u) (u :: visited) in (u :: (fst recursion), w + (snd recursion)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with [] -> raise Fail | hd :: tl -> try aux_node hd visited with Fail -> aux_list tl visited in aux_node (a, 0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((u, w): 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if List.mem u visited then fc () else if u = b then sc ([u], w) else let new_sc = fun x -> sc (u :: (fst x), w + (snd x)) in aux_list (neighbours g u) (u :: visited) fc new_sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with [] -> fc () | hd :: tl -> let new_fc = fun () -> aux_list tl visited fc sc in aux_node hd visited new_fc sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun x -> x);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match find_all_paths g a b with [] -> None | p :: l -> let r p1 p2 = let (_, w1) = p1 in let (_, w2) = p2 in if w2 > w1 then p2 else p1 in Some (List.fold_left r p l);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let fail_auth_count = ref 0 in let check_pass auth updating = if (!fail_auth_count >= 5) && not updating then raise too_many_failures else if not (auth = !password) then (fail_auth_count := !fail_auth_count + 1; raise wrong_pass) else fail_auth_count := 0 in let check_amount amount retrieving = if amount < 0 then raise negative_amount else if retrieving && (amount > !balance) then raise not_enough_balance in { update_pass = (fun old_pass new_pass -> check_pass old_pass true; password := new_pass); retrieve = (fun pass amount -> check_pass pass false; check_amount amount true; balance := !balance - amount); deposit = (fun pass amount -> check_pass pass false; check_amount amount false; balance := !balance + amount); show_balance = (fun pass -> check_pass pass false; !balance); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let proj (_, y, z) = (y, z) in let is_adjacent v (u, _, _) = v = u in List.map proj (List.filter (is_adjacent vertex) g.edges);; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((u, w): 'a * weight) (visited : 'a list) : ('a list * weight)= if List.mem u visited then raise Fail else if u = b then ([u], w) else let recursion = aux_list (neighbours g u) (u :: visited) in (u :: (fst recursion), w + (snd recursion)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with [] -> raise Fail | hd :: tl -> try aux_node hd visited with Fail -> aux_list tl visited in aux_node (a, 0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((u, w): 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if List.mem u visited then fc () else if u = b then sc ([u], w) else let new_sc = fun x -> sc (u :: (fst x), w + (snd x)) in aux_list (neighbours g u) (u :: visited) fc new_sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with [] -> fc () | hd :: tl -> let new_fc = fun () -> aux_list tl visited fc sc in aux_node hd visited new_fc sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun x -> x);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match find_all_paths g a b with [] -> None | p :: l -> let r p1 p2 = let (_, w1) = p1 in let (_, w2) = p2 in if w2 > w1 then p2 else p1 in Some (List.fold_left r p l);; |
let open_account (initial_pass: passwd) : bank_account = let failures = ref 0 in let balance = ref 0 in let password = ref (initial_pass:string) in let newAccount = { update_pass = ( fun input_pass new_pass -> if !password = input_pass then ( password := new_pass; failures := 0; ) else ( failures := !failures + 1; raise wrong_pass; ) ); retrieve = ( fun initial_pass amount -> if !failures > 4 then raise too_many_failures else ( if !password = initial_pass then ( failures := 0; if amount >= 0 then ( if (!balance - amount) >= 0 then balance := !balance - amount else raise not_enough_balance; ) else raise negative_amount ) else ( failures := !failures + 1; raise wrong_pass; ) ) ); deposit=( fun initial_pass amount -> if !failures > 4 then raise too_many_failures else ( if !password = initial_pass then ( failures := 0; if amount >= 0 then balance := !balance + amount else raise negative_amount ) else ( failures := !failures + 1; raise wrong_pass; ) ) ); show_balance = ( fun initial_pass -> if !failures > 4 then raise too_many_failures else ( if !password = initial_pass then ( failures := 0; !balance; ) else ( failures := !failures + 1; raise wrong_pass; ) ) ) } in newAccount;; let n_helper (composite: ('a * ('a * weight) list)) (cur: ('a * 'a * weight)): ('a * ('a * weight) list) = match cur with a, b, weight -> ( match composite with c, lst -> ( if a = c then (c, lst @ [(b, weight)]) else composite ) );; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let tmp = List.fold_left n_helper (vertex, []) g.edges in match tmp with |a,b -> b |_ -> raise Fail;; let neighbours2 (g: 'a graph) (vertex: 'a) : ('a * weight) list = let tmp = List.fold_left n_helper (vertex, []) g.edges in match tmp with |a,b -> b |_ -> [];; let helper3 (acc: int) (result:('a * weight)) = let weight = match result with a,weight -> weight in weight + acc;; let rec helper4 (acc: 'a list) (result:('a * weight) list): 'a list = match result with |[] -> acc |_ -> ( let firstEl = List.nth result 0 in let node = match firstEl with node,weight -> node in helper4 (acc@[node]) (List.tl result) ) ;; let helper5 (visited : ('a * weight) list) (neighbour:('a * weight)): bool = let nodes = helper4 [] visited in let nNode = match neighbour with a,b -> a in let x = List.mem nNode nodes in not x ;; let helper5b (visited : ('a*weight) list) (neighbour:('a * weight)): bool = let nodes = helper4 [] visited in let nNode = match neighbour with a,b -> a in let x = List.mem nNode nodes in not x ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux (nd: 'a * weight) (visited : ('a * weight) list) (result: ('a * weight) list):(('a * weight) list) = let node = match nd with a,b -> a in let nodeWeight = match nd with a,b -> b in try ( let adjacent = neighbours g node in match List.find (helper5 visited) adjacent with c, w ->( if c = b then (result@[c, w]) else aux (c,w)(visited@[c,w])(result@[c,w]) ) ) with |Not_found -> ( if node = a then raise Fail; try aux (List.nth(List.rev visited)1)(visited)(List.rev (List.tl (List.rev result))) with | Failure "hd"-> aux (a,0)(visited)([]) | Failure "tl" -> aux (List.nth(List.rev visited)1)(visited)([(a,0)]) ) in let calcWeight (result:('a * weight) list):int= List.fold_left helper3 0 result in let firstWeight = try match List.nth (neighbours g a) 0 with x,y -> y with Failure "nth" -> 0 in (helper4 [](aux (a,0) [(a,0)] [(a,firstWeight)]), calcWeight (aux (a,firstWeight) [(a,0)] [])) ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux (nd: 'a * weight) (visited : ('a * weight) list) (k: ('a * weight) list -> ('a * weight) list):(('a * weight) list) = let node = match nd with a,b -> a in let nodeWeight = match nd with a,b -> b in let adjacent = neighbours2 g node in let (c: 'a) = ( try match List.find (helper5 visited) adjacent with c, w -> c with Not_found -> "" ) in let w = ( try match List.find (helper5 visited) adjacent with c, w -> w with Not_found -> 0 ) in if (adjacent = [] || c = "") then ( if node = a then raise Fail; (k []) ) else ( if (c = b) then k [(c,w)] else aux (c,w)(visited@[c,w])(fun r -> k([(c,w)] @ r)) ) in let calcWeight (result:('a * weight) list):int= List.fold_left helper3 0 result in let firstWeight = try match List.nth (neighbours g a) 0 with x,y -> y with Failure "nth" -> 0 in ([a]@(helper4 [](aux (a,0) [(a,0)] (fun r -> r))), calcWeight (aux (a,firstWeight) [(a,0)] (fun r -> r))) ;; let delNodes (g: 'a graph)(lst: 'a list)(a:'a)(b:'a): ('a graph) = let notInList (c: 'a list)(d: 'a * 'a * weight) = if (match d with x,y,z -> x) = a || (match d with x,y,z -> y) = b then true else not (List.mem (match d with x,y,z -> x) c || List.mem (match d with x,y,z -> y) c ) in let notInList2 (c: 'a list)(e: 'a) = let d = List.filter (fun x -> if x = a || x = b then false else true) c in not (List.mem e d) in {nodes=(List.filter (notInList2 lst) g.nodes); edges=List.filter (notInList lst) g.edges} ;; |[] -> resList |[(a:'a);(b:'a)] -> resList |_ -> try ( let newPath = (find_path grph a b) in let grph2 = ( if (match newPath with x,y -> x) = [a;b] then {nodes=grph.nodes; edges = List.filter (fun x -> if (a = match x with r,s,t -> r) && (b = match x with r,s,t -> s) then false else true) grph.edges} else delNodes grph (match newPath with x,y -> x) a b ) in aux grph2 (resList @ [newPath]) ) with Fail -> resList in try List.tl (aux g [[],0]) with Failure "tl" -> [] ;; *) let calcWeight (result:('a * weight) list):int= List.fold_left helper3 0 result;; let extractNodes (lst:('a*weight)list) : ('a list) = let helper (edge:'a * weight):('a) = match edge with x,y -> x in List.map helper lst ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let pathsList = find_all_paths g a b in let sortedList = List.rev (List.sort sortPaths pathsList) in let res = try List.hd sortedList with Failure "hd" -> ([],0) in match res with | [],_ -> None | _ -> Some res ;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in let check_pass p f = if p = !pass then (attempts := 0; f ()) else (incr attempts; raise wrong_pass) in let attempt p f = if !attempts >= 5 then raise too_many_failures else check_pass p f in let update_pass old_pass new_pass = check_pass old_pass (fun () -> pass := new_pass) in let deposit p amt = attempt p (fun () -> if amt < 0 then raise negative_amount else balance := !balance + amt) in let retrieve p amt = attempt p (fun () -> if amt < 0 then raise negative_amount else if amt > !balance then raise not_enough_balance else balance := !balance - amt) in let show_balance p = attempt p (fun () -> !balance) in {update_pass; deposit; retrieve; show_balance};; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun (x, y, w) acc -> if x = vertex then (y,w) :: acc else acc) g.edges [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (x, y) = node in if x = b then ([b] , y) else if List.mem x visited then raise Fail else (let (path , cost) = aux_list (neighbours g x) (x :: visited) in (x :: path, cost + y)) and aux_list nodes visited = match nodes with | [] -> raise Fail | (v,w) :: vs -> try aux_node (v,w) visited with Fail -> aux_list vs visited in aux_node (a,0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (x, y) = node in if x = b then sc ([b], y) else if List.mem x visited then fc () else aux_list (neighbours g x) (x :: visited) fc (fun (path, cost) -> sc (x :: path, cost + y)) and aux_list nodes visited fc sc = match nodes with | [] -> fc () | v :: vs -> aux_node v visited (fun () -> aux_list vs visited fc sc) sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun l -> l);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max l = match l with | [] -> None | [(p,w)] -> Some (p,w) | (p1,w1)::(p2,w2)::rest -> if w1 >= w2 then max ((p1,w1)::rest) else max ((p2,w2)::rest) in max(find_all_paths g a b);; |
let open_account (initial_pass: passwd) : bank_account = let passwd = ref initial_pass in let count = ref 0 in let balance = ref 0 in {update_pass = (fun old_pass new_pass -> (match old_pass = !passwd with |true -> passwd := new_pass; count := 0 |false -> count := !count + 1; raise wrong_pass)); retrieve = (fun pass amount -> if !count >= 5 then (raise too_many_failures) else if amount < 0 then (raise negative_amount) else (match pass = !passwd, amount >= !balance with |true, true -> count := 0; raise not_enough_balance |true, false -> count := 0; balance := !balance - amount |false, _ -> count := !count + 1; raise wrong_pass)); deposit = (fun pass amount -> if !count >= 5 then (raise too_many_failures) else if amount < 0 then (raise negative_amount) else (match pass = !passwd with |true -> balance := !balance + amount; count := 0 |false -> count := !count + 1; raise wrong_pass)); show_balance = (fun pass -> if !count >= 5 then (raise too_many_failures) else (match pass = !passwd with |true -> count := 0; !balance |false -> count := !count + 1; raise wrong_pass)) } ;; let graph = {nodes = ["Montreal"; "Toronto"; "Ottowa"; "Vancouver"; "Kingston"]; edges = [("Montrea", " Toronto", 6); ("Montreal", "Ottowa", 3); ("Toronto", "Ottowa", 2); ("Toronto", "Vancouver", 8); ("Toronto", "Kingston", 1); ("Vancouver", "Ottowa", 10);]} in [ ((graph, "Montreal"), ["Ottowa", 3]); ((graph, "Kingston"), []); ];; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edge l (a, b, w) = if a = vertex then (b, w) :: l else l in List.fold_left edge [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (a', w) = node in if List.mem a' visited then raise Fail else match b with |[] -> raise Fail |node::_ -> if a' = b then ([], 0) else let n = (neighbours g a') in aux_list n (a' :: visited) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |(a', w)::xs -> try(aux_node (a', w) visited) with Fail -> aux_list xs (a':: visited) in aux_list [(a, 0)] [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let passwd = ref initial_pass in let count = ref 0 in let balance = ref 0 in {update_pass = (fun old_pass new_pass -> (match old_pass = !passwd with |true -> passwd := new_pass; count := 0 |false -> count := !count + 1; raise wrong_pass)); retrieve = (fun pass amount -> if !count >= 5 then (raise too_many_failures) else if amount < 0 then (raise negative_amount) else (match pass = !passwd, amount >= !balance with |true, true -> count := 0; raise not_enough_balance |true, false -> count := 0; balance := !balance - amount |false, _ -> count := !count + 1; raise wrong_pass)); deposit = (fun pass amount -> if !count >= 5 then (raise too_many_failures) else if amount < 0 then (raise negative_amount) else (match pass = !passwd with |true -> balance := !balance + amount; count := 0 |false -> count := !count + 1; raise wrong_pass)); show_balance = (fun pass -> if !count >= 5 then (raise too_many_failures) else (match pass = !passwd with |true -> count := 0; !balance |false -> count := !count + 1; raise wrong_pass)) } ;; let graph = {nodes = ["Montreal"; "Toronto"; "Ottowa"; "Vancouver"; "Kingston"]; edges = [("Montrea", " Toronto", 6); ("Montreal", "Ottowa", 3); ("Toronto", "Ottowa", 2); ("Toronto", "Vancouver", 8); ("Toronto", "Kingston", 1); ("Vancouver", "Ottowa", 10);]} in [ ((graph, "Montreal"), ["Ottowa", 3]); ((graph, "Kingston"), []); ];; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edge l (a, b, w) = if a = vertex then (b, w) :: l else l in List.fold_left edge [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (a', w) = node in if List.mem a' visited then raise Fail else match a' = b with |true -> ([a'], w) |false -> let n = (neighbours g a') in aux_list n (a' :: visited) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |(a', w)::xs -> try(aux_node (a', w) visited) with Fail -> aux_list xs (a':: visited) in aux_list [(a, 0)] [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let num_fails = ref 0 in let password = ref initial_pass in let pass_fail = (fun () -> if !num_fails >= 5 then raise too_many_failures else (num_fails := !num_fails + 1 ; raise wrong_pass)) in { update_pass = (fun old_pass new_pass -> if (old_pass = !password) then (num_fails := 0 ; password := new_pass) else (num_fails := !num_fails + 1 ; raise wrong_pass) ); deposit = (fun pass money -> if (pass = !password && !num_fails < 5) then ( num_fails := 0 ; (if (money >= 0) then (balance := !balance + money) else raise negative_amount)) else pass_fail () ); retrieve = (fun pass money -> if (pass = !password && !num_fails < 5) then (num_fails := 0 ; if (money >= 0) then if (!balance - money < 0) then raise not_enough_balance else balance := !balance - money else raise negative_amount) else pass_fail () ); show_balance = (fun pass -> if (pass = !password && !num_fails < 5) then (num_fails := 0 ; !balance) else pass_fail () ); } ;; let a = {nodes = ["a" ; "b" ; "c"]; edges = [("a","b",5);("a","c",10);("b","c",6)]};; let get_2_3 (_, b, c) = (b, c) ;; let get_1 (a, _) = a ;; let get_2 (_,b) = b ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let desired_edges = List.find_all (fun (x,y,w) -> x = vertex) g.edges in List.map get_2_3 desired_edges ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if (get_1 node) = b then ([get_1 node], (get_2 node)) else let unvisited : ('a * weight) list = List.filter (fun (x,_) -> not (List.mem x visited)) (neighbours g (get_1 node)) in if unvisited = [] then raise Fail else let path = aux_list unvisited ((get_1 node)::visited) in ((get_1 node)::(get_1 path), (get_2 node) + (get_2 path)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try aux_node x visited with Fail -> aux_list xs visited in aux_list [(a,0)] [] ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if (get_1 node) = b then sc (node) else let unvisited : ('a * weight) list = List.filter (fun (x,_) -> not (List.mem x visited)) (neighbours g (get_1 node)) in match unvisited with | [] -> fc () | _::tl -> let succ = fun base_node -> (List.append (get_1(sc node)) [(get_1 base_node)], (get_2 base_node) + (get_2 (sc node))) in aux_list unvisited ((get_1 node)::visited) fc succ and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> let fail = fun () -> aux_list xs visited fc sc in aux_node x visited fail sc in let base_sc = (fun base_node -> ([(get_1 base_node)], get_2 base_node)) in aux_list [(a,0)] [] (raise Fail) base_sc ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let compare path1 path2 = if (get_2 path1) > (get_2 path2) then path1 else path2 in let all_paths = find_all_paths g a b in match all_paths with | [] -> None | x::xs -> Some (List.fold_right compare xs x);; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 and password = ref initial_pass and attempts = ref 0 in let check_passwd (p: passwd): bool = if (!attempts <= 3) then if (p = !password) then (attempts := 0; true) else (attempts := !attempts+1; raise wrong_pass) else raise too_many_failures in { update_pass = (fun (oldP: passwd) (newP: passwd) -> if (check_passwd oldP) then if (oldP = !password) then (password := newP; attempts := 0) else (attempts := !attempts+1; raise wrong_pass)) ; retrieve = (fun (p: passwd) (amount: int) -> if (check_passwd p) then if (amount <= !balance) then balance := !balance - amount else if (amount < 0) then raise negative_amount else raise not_enough_balance) ; deposit = (fun (p: passwd) (amount: int) -> if (check_passwd p) then if (amount < 0) then raise negative_amount else balance := !balance + amount) ; show_balance = (fun (p: passwd) -> if (check_passwd p) then !balance else raise too_many_failures); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun neighbors edge -> let (v1,v2,w) = edge in if v1 = vertex then neighbors @ [(v2,w)] else neighbors ) [] g.edges ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (acc : weight): ('a list * weight) = let (v, w) = node in if (List.mem v visited) then raise Fail else if (v = b) then (visited @ [v], acc + w) else aux_list (neighbours g v ) (visited @ [v]) (acc + w); and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc : weight) : ('a list * weight) = match nodes with | [] -> raise Fail | h::t -> try aux_node h visited acc with Fail -> aux_list t visited acc in aux_node (a,0) [] 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (acc : weight) fc sc : ('a list * weight)= let (v , w) = node in if (List.mem v visited ) then fc () else if (v = b) then sc (visited @ [v], acc+w) else aux_list (neighbours g v) (visited@[v]) (acc+w) fc sc; and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc : weight) fc sc : ('a list * weight) = match nodes with | [] -> fc () | h::t -> aux_node h visited acc (fun () -> aux_list t visited acc fc sc) sc in aux_node (a,0) [] 0 (fun () -> raise Fail) (fun (l,w) -> (l,w));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let f (longest) (l,w) = match longest with | None -> Some(l,w) | Some(longestPath, maxWeight) -> if (w >= maxWeight) then Some(l,w) else Some(longestPath, maxWeight) in List.fold_left f None (find_all_paths g a b) ;; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 and password = ref initial_pass and attempts = ref 0 in let check_passwd (p: passwd): bool = if (!attempts <= 5) then if (p = !password) then (attempts := 0; true) else (attempts := !attempts+1; raise wrong_pass) else raise too_many_failures in { update_pass = (fun (oldP: passwd) (newP: passwd) -> if (check_passwd oldP) then if (oldP = !password) then (password := newP; attempts := 0) else (attempts := !attempts+1; raise wrong_pass)) ; retrieve = (fun (p: passwd) (amount: int) -> if (check_passwd p) then if (amount <= !balance) then balance := !balance - amount else if (amount < 0) then raise negative_amount else raise not_enough_balance) ; deposit = (fun (p: passwd) (amount: int) -> if (check_passwd p) then if (amount < 0) then raise negative_amount else balance := !balance + amount) ; show_balance = (fun (p: passwd) -> if (check_passwd p) then !balance else raise too_many_failures); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun neighbors edge -> let (v1,v2,w) = edge in if v1 = vertex then neighbors @ [(v2,w)] else neighbors ) [] g.edges ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (acc : weight): ('a list * weight) = let (v, w) = node in if (List.mem v visited) then raise Fail else if (v = b) then (visited @ [v], acc + w) else aux_list (neighbours g v ) (visited @ [v]) (acc + w); and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc : weight) : ('a list * weight) = match nodes with | [] -> raise Fail | h::t -> try aux_node h visited acc with Fail -> aux_list t visited acc in aux_node (a,0) [] 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (acc : weight) fc sc : ('a list * weight)= let (v , w) = node in if (List.mem v visited ) then fc () else if (v = b) then sc (visited @ [v], acc+w) else aux_list (neighbours g v) (visited@[v]) (acc+w) fc sc; and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc : weight) fc sc : ('a list * weight) = match nodes with | [] -> fc () | h::t -> aux_node h visited acc (fun () -> aux_list t visited acc fc sc) sc in aux_node (a,0) [] 0 (fun () -> raise Fail) (fun (l,w) -> (l,w));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let f (longest) (l,w) = match longest with | None -> Some(l,w) | Some(longestPath, maxWeight) -> if (w >= maxWeight) then Some(l,w) else Some(longestPath, maxWeight) in List.fold_left f None (find_all_paths g a b) ;; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 and password = ref initial_pass and attempts = ref 0 in let check_passwd (pass: passwd): bool = if (!attempts <= 5) then if (pass = !password) then (attempts := 0; true) else (attempts := !attempts+1; raise wrong_pass) else raise too_many_failures in { update_pass = (fun (oldP: passwd) (newP: passwd) -> if (check_passwd oldP) then if (oldP = !password) then (password := newP; attempts := 0) else (attempts := !attempts+1; raise wrong_pass)) ; retrieve = (fun (pass: passwd) (amount: int) -> if (check_passwd pass) then if (amount <= !balance) then balance := !balance - amount else if (amount < 0) then raise negative_amount else raise not_enough_balance) ; deposit = (fun (pass: passwd) (amount: int) -> if (check_passwd pass) then if (amount < 0) then raise negative_amount else balance := !balance + amount) ; show_balance = (fun (pass: passwd) -> if (check_passwd pass) then !balance else raise too_many_failures); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun neighbors edge -> let (v1,v2,w) = edge in if v1 = vertex then neighbors @ [(v2,w)] else neighbors ) [] g.edges ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.