text
stringlengths
0
601k
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left(fun acc (v,n, w) -> if (v = vertex) then (n,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 (n,w) = node in let neighbour_list = neighbours g n in if n = b then [n],w else if List.exists (fun r -> r = n) visited then raise Fail else try let next_node,next_weight = (aux_list neighbour_list (n::visited)) in n::next_node, next_weight + w with Fail -> raise Fail 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 (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (n,w) = node in let neighbour_list = neighbours g n in if n = b then sc ([n],w) else if List.exists (fun r -> r = n) visited then fc() else aux_list neighbour_list (n::visited) fc (fun(x,y)-> sc (n::x, y+w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc() | hd::tl -> aux_node hd visited (fun () -> aux_list tl visited fc sc) sc in aux_node (a,0) [] (fun() -> raise Fail) (fun (x1,y1) -> (x1,y1));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths g a b in match all_paths with | [] -> None | [hd] -> Some hd | hd::tl -> Some(List.fold_left (fun p1 p2 -> let (path1, w1) = p1 in let (path2, w2) = p2 in if w1 > w2 then p1 else p2 ) hd tl);;
let open_account a = let pass = ref a in let balance = ref 0 in let attempts = ref 0 in let check_pass pwd f = if pwd = !pass then (attempts := 0; f ()) else (incr attempts; raise wrong_pass) in let attempt pwd f = if !attempts >= 5 then raise too_many_failures else check_pass pwd f in let update_pass old new_a = check_pass old (fun () -> pass := new_a) in let retrieve pwd amt = attempt pwd (fun () -> if amt < 0 then raise negative_amount else if !balance >= amt then balance := !balance - amt else raise not_enough_balance) in let deposit pwd amt = attempt pwd (fun () -> if amt < 0 then raise negative_amount else balance := !balance + amt) in let show_balance pwd = attempt pwd (fun () -> !balance) in {update_pass; retrieve; deposit; show_balance};;
let neighbours (g: 'a graph) (vertex: 'a) = List.fold_right (fun (v1, v2, w) acc -> if v1 = vertex then (v2, w) :: acc else acc ) g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node, w) visited : ('a list * weight) = if node = b then ([b], w) else if List.mem node visited then raise Fail else (let (path, cost) = aux_list (neighbours g node) (node :: visited) in (node :: path, cost + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = 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, w) visited fc sc : ('a list * weight) = if node = b then sc ([b], w) else if List.mem node visited then fc () else aux_list (neighbours g node) (node :: visited) fc (fun (path, cost) -> sc (node :: path, cost + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = 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 a -> a);;
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 init_pass = ref initial_pass in let balance = ref 0 in let count = ref 0 in let check_pswd pswd = (if pswd <> !init_pass || !count >= 5 then (count := !count + 1; if !count > 5 then raise too_many_failures else raise wrong_pass) else count := 0 ) in let negative amount = if amount < 0 then raise negative_amount in let not_enough amount = if amount > !balance then raise not_enough_balance in { update_pass = (fun old_pass new_pass -> if old_pass = !init_pass then (init_pass := new_pass; count := 0) else (count := !count + 1; raise wrong_pass)); retrieve = (fun pswd amount_out -> check_pswd pswd ; negative amount_out; not_enough amount_out; balance := !balance - amount_out); deposit = (fun pswd amount -> check_pswd pswd ; negative amount; balance := !balance + amount); show_balance = (fun pswd -> check_pswd pswd ; !balance); };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let helper = List.filter (fun e -> let (x,y,w) = e in vertex = x) g.edges in List.map (fun (x,y,w) -> (y,w)) helper;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) total_weight : ('a list * weight) = let (x, w) = node in let neighbours_node = neighbours g x in if x = b then ((visited @ [x]), total_weight + w) else if List.length neighbours_node = 0 then raise Fail else if List.exists (fun u -> u = x) visited then raise Fail else aux_list neighbours_node (visited @ [x]) (total_weight + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) total_weight : ('a list * weight) = match nodes with | [] -> raise Fail | (x, w) :: t -> try aux_node (x, w) visited total_weight with Fail -> aux_list t visited total_weight 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) total_weight fc sc : ('a list * weight) = let (x, w) = node in let neighbours_node = neighbours g x in if x = b then sc ((visited @ [x]),(total_weight + w)) else if List.length neighbours_node = 0 then fc () else if List.exists (fun u -> u = x) visited then fc () else aux_list neighbours_node (visited @ [x]) (total_weight + w) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) total_weight fc sc : ('a list * weight) = match nodes with | [] -> fc () | (x, w) :: t -> aux_node (x, w) visited total_weight (fun () -> aux_list t visited total_weight fc sc) sc in aux_node (a, 0) [] 0 (fun () -> raise Fail) (fun u -> u);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths g a b in match all_paths with | [] -> None | _ -> Some (List.fold_right (fun (x1,w1) (x2,w2) -> if w1 >= w2 then (x1,w1) else (x2,w2)) all_paths ([], 0));;
let open_account (initial_pass: passwd) : bank_account = let cur_pass = ref initial_pass in let attempts = ref 0 in let balance = ref 0 in let change_pass new_pass = function () -> cur_pass := new_pass in let deposit_money amount = function () -> if amount < 0 then raise negative_amount else balance := balance.contents + amount in let retrieve_money amount = function () -> if amount < 0 then raise negative_amount else if amount > balance.contents then raise not_enough_balance else balance := balance.contents - amount in let display = function () -> balance.contents in let upd_check_pass test_pass f = if cur_pass.contents <> test_pass then (attempts := attempts.contents + 1; raise wrong_pass) else (attempts := 0; f()) in let other_check_pass test_pass f = if attempts.contents < 5 then if cur_pass.contents <> test_pass then (attempts := attempts.contents + 1; raise wrong_pass) else (attempts := 0; f()) else raise too_many_failures in { update_pass = (fun old_pass new_pass -> upd_check_pass old_pass (change_pass new_pass)) ; deposit = (fun test_pass amount -> other_check_pass test_pass (deposit_money amount)) ; retrieve = (fun test_pass amount -> other_check_pass test_pass (retrieve_money amount)) ; show_balance = (fun test_pass -> other_check_pass test_pass (display)) };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let get_edge (departure, arrival, w) = if departure = vertex then [(arrival, w)] else [] in List.concat (List.map get_edge g.edges);;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (name, cost) (visited : 'a list) : ('a list * weight) = let add_Node (trajectory, totalc) = (name :: trajectory, cost + totalc) in if List.mem name visited then raise Fail else if name = b then ([name], cost) else let neighbouring_nodes = neighbours g name in let new_visited = name :: visited in let result = aux_list (neighbouring_nodes) (new_visited) in add_Node result and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | this_node :: other_nodes -> try aux_node this_node visited with Fail -> aux_list other_nodes visited in aux_node (a, 0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let failure = function () -> raise Fail in let success = function a -> a in let rec aux_node (name, cost) (visited : 'a list) fc sc : ('a list * weight) = if List.mem name visited then fc () else if name = b then sc ([name], cost) else let neighbouring_nodes = neighbours g name in let new_visited = name :: visited in let new_success = function result -> sc (name :: fst(result), cost + snd(result)) in aux_list (neighbouring_nodes) (new_visited) fc new_success and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | this_node :: other_nodes -> let new_failure = function () -> aux_list other_nodes visited fc sc in aux_node this_node visited new_failure sc in aux_node (a, 0) [] failure success;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths g a b in let rec find_longest current (big_trajectory, big_cost) = if List.length current > 0 then let (trajectory, totalc) = List.hd current in if totalc > big_cost then find_longest (List.tl current) (trajectory, totalc) else find_longest (List.tl current) (big_trajectory, big_cost) else (big_trajectory, big_cost) in let longest_path = find_longest all_paths ([], 0) in match longest_path with | ([], 0) -> None | _ -> Some (longest_path);;
let open_account (initial_pass: passwd) : bank_account = let curr_password = ref initial_pass in let curr_acc_balance = ref 0 in let num_of_attempts = ref 0 in { update_pass = (fun old_pass new_pass -> if old_pass = !curr_password then (curr_password := new_pass; num_of_attempts := 0) else (num_of_attempts := !num_of_attempts + 1; raise wrong_pass)); retrieve = (fun password amount -> if !num_of_attempts >= 5 then raise too_many_failures else if password <> !curr_password then (num_of_attempts:= !num_of_attempts +1; raise wrong_pass) else ( if password = !curr_password then ( num_of_attempts:= 0; if amount < 0 then raise negative_amount else ( if !curr_acc_balance < amount then raise not_enough_balance else ( (curr_acc_balance:= !curr_acc_balance - amount) ) ) ) ) ) ; deposit = (fun password amount -> if (!num_of_attempts >= 5) then raise too_many_failures else if (password <> !curr_password) then (num_of_attempts := !num_of_attempts + 1; raise wrong_pass) else if (amount < 0) then raise negative_amount else (curr_acc_balance := !curr_acc_balance + amount; num_of_attempts := 0)); show_balance = (fun password -> if (!num_of_attempts >= 5) then raise too_many_failures else if (password <> !curr_password) then (num_of_attempts := !num_of_attempts + 1; raise wrong_pass) else num_of_attempts := 0; !curr_acc_balance) } ;; let graph = { nodes = ["v1"; "v2"; "v3"; "v4"; "v5"]; edges = [("v1", "v2", 3); ("v1", "v3", 2); ("v2", "v3", 6); ("v3", "v4", 12)] } in [ ((graph, "v1"), [("v2", 3); ("v3", 2)]); ((graph, "v2"), [("v3", 6)]); ((graph, "v3"), [("v4", 12)]); ((graph, "v4"), []); ((graph, "v5"), []) ];;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc (v1, v2, w) -> if v1 = vertex then ((v2,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 (a, w) = node in if a = b then ([b], w) else if (List.exists (fun x -> x = a) visited) then raise Fail else let neighbour_nodes = neighbours g a in match neighbour_nodes with | [] -> raise Fail | _ -> let (a_list, weights) = aux_list neighbour_nodes (a :: visited) in ((a :: a_list), w + weights) 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: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (a, w) = node in if a = b then (sc ([b], w)) else if (List.exists (fun x -> x = a) visited) then fc () else let neighbour_nodes = neighbours g a in match neighbour_nodes with | [] -> fc () | _ -> aux_list neighbour_nodes (a :: visited) fc (fun (a_list, weights) -> sc ((a :: a_list), w + weights)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x :: xs -> aux_node x visited (fun () -> aux_list xs visited fc sc) sc in aux_node (a,0) [] (fun () -> raise Fail) (fun r -> r);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in match paths with | [] -> None | _ -> let (_, weights) = List.split paths in let max_weight = (List.nth (List.sort compare weights) ((List.length weights) - 1)) in let longest_paths = List.find_all (fun (_, w) -> w = max_weight) paths in Some (List.nth longest_paths ((List.length longest_paths) - 1));;
let open_account (initial_pass: passwd) : bank_account = let password: passwd ref = ref initial_pass in let accHoldings = ref 0 in let pass_failed_attempts = ref 0 in let auth_password (pass: passwd) : bool = if !pass_failed_attempts >= 5 then raise too_many_failures; if pass = !password then begin pass_failed_attempts := 0; true end else begin pass_failed_attempts := !pass_failed_attempts + 1; raise wrong_pass end in let update_pass old_pass new_pass = if old_pass = !password then begin password := new_pass; pass_failed_attempts := 0 end else begin pass_failed_attempts := !pass_failed_attempts + 1; raise wrong_pass end in let retrieve pass amount = if auth_password(pass) then begin if amount < 0 then raise negative_amount; if amount <= !accHoldings then begin accHoldings := !accHoldings - amount; end else raise not_enough_balance end in let deposit pass amount = if auth_password(pass) then begin if amount < 0 then raise negative_amount; accHoldings := !accHoldings + amount end in let show_balance (pass : passwd) : int = if auth_password(pass) then !accHoldings else raise wrong_pass in let new_account : bank_account = { update_pass; retrieve; deposit; show_balance } in new_account ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let neighbourHelper edge neighbours = let (origin, destination, weight) = edge in if origin = vertex then (destination, weight) :: neighbours else neighbours in List.fold_right neighbourHelper (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) w : ('a list * weight) = let new_visited = (fst node) :: visited in let new_w = w + (snd node) in let new_neighbours = neighbours g (fst node) in if List.length new_neighbours = 0 then raise Fail; aux_list new_neighbours new_visited new_w and aux_list (nodes: ('a * weight) list) (visited: 'a list) w : ('a list * weight) = match nodes with [] -> raise Fail | hd::_ when (fst hd) = b -> begin (fst hd) :: visited, w + (snd hd) end | hd::tl -> if List.mem (fst hd) visited then aux_list tl visited w else try aux_node hd visited w with Fail -> aux_list tl visited w in let ans = aux_list (neighbours g a) [a] 0 in let final = (List.rev (fst ans), snd ans) in final;;
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 w : ('a list * weight)= let new_visited = (fst node) :: visited in let new_w = w + (snd node) in let new_neighbours = neighbours g (fst node) in if List.exists (fun c -> (fst c) = b) new_neighbours then begin let dest = List.find (fun c -> (fst c) = b) new_neighbours in aux_list [dest] new_visited fc sc new_w end else if List.length new_neighbours = 0 then aux_list (fst fc) (snd fc) fc sc w else aux_list new_neighbours new_visited fc sc new_w and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc w : ('a list * weight) = match nodes with [] -> raise Fail | hd::_ when (fst hd) = b -> sc hd visited w | hd::tl -> if List.mem (fst hd) visited then aux_list tl visited fc sc w else begin let fc = (tl, visited) in aux_node hd visited fc sc w end in let sc = (fun c visited w -> (fst c) :: visited, w + (snd c)) in let fcIn = ((neighbours g a), [a]) in let ans = aux_node (a, 0) [] fcIn sc 0 in let final = (List.rev (fst ans), snd ans) in final;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in if List.length paths = 0 then None else Some (List.hd (List.sort (fun c d -> compare (snd c) (snd d)) paths)) ;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let curr_bal = ref 0 in let num_tries = ref 0 in { update_pass = (fun x y -> if (x = !password) then (password := y ; num_tries := 0;()) else (num_tries := !num_tries +1; raise wrong_pass)); deposit= (fun x y -> if (!num_tries >= 5)then(raise too_many_failures) else if (x <> !password) then (num_tries := !num_tries +1; raise wrong_pass ) else if y<0 then ( num_tries := 0; raise negative_amount) else (num_tries := 0; curr_bal := !curr_bal + y)); retrieve= (fun x y -> if (!num_tries >= 5)then(raise too_many_failures); if (x <> !password) then (num_tries := !num_tries +1; raise wrong_pass) else if (!curr_bal < y) then (num_tries := 0; raise not_enough_balance) else if y<0 then (num_tries := 0; raise negative_amount) else (num_tries := 0;curr_bal := !curr_bal - y; ())); show_balance = (fun x -> if (!num_tries >= 5)then(raise too_many_failures) else if (x <> !password) then (num_tries := !num_tries +1; raise wrong_pass) else ( num_tries := 0; !curr_bal)); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec traverse edges looking = match edges with | [] -> [] | x::xs -> let (y,m,w) = x in if y = looking then (m, w)::(traverse xs looking) else traverse xs looking in traverse g.edges vertex;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (n: 'a * weight) (visited : 'a list) : ('a list * weight) = let (node,nodeweight) = n in if node = b then ([b], nodeweight) else if (not (List.mem node visited)) then ( let (a,b) = (aux_list (neighbours g node) (visited@[node])) in (node::a, b+nodeweight)) else raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::rest -> let (node,nodeweight) = x in try aux_node x visited; with Fail -> aux_list rest visited in aux_node (a, 0) [] ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (n: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (node,nodeweight) = n in if node = b then sc ([b], nodeweight) else if (not (List.mem node visited)) then ( let (a,b) = (aux_list (neighbours g node) (visited@[node]) fc sc) in (node::a, b+nodeweight)) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::rest -> let (node,nodeweight) = x in let suc2 = sc in let fail2 = fun () -> aux_list rest visited fc sc in aux_node x visited fail2 suc2 in aux_node (a, 0) [] (fun () -> raise Fail) (fun (c,d) -> (c,d));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = if find_all_paths g a b = [] then None else let rec findit highcost highpath allpaths = match allpaths with | [] -> Some (highpath, highcost) | (list, weight)::xs -> if (weight > highcost) then (findit weight list xs) else (findit highcost highpath xs) in findit 0 [] (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let curr_bal = ref 0 in let num_tries = ref 0 in { update_pass = (fun x y -> if (x = !password) then (password := y ; num_tries := 0;()) else (num_tries := !num_tries +1; raise wrong_pass)); deposit= (fun x y -> if (!num_tries >= 5)then(raise too_many_failures) else if (x <> !password) then (num_tries := !num_tries +1; raise wrong_pass ) else if y<0 then ( num_tries := 0; raise negative_amount) else (num_tries := 0; curr_bal := !curr_bal + y)); retrieve= (fun x y -> if (!num_tries >= 5)then(raise too_many_failures); if (x <> !password) then (num_tries := !num_tries +1; raise wrong_pass) else if (!curr_bal < y) then (num_tries := 0; raise not_enough_balance) else if y<0 then (num_tries := 0; raise negative_amount) else (num_tries := 0;curr_bal := !curr_bal - y; ())); show_balance = (fun x -> if (!num_tries >= 5)then(raise too_many_failures) else if (x <> !password) then (num_tries := !num_tries +1; raise wrong_pass) else ( num_tries := 0; !curr_bal)); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec traverse edges looking = match edges with | [] -> [] | x::xs -> let (y,m,w) = x in if y = looking then (m, w)::(traverse xs looking) else traverse xs looking in traverse g.edges vertex;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (n: 'a * weight) (visited : 'a list) : ('a list * weight) = let (node,nodeweight) = n in if node = b then ([b], nodeweight) else if (not (List.mem node visited)) then ( let (a,b) = (aux_list (neighbours g node) (visited@[node])) in (node::a, b+nodeweight)) else raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::rest -> let (node,nodeweight) = x in try aux_node x visited; with Fail -> aux_list rest visited in aux_node (a, 0) [] ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (n: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (node,nodeweight) = n in if node = b then sc ([b], nodeweight) else if (not (List.mem node visited)) then (aux_list (neighbours g node) (node :: visited) fc (fun (p, c) -> sc (node :: p, c + nodeweight))) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::rest -> let (node,nodeweight) = x in let suc2 = sc in let fail2 = fun () -> aux_list rest visited fc sc in aux_node x visited fail2 suc2 in aux_node (a, 0) [] (fun () -> raise Fail) (fun (c,d) -> (c,d));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = if find_all_paths g a b = [] then None else let rec findit highcost highpath allpaths = match allpaths with | [] -> Some (highpath, highcost) | (list, weight)::xs -> if (weight > highcost) then (findit weight list xs) else (findit highcost highpath xs) in findit 0 [] (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = notimplemented () ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = notimplemented ();;
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) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented ();;
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 (p: passwd) : bank_account = let password = ref p in let attempts = ref 0 in let balance = ref 0 in let check_password (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 (old_password: passwd) (new_password: passwd) -> if (old_password = !password) then (password := new_password; attempts := 0) else (attempts := !attempts+1; raise wrong_pass)); retrieve = (fun (p: passwd) (amount: int) -> if(amount < 0) then raise negative_amount; if (check_password p) then if (amount <= !balance) then balance := !balance - amount else raise not_enough_balance); deposit = (fun (p: passwd) (amount: int) -> if(amount < 0) then raise negative_amount; if (check_password p) then balance := !balance + amount); show_balance = (fun (p: passwd) -> if (check_password p) then !balance else raise too_many_failures); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f (ww: ('a * weight) list) (edge: ('a * 'a * weight)): ('a * weight) list = let (ver1, ver2, w) = edge in if (ver1 = vertex) then (ver2, w)::ww else ww in List.fold_left f [] g.edges ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (ww: weight) (visited : 'a list) : ('a list * weight) = let (ver1,ver2) = node in if (List.mem ver1 visited) then raise Fail else if (b == ver1) then (visited@[ver1], ver2 + ww) else aux_list (neighbours g ver1) (visited@[ver1]) (ver2 + ww); and aux_list (nodes: ('a * weight) list) (visited: 'a list) (ww: weight) : ('a list * weight) = match nodes with | [] -> raise Fail | h::t -> try aux_node h ww visited with Fail -> aux_list t visited ww 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) (ww: weight) (visited : 'a list) (aa) (bb) : ('a list * weight)= let (ver1,ver2) = node in if (List.mem ver1 visited) then aa () else if (b == ver1) then bb (visited@[ver1], ver2 + ww) else aux_list (neighbours g ver1) (ver2 + ww) (visited@[ver1]) aa bb; and aux_list (all_nodes: ('a * weight) list) (ww: weight) (visited: 'a list) (aa) (bb) : ('a list * weight) = match all_nodes with | [] -> aa () | rest::rest2 -> aux_node rest ww visited (fun () -> aux_list rest2 ww visited aa bb) bb in aux_node (a, 0) 0 [] (fun () -> raise Fail) (fun (g, ver2) -> (g, ver2)) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let f (best) (l,w) = match best with | None -> Some(l,w) | Some(maxPath, maxWeight) -> if (w > maxWeight) then Some(l,w) else Some(maxPath, maxWeight) in List.fold_left f None (find_all_paths g a b) ;;
let open_account (initial_pass: passwd) : bank_account = let cur_passwd = ref initial_pass in let cur_amount = ref 0 in let attempt_counter = ref 0 in { update_pass = (fun input_passwd new_passwd -> if !cur_passwd <> input_passwd then (attempt_counter := !attempt_counter + 1; raise wrong_pass) else (cur_passwd := new_passwd; attempt_counter := 0) ); deposit = (fun input_passwd amount -> if !attempt_counter >= 5 then raise too_many_failures else if !cur_passwd = input_passwd then (attempt_counter := 0; if amount < 0 then raise negative_amount else cur_amount := !cur_amount + amount ) else (attempt_counter := !attempt_counter + 1; raise wrong_pass) ); retrieve = (fun input_passwd amount -> if !attempt_counter >= 5 then raise too_many_failures else if !cur_passwd = input_passwd then (attempt_counter := 0; if amount < 0 then raise negative_amount else if !cur_amount < amount then raise not_enough_balance else cur_amount := !cur_amount - amount ) else (attempt_counter := !attempt_counter + 1; raise wrong_pass;) ); show_balance = (fun input_passwd -> if !attempt_counter >= 5 then raise too_many_failures else if !cur_passwd = input_passwd then (attempt_counter := 0; !cur_amount; ) else (attempt_counter := !attempt_counter + 1; raise wrong_pass;) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let l1 = [] in let rec neighbours_helper (edge: ('a * 'a * weight) list) (vertex: 'a) (input_list: ('a * weight) list) : ('a * weight) list = match edge with | [] -> input_list | (a, b, c) :: t -> if a = vertex then neighbours_helper t vertex [(b,c)] @ input_list else neighbours_helper t vertex input_list in neighbours_helper g.edges vertex l1;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let sum_weight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = match node with |(value,weight) -> if List.mem value visited then raise Fail else ( if value = b then ((visited @ [value]), !sum_weight) else let neighbour = neighbours g value in aux_list neighbour (visited @ [value]) ) |_-> raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |h::t -> match h with |(value2, weight2) -> (try sum_weight := !sum_weight + weight2; aux_node h visited; with Fail -> sum_weight := !sum_weight - weight2; aux_list t visited) |_-> raise Fail in let neighbour2 = neighbours g a in aux_list neighbour2 [a];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let sum_weight = ref 0 in let added = ref [] in let subtract_weight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (value, weight) = node in if (not (List.mem value visited)) then ( sum_weight := !sum_weight + weight; subtract_weight := weight; if value = b then ( added := visited @ [value]; sc(); ) else let fc2 = fun () -> aux_list (neighbours g value) visited fc sc in aux_list (neighbours g value) (visited @ [value]) fc2 sc ) else (subtract_weight := 0; fc()) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc() |h::t -> let (value2,weight2) = h in let sc2 = sc in let fc2 = fun () -> aux_list t visited fc sc in aux_node h visited fc2 sc2 in let fail = fun () -> sum_weight := !sum_weight - !subtract_weight; raise Fail in aux_list (neighbours g a) [a] (fail) (fun () -> (!added, !sum_weight));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = try Some (find_path g a b) with Fail -> None;;
let open_account (initial_pass: passwd) : bank_account = let current_pass = ref initial_pass in let invalid_attempt = ref 0 in let current_balance = ref 0 in { update_pass = (fun old_pass new_pass -> if old_pass <> !current_pass then (invalid_attempt := !invalid_attempt + 1; raise wrong_pass) else (invalid_attempt := 0 ; current_pass := new_pass)); deposit = (fun entered_pass amount -> if !invalid_attempt >= 5 then raise too_many_failures else if entered_pass = !current_pass then (if amount < 0 then raise negative_amount else (current_balance := !current_balance + amount; invalid_attempt := 0)) else (invalid_attempt := !invalid_attempt + 1; raise wrong_pass)); retrieve = (fun entered_pass amount -> if !invalid_attempt >= 5 then raise too_many_failures else if entered_pass = !current_pass then (if amount < 0 then raise negative_amount else invalid_attempt := 0; (if !current_balance < amount then raise not_enough_balance else current_balance := !current_balance - amount)) else (invalid_attempt := !invalid_attempt + 1; raise wrong_pass)); show_balance = (fun entered_pass -> if !invalid_attempt >= 5 then raise too_many_failures else if entered_pass <> !current_pass then (invalid_attempt := !invalid_attempt + 1; raise wrong_pass) else (invalid_attempt := 0; !current_balance)); };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let isNeighbours (theList: ('a * weight) list) (edge: ('a * 'a * weight)) = let theVertex = vertex in let (x,y,z) = edge in if x = theVertex then (y,z)::theList else theList in List.fold_left isNeighbours [] 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) (weight: int) : ('a list * weight) = let (x,_) = node in (if List.mem x visited then raise Fail else aux_list (neighbours g x) (x::visited) weight ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (weight: int) : ('a list * weight) = match nodes with |[] -> raise Fail |(x,y)::t -> if x = b then ([x], weight + y) else (try let (c,d) = aux_node (x,y) visited (y+weight) in x::c,(d) with Fail -> try (aux_list t visited weight) with Fail -> raise Fail) in if a = b then ([],0) else aux_list [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) fc sc (weight: int) : ('a list * weight)= let (node,theWeight) = node in if List.mem node visited then fc() else aux_list (neighbours g node) (node::visited) fc (fun(e,f)-> sc (node::e, f+theWeight)) weight and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc (weight: int) : ('a list * weight) = match nodes with | (x,y)::t -> if x = b then sc ([x],y) else aux_node (x,y) visited (fun () -> aux_list t visited fc sc weight) sc weight | [] -> fc() in aux_node (a,0) [] (fun() -> raise Fail) (fun (c,d) -> (c,d)) 0;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let result = List.sort (fun (_,d) (_,f) -> compare d f ) (find_all_paths g a b) in if result = [] then None else Some (List.nth (List.rev result) 0);;
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let password = ref initial_pass in let num_pass_fail = ref 0 in let max_pass_fail = 5 in let auth max f pass = if !num_pass_fail < max || max = 0 then if pass = !password then ( num_pass_fail := 0; f () ) else ( num_pass_fail := !num_pass_fail + 1; raise wrong_pass ) else raise too_many_failures in let update_pass = (:=) password in let retrieve amount = if amount <= !balance then if amount >= 0 then balance := !balance - amount else raise negative_amount else raise not_enough_balance in let deposit amount = if amount >= 0 then balance := !balance + amount else raise negative_amount in { update_pass = auth 0 (fun () -> update_pass); retrieve = auth max_pass_fail (fun () -> retrieve); deposit = auth max_pass_fail (fun () -> deposit); show_balance = auth max_pass_fail (fun () -> !balance); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = g.edges |> List.filter (fun (v1, _, _) -> v1 = vertex) |> List.rev_map (fun (_, v2, w) -> (v2, w));;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux rev_path cost = match rev_path with | [] -> raise (Msg "rev_path is not supposed to be empty") | curr :: _ -> if curr = b then (List.rev rev_path, cost) else let rec aux' l = match l with | [] -> raise Fail | (v, w) :: ns -> try aux (v :: rev_path) (cost + w) with Fail -> aux' ns in neighbours g curr |> List.filter (fun (v, _) -> not (List.mem v rev_path)) |> aux' in aux [a] 0;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux rev_path cost success fail = match rev_path with | [] -> raise (Msg "rev_path is not supposed to be empty") | curr :: _ -> if curr = b then success (List.rev rev_path, cost) else let ns = neighbours g curr |> List.filter (fun (v, _) -> not (List.mem v rev_path)) in match ns with | [] -> fail () | (v, w) :: ns -> let fail = List.fold_left (fun f (vv, ww) -> fun () -> aux (vv :: rev_path) (cost + ww) success f ) fail ns in aux (v :: rev_path) (cost + w) success fail in aux [a] 0 (fun n -> n) (fun () -> raise Fail);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match find_all_paths g a b with | [] -> None | path :: paths -> Some( List.fold_left (fun a b -> if snd b > snd a then b else a) path paths );;
let open_account (initial_pass: passwd) : bank_account = let ini_pw = ref initial_pass in let balance = ref 0 in let failure = ref 0 in let check_password password = if password = !ini_pw then failure := 0 else (failure := !failure + 1; if !failure <= 5 then raise wrong_pass else raise too_many_failures) in let check_p p = if p = !ini_pw then failure := 0 else (failure := !failure + 1; raise wrong_pass) in let check_amount amt = if amt < 0 then raise negative_amount in let retrieve_money amt = if amt > (!balance) then raise not_enough_balance else balance := !balance - amt in let deposit_money amt = balance := !balance + amt in let update_pass old_p new_p = check_p old_p; failure := 0; ini_pw := new_p; in let deposit p amt = if !failure >= 5 then raise too_many_failures else (check_password p; failure := 0; check_amount amt; deposit_money amt;) in let show_balance p = if !failure >= 5 then raise too_many_failures else( check_password p; failure := 0; !balance;) in let retrieve p amt = if !failure >= 5 then raise too_many_failures else( check_password p; failure := 0; check_amount amt; retrieve_money amt;) in { update_pass; deposit; show_balance; retrieve; } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec find_outnode edges visited = match edges with |(a,d,w)::xs -> if a = vertex then find_outnode xs ((d,w)::visited) else find_outnode xs visited |[] -> visited in find_outnode g.edges [] ;; let rev_list l = let rec rev_acc acc = function | [] -> acc | hd::tl -> rev_acc (hd::acc) tl in rev_acc [] l;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) dist: ('a list * weight) = let (v,w) = node in if (v = b) then ((rev_list (b::visited)),(dist+w)) else (if (List.mem v visited) then raise Fail else aux_list (neighbours g v) (v :: visited) (dist+w) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) dist: ('a list * weight) = match nodes with |[]->raise Fail |hd::tl-> try aux_node hd visited dist with Fail -> aux_list tl visited dist 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) fc dist: ('a list * weight) = let (v,w) = node in if (v = b) then ((rev_list (b::visited)),(dist+w)) else (if (List.mem v visited) then fc() else aux_list (neighbours g v) (v :: visited) fc (dist+w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc dist: ('a list * weight) = match nodes with |[]->fc() |hd::tl-> try aux_node hd visited fc dist with Fail -> aux_list tl visited fc dist in aux_node (a,0) [] (fun () -> raise Fail) 0 let r_l l = match l with |hd :: tl -> tl let rec sum l acc= match l with |hd :: tl -> sum tl hd+acc |[]->acc let rec get_l l acc = match l with |hd :: tl -> get_l tl (hd::acc) |[]-> let (x,y) = (List.nth acc 0) in x;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let l = find_all_paths g a b in match l with | [] -> None | _ -> let rec compare l acc weight = match l with | hd :: tl -> let (a,b) = hd in if b > weight then compare tl hd b else compare tl acc weight | [] -> acc in Some (compare l ([], 0) 0);;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let num_wrong = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> match old_pass with | x when x = !pass -> num_wrong := 0; pass := new_pass |_ -> num_wrong := !num_wrong + 1; raise wrong_pass); deposit = (fun (old_pass: passwd) (amt: int) -> match !num_wrong with | b when b >= 5 -> raise too_many_failures |_ -> match old_pass with | x when x = !pass -> num_wrong := 0; (match amt with |c when c >= 0 -> balance := !balance + amt |_ -> raise negative_amount) |_ -> num_wrong := !num_wrong + 1; raise wrong_pass); retrieve = (fun (old_pass: passwd) (amt: int) -> match !num_wrong with | b when b >= 5 -> raise too_many_failures |_ -> match old_pass with | x when x = !pass -> num_wrong := 0; (match amt with | p when !balance - p < 0 -> raise not_enough_balance |c when c >= 0 -> balance := !balance - amt |_ -> raise negative_amount) |_ -> num_wrong := !num_wrong + 1; raise wrong_pass); show_balance = (fun (old_pass: passwd) -> match !num_wrong with | b when b >= 5 -> raise too_many_failures |_ -> match old_pass with | x when x = !pass -> num_wrong := 0; !balance |_ -> num_wrong := !num_wrong + 1; raise wrong_pass); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let is_v1 x = match x with | (a, b, c) when a = vertex -> true |_ -> false in let l1 = List.filter is_v1 g.edges in List.map (fun (a1, b, c) -> (b, c)) l1;;
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) = match node with |(x, y) when x = b -> ([b], y) |(x, y) when List.mem x visited -> raise Fail |(x, y) -> try let z = aux_list (neighbours g x) (x :: visited) in let (e,f) = z in (x :: e, f + y) with Fail -> raise Fail 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 try let l1 = aux_list (neighbours g a) [a] in let (g,h) = l1 in (a :: g, h) with Fail -> raise Fail;;
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)= match node with |(x, y) when x = b -> sc ([b], y) |(x, y) when List.mem x visited -> fc () |(x, y) -> let succ = (fun r -> let (c,d) = r in sc (x :: c, d + y)) in aux_list (neighbours g x) (x :: 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 fail2 = (fun () -> aux_list xs visited fc sc) in aux_node x visited (fail2) sc in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun r -> let (c,d) = r in (a :: c, d));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let l1 = find_all_paths g a b in match l1 with |[] -> None |x :: xs -> Some (List.fold_left (fun x y-> let (c, d) = x in let (e,f) = y in if f > d then y else x) ([], 0) l1);;;
let open_account (initial_pass: passwd) : bank_account = let pass= ref initial_pass in let balance= ref 0 in let counter= ref 0 in { update_pass =( fun old_pass new_pass -> if old_pass = !pass then ( pass := new_pass; counter := 0; ) else ( counter := !counter +1; raise wrong_pass ) ); deposit =( fun password amount -> if !counter >= 5 then raise too_many_failures else ( if password = !pass then ( counter := 0; if amount < 0 then raise negative_amount else ( balance := !balance + amount; ) ) else ( counter := !counter +1; raise wrong_pass ) ) ); retrieve =( fun password amount -> if !counter >= 5 then raise too_many_failures else ( if password = !pass then ( counter := 0; if amount < 0 then raise negative_amount else ( if !balance < amount then raise not_enough_balance else ( balance := !balance - amount; ) ) ) else ( counter := !counter +1; raise wrong_pass ) ) ); show_balance =( fun password -> if !counter >= 5 then raise too_many_failures else ( if password = !pass then ( counter := 0; !balance ) else ( counter := !counter +1; raise wrong_pass ) ) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let check l ( (v1, v2, w): ('a * 'a * weight) ) = if vertex = v1 then (v2, w) :: l else l in List.fold_left (check) [] 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 (n , w) = node in let add_el el = let (l, curr_w) = el in (List.cons n l , curr_w + w) in if n = b then ( [n] , w ) else match neighbours g n with | [] -> raise Fail | lst -> add_el (aux_list lst (List.cons n visited)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (n , w) :: tl -> ( if List.mem n visited then aux_list tl visited else (try (aux_node (n , w) 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 (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (n , w) = node in if n = b then sc ( [n] , w ) else match neighbours g n with | [] -> fc () | lst -> aux_list lst (List.cons n visited) fc ( fun r -> let (l, curr_w) = r in sc (List.cons n l , curr_w + w) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc() | (n , w) :: tl -> ( if List.mem n visited then aux_list tl visited fc sc else aux_node (n , w) visited (fun() -> aux_list tl visited fc sc) sc ) in aux_node (a, 0) [] (fun () -> raise Fail) (fun r -> r);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match find_all_paths g a b with | [] -> None | (lst, w) :: tl -> ( let rec find_longest l (long, highest_cost)= match l with | [] -> Some ( (long, highest_cost) ) | ( cur , cur_cost) :: tl -> ( if cur_cost > highest_cost then find_longest tl (cur, cur_cost) else find_longest tl (long, highest_cost) ) in find_longest tl (lst, w) ) ;;
let open_account (initial_pass: passwd) : bank_account = let update_password info old_pass new_pass : unit = if old_pass = info.password then ( info.password <- new_pass; info.failures <- 0 ) else ( info.failures <- info.failures + 1; raise wrong_pass ) in let ret info pass money : unit = if info.failures >= 5 then raise too_many_failures else if pass = info.password then ( info.failures <- 0; if money < 0 then raise negative_amount else if info.money >= money then info.money <- info.money - money else raise not_enough_balance ) else if info.failures < 5 then ( info.failures <- info.failures + 1; raise wrong_pass ) else raise too_many_failures in let dep info pass money : unit = if info.failures >= 5 then raise too_many_failures else if pass = info.password then ( info.failures <- 0; if money < 0 then raise negative_amount else info.money <- info.money + money ) else if info.failures < 5 then ( info.failures <- info.failures + 1; raise wrong_pass ) else raise too_many_failures in let bal info pass : int = if info.failures >= 5 then raise too_many_failures else if pass = info.password then (info.failures <- 0; info.money) else if info.failures < 5 then ( info.failures <- info.failures + 1; raise wrong_pass ) else raise too_many_failures in let info = { password = initial_pass; failures = 0; money = 0; } in { update_pass = update_password info; retrieve = ret info; deposit = dep info; show_balance = bal info; };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (fun (_, y, z) -> (y, z)) (List.filter (fun (x, _, _) -> x = vertex) g.edges);;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let neigh src path = List.filter (fun (x, _) -> not (List.mem x path)) (neighbours g src) in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let l = fun _ -> aux_list (neigh (fst node) visited) ((fst node)::visited) in if (fst node) = b then ([fst node], snd node) else ((fst node)::(fst (l 0)),(snd node) + (snd (l 0))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | h::t -> try aux_node h visited with Fail -> aux_list t ((fst h)::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 neigh src path = List.filter (fun (x, _) -> not (List.mem x path)) (neighbours g src) in let l = fun _ -> aux_list (neigh (fst node) visited) ((fst node)::visited) fc sc in if (fst node) = b then sc ([], 0) else (l 0) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | h::t -> let fail = fun () -> aux_list t ((fst h)::visited) fc sc in aux_node h visited fail (fun (x, y) -> sc (x @ [fst h], y + (snd h))) in let (x, y) = aux_node (a, 0) [] (fun () -> raise Fail) (fun x -> x) in (a::(List.rev x), y);;
let find_longest_path g a b : ('a list * int) option = let l = find_all_paths g a b in if List.length l > 0 then Some (List.hd (List.sort (fun (_,y) (_,x)-> compare x y) (l))) else None;;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let count = ref 0 in { update_pass = (fun (oldpass:passwd) (npwd:passwd) -> if (oldpass = !pass) then (pass:=npwd ; count := 0) else (count := !count+1 ; raise wrong_pass)) ; deposit = (fun (pwd:passwd) (amount:int) -> if (!count >= 5) then raise too_many_failures else if (pwd = !pass) then (if (amount<0) then (raise negative_amount := 0) else balance:= !balance+amount; count := 0) else (count := !count+1; raise wrong_pass ) ) ; retrieve = (fun (pwd:passwd) (amount:int) -> if (!count >= 5) then raise too_many_failures else if (pwd = !pass) then ( if (!balance<amount) then (count := 0; raise not_enough_balance) else if (amount<0) then (count := 0; raise negative_amount) else count := 0; balance := !balance-amount) else (count:= !count+1; raise wrong_pass)); show_balance = (fun (pwd:passwd) -> if (!count >= 5) then raise too_many_failures else if (pwd = !pass) then (count:=0; !balance) else (count := !count + 1 ;raise wrong_pass)) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper (g: 'a graph) (vertex: 'a) (acc: ('a * weight) list) = match g with | { nodes=_ ; edges=[]} -> acc | {nodes= l1 ; edges= (a, b, weight) :: tl } -> if a=vertex then helper {nodes= l1 ; edges = tl} vertex ((b, weight)::acc) else helper {nodes= l1 ; edges = tl} vertex acc in helper g vertex [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let l1 = neighbours g a in let rec aux_node (node: 'a * weight) (visited : 'a list) (weight:int): ('a list * weight) = let (d, w) = node in if (List.mem d visited) then raise Fail else if d=b then ((visited@[d]), w+weight) else aux_list (neighbours g d) (visited@[d]) (weight+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (weight:int): ('a list * weight) = match nodes with | [] -> raise Fail | (c, w) :: tl -> try aux_node (c, w) (visited) (weight) with Fail -> aux_list tl visited weight in aux_list l1 [a] 0 ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let l1 = neighbours g a in let rec aux_node (node: 'a * weight) (visited : 'a list) (weight:int) fc sc : ('a list * weight)= let (c, w) = node in if (List.mem c visited) then fc() else if c=b then (sc c w) else aux_list (neighbours g c) (visited@[c]) (weight+w) (fun () -> raise Fail) (fun x y -> (visited@[x], weight + y)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (weight:int) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (d, w) :: tl -> try aux_node (d, w) visited weight (fc) (fun x y -> (visited@[x], weight+y)) with Fail -> aux_list tl visited weight fc sc in aux_list l1 [a] 0 (fun() -> raise Fail) (fun x y -> ([x], y));;
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 pass = ref initial_pass in let cur_balance = ref 0 in let attempts = ref 0 in { update_pass = (fun oldpass newpass -> if oldpass = !pass then begin attempts := 0; pass := newpass end else begin attempts := !attempts + 1; raise wrong_pass end); deposit = (fun oldpass amount -> if !attempts >= 5 then raise too_many_failures else if oldpass = !pass then begin attempts := 0; cur_balance := !cur_balance + amount; end else if amount < 0 then raise negative_amount else begin attempts := !attempts + 1; raise wrong_pass end ); retrieve = (fun oldpass amount -> if !attempts >= 5 then raise too_many_failures else if amount < 0 then raise negative_amount else if oldpass = !pass then begin attempts := 0; if !cur_balance >= amount then cur_balance := !cur_balance - amount else raise not_enough_balance end else begin attempts := !attempts + 1; raise wrong_pass end ); show_balance = (fun oldpass -> if !attempts >= 5 then raise too_many_failures else if oldpass = !pass then begin attempts := 0; !cur_balance end else begin attempts := !attempts + 1; raise wrong_pass end) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let foldR = List.fold_right (fun edge nodes -> let (a, b, wgt) = edge in if a = vertex then (b, wgt) :: nodes else nodes ) in foldR g.edges [] ;; let rec append lst1 lst2 = match lst1 with | [] -> lst2 | h :: t -> h :: (append t lst2) ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node node visited listnodes: ('a list * weight) = match (neighbours g node) with | [] -> raise Fail | _ -> aux_list (neighbours g node) visited listnodes and aux_list (nodes : ('a * weight) list) (visited: 'a list) listnodes : ('a list * weight) = match nodes with | [] -> raise Fail | (vertex, weight) :: t -> if List.mem vertex visited then aux_list t visited listnodes else if vertex = b then begin ((append visited [vertex]), (listnodes + weight)) end else begin try aux_node vertex (append visited [vertex]) (listnodes + weight) with | Fail -> aux_list t visited listnodes end in aux_node a [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) listnodes fc sc : ('a list * weight)= let (vertex, weight) = node in if (List.mem vertex visited) then fc () else if (vertex = b) then sc ((append visited [vertex]), listnodes + weight) else aux_list (neighbours g vertex) (append visited [vertex]) (listnodes + weight) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) listnodes fc sc : ('a list * weight) = match nodes with | [] -> fc () | h :: t -> aux_node h visited listnodes (fun () -> aux_list t visited listnodes fc sc) sc in aux_node (a,0) [] 0 (fun () -> raise Fail) (fun n -> n) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let path (worst) (l,w) = match worst with | None -> Some(l,w) | Some(maxPath, maxWeight) -> if (w > maxWeight) then Some(l,w) else Some(maxPath, maxWeight) in List.fold_left path None (find_all_paths g a b) ;;
let open_account (initial_pass: passwd) : bank_account = let tries_count : int ref = ref 0 in let password : passwd ref = ref initial_pass in let acc_balance : int ref = ref 0 in {update_pass = (fun old_pass new_pass -> if old_pass = !password then (tries_count := 0; password := new_pass;) else (tries_count := !tries_count + 1; raise wrong_pass;)); deposit = (fun pass amount -> if (!tries_count = 5) then raise too_many_failures else if pass = !password then (if (amount < 0) then raise negative_amount else (acc_balance := !acc_balance + amount; tries_count := 0;)) else (tries_count := !tries_count +1; raise wrong_pass;)); retrieve = (fun pass amount -> if (!tries_count = 5) then raise too_many_failures else if pass = !password then (if (amount < 0) then raise negative_amount else if (!acc_balance - amount < 0) then raise not_enough_balance else (acc_balance := !acc_balance - amount; tries_count := 0;)) else (tries_count := !tries_count +1; raise wrong_pass;)); show_balance = (fun pass -> if (!tries_count = 5) then raise too_many_failures else if pass = !password then (tries_count := 0; !acc_balance;) else (tries_count := !tries_count +1; raise wrong_pass;)); };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f = fun (a,b,c) s -> if a = vertex then (b,c)::s else s in List.fold_right (f) g.edges [] let helper g node b weight visited aux_list neighbour = if node = b then ([node], weight) else if (neighbour = []) then raise Fail else if (List.mem node visited) then raise Fail else let (n, w) = aux_list neighbour (node::visited) in (node::n, weight+w);;
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 (nd, wt) = node in (let neighbour = neighbours(g)(nd) in (helper(g)(nd)(b)(wt)(visited)(aux_list)(neighbour)) ) 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 helper2 g node weight visited aux_list fc sc neighbour = if (neighbour = []) then fc () else if (List.mem node visited) then fc () else let f = fun t -> let (n,w) = t in sc((node::n),(weight+w)) in aux_list neighbour (node::visited) fc f;;
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 (nd, wt) = node in (let neighbour = neighbours(g)(nd) in (helper2(g)(nd)(wt)(visited)(aux_list)(fc)(sc)(neighbour)) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with [] -> fc () | [(nd,wt)] -> if b = nd then sc([nd],wt) else aux_node((nd,wt))(visited)(fc)(sc) | (nd,wt)::tl -> if b = nd then sc([nd],wt) else let f1 = (fun () -> aux_list(tl)(visited)(fc)(sc)) in aux_node((nd,wt))(visited)(f1)(sc) in aux_node(a,0)([])(fun () -> raise Fail)(fun(wt,nd) -> (wt,nd) ) let helper3 g node b weight visited aux_list neighbour = if b = node then [([node],weight)] else if (neighbour = []) then [] else if (List.mem(node)(visited)) then [] else (let l = aux_list(neighbour)(node::visited) in List.map (fun (p,c) -> (node::p, weight+c))(l));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let allPaths = find_all_paths(g)(a)(b) in helper4(allPaths);;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let wrongAttempts = ref 0 in { update_pass = (fun oldPass newPass -> if oldPass = !password then (password := newPass ; wrongAttempts := 0) else (wrongAttempts := (!wrongAttempts + 1); raise wrong_pass)); retrieve = (fun pass amount -> if !wrongAttempts < 5 then ( if pass = !password then (wrongAttempts := 0 ; if amount < 0 then raise negative_amount else (if !balance >= amount then balance := !balance - amount else raise not_enough_balance)) else (wrongAttempts := (!wrongAttempts + 1); raise wrong_pass )) else raise too_many_failures ); deposit = (fun pass amount -> if !wrongAttempts < 5 then (if pass = !password then (wrongAttempts := 0 ; if amount < 0 then raise negative_amount else balance := !balance + amount) else (wrongAttempts := (!wrongAttempts + 1); raise wrong_pass )) else raise too_many_failures ); show_balance = (fun pass -> if !wrongAttempts < 5 then (if pass = !password then (wrongAttempts := 0 ; !balance) else (wrongAttempts := (!wrongAttempts + 1); raise wrong_pass)) else raise too_many_failures ); } ;;