text
stringlengths 0
601k
|
---|
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((node, w): 'a * weight) (visited : 'a list) : ('a list * weight) = if node = b then ([b], w) else if List.mem node visited then raise Fail else let adj = neighbours g node in let (node_path, total_weight) = aux_list adj (node :: visited) in (node :: node_path, total_weight + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (node, w) :: tl -> try aux_node (node, 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, w): 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if node = b then sc ([b], w) else if List.mem node visited then fc () else let adj = neighbours g node in let sc2 = fun (node_path, total_weight) -> sc (node :: node_path, total_weight + w) in aux_list adj (node :: visited) fc sc2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (node, w) :: tl -> let fc2 = fun () -> aux_list tl visited fc sc in aux_node (node, w) visited fc2 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 | hd :: tl -> Some (List.fold_left ( fun (lpath, lweight) (path, weight) -> if weight > lweight then (path, weight) else (lpath, lweight) ) hd tl);; |
let open_account (initial_pass: passwd) : bank_account = let passwd = ref initial_pass in let balance = ref 0 in let incorrect_passwd_count = ref 0 in let update_pass_func old_passwd new_passwd = if old_passwd = !passwd then ( incorrect_passwd_count := 0; passwd := new_passwd ) else ( incorrect_passwd_count := !incorrect_passwd_count + 1; raise wrong_pass) in let deposit_func given_passwd amount = if !incorrect_passwd_count >= 5 then ( incorrect_passwd_count := !incorrect_passwd_count + 1; raise too_many_failures) else if given_passwd = !passwd then ( incorrect_passwd_count := 0; if amount > 0 then balance:= !balance + amount else raise negative_amount ) else ( incorrect_passwd_count := !incorrect_passwd_count + 1; raise wrong_pass) in let retrieve_func given_passwd amount = if !incorrect_passwd_count >= 5 then ( incorrect_passwd_count := !incorrect_passwd_count + 1; raise too_many_failures) else if given_passwd = !passwd then ( incorrect_passwd_count := 0; if amount > 0 then if ((!balance) - amount) >= 0 then balance:= !balance - amount else raise not_enough_balance else raise negative_amount ) else ( incorrect_passwd_count := !incorrect_passwd_count + 1; raise wrong_pass) in let show_balance_func given_passwd = if !incorrect_passwd_count >= 5 then ( incorrect_passwd_count := !incorrect_passwd_count + 1; raise too_many_failures) else if given_passwd = !passwd then ( incorrect_passwd_count := 0; !balance ) else ( incorrect_passwd_count := !incorrect_passwd_count + 1; raise wrong_pass) in { update_pass = update_pass_func ; deposit = deposit_func ; retrieve = retrieve_func ; show_balance = show_balance_func } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let is_correct_edge edge = let departure, destination, cost = edge in departure = vertex in let correct_edges = List.filter is_correct_edge g.edges in let edge_to_tuple edge = let (dep, dest, cost) = edge in (dest, cost) in List.map edge_to_tuple correct_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 : int) : ('a list * weight) = let (dest, cost) = node in let not_visited neighbour = let (dest, cost) = neighbour in not (List.mem dest visited) in let next_node_options = (List.filter not_visited (neighbours g dest)) in try let res = (aux_list next_node_options (dest::visited) (acc+cost)) in let (ls, weight) = res in (dest :: ls, weight + cost) with Fail -> raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc : int) : ('a list * weight) = match nodes with | [] -> raise Fail | x :: xs -> try let (dest, cost) = x in if dest = b then ([b], cost) else aux_node x visited acc with Fail -> aux_list xs visited acc in let ls = aux_list (neighbours g a) [a] 0 in match ls with | (ls, weight) -> (a::ls, weight) | _ -> 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) (acc : int) fail succeed : ('a list * weight) = let (dest, cost) = node in let not_visited neighbour = let (dest, cost) = neighbour in not (List.mem dest visited) in let next_node_options = (List.filter not_visited (neighbours g dest)) in let succeed res = let (ls, weight) = res in (dest :: ls, weight + cost) in aux_list next_node_options (dest::visited) (acc+cost) (raise Fail) succeed and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc : int) fail succeed : ('a list * weight) = match nodes with | [] -> raise Fail | x :: xs -> let fail = aux_list xs visited acc fail succeed in let (dest, cost) = x in if dest = b then succeed ([b], cost) else aux_node x visited acc fail (fun x -> x) in let ls = aux_list (neighbours g a) [a] 0 (raise Fail) (fun x -> x) in match ls with | (ls, weight) -> (a::ls, weight) | _ -> raise Fail ;; |
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 (ls : ('a list * weight) list) (longest : ('a list * weight)) : ('a list * weight) option = match ls with | [] -> Some(longest) | x :: xs -> let (longest_ls, longest_cost) = longest in let (ls, cost) = x in if cost > longest_cost then find_longest xs x else find_longest xs longest in match all_paths with | [] -> None | x :: xs -> find_longest xs x ;; |
let open_account (initial_pass: passwd) : bank_account = let passwd = ref initial_pass in let balance = ref 0 in let incorrect_passwd_count = ref 0 in let update_pass_func old_passwd new_passwd = if old_passwd = !passwd then ( incorrect_passwd_count := 0; passwd := new_passwd ) else ( incorrect_passwd_count := !incorrect_passwd_count + 1; raise wrong_pass) in let deposit_func given_passwd amount = if !incorrect_passwd_count >= 5 then ( incorrect_passwd_count := !incorrect_passwd_count + 1; raise too_many_failures) else if given_passwd = !passwd then ( incorrect_passwd_count := 0; if amount > 0 then balance:= !balance + amount else raise negative_amount ) else ( incorrect_passwd_count := !incorrect_passwd_count + 1; raise wrong_pass) in let retrieve_func given_passwd amount = if !incorrect_passwd_count >= 5 then ( incorrect_passwd_count := !incorrect_passwd_count + 1; raise too_many_failures) else if given_passwd = !passwd then ( incorrect_passwd_count := 0; if amount > 0 then if ((!balance) - amount) >= 0 then balance:= !balance - amount else raise not_enough_balance else raise negative_amount ) else ( incorrect_passwd_count := !incorrect_passwd_count + 1; raise wrong_pass) in let show_balance_func given_passwd = if !incorrect_passwd_count >= 5 then ( incorrect_passwd_count := !incorrect_passwd_count + 1; raise too_many_failures) else if given_passwd = !passwd then ( incorrect_passwd_count := 0; !balance ) else ( incorrect_passwd_count := !incorrect_passwd_count + 1; raise wrong_pass) in { update_pass = update_pass_func ; deposit = deposit_func ; retrieve = retrieve_func ; show_balance = show_balance_func } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let is_correct_edge edge = let departure, destination, cost = edge in departure = vertex in let correct_edges = List.filter is_correct_edge g.edges in let edge_to_tuple edge = let (dep, dest, cost) = edge in (dest, cost) in List.map edge_to_tuple correct_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 (dest, cost) = node in let not_visited neighbour = let (dest, cost) = neighbour in not (List.mem dest visited) in let next_node_options = (List.filter not_visited (neighbours g dest)) in try let res = (aux_list next_node_options (dest::visited)) in let (ls, weight) = res in (dest :: ls, weight + cost) 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 let (dest, cost) = x in if dest = b then ([b], cost) else aux_node x visited with Fail -> aux_list xs visited in let ls = aux_list (neighbours g a) [a] in match ls with | (ls, weight) -> (a::ls, weight) | _ -> 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) fail succeed : ('a list * weight) = let (dest, cost) = node in let not_visited neighbour = let (dest, cost) = neighbour in not (List.mem dest visited) in let next_node_options = (List.filter not_visited (neighbours g dest)) in let succeed res = let (ls, weight) = res in succeed (dest :: ls, weight + cost) in aux_list next_node_options (dest::visited) fail succeed and aux_list (nodes: ('a * weight) list) (visited: 'a list) fail succeed : ('a list * weight) = match nodes with | [] -> fail () | x :: xs -> let fail () = aux_list xs visited fail succeed in let (dest, cost) = x in if dest = b then succeed ([b], cost) else aux_node x visited fail succeed in let ls = aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun x -> x) in match ls with | (ls, weight) -> (a::ls, weight) | _ -> raise Fail ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec find_longest (ls : ('a list * weight) list) (longest : ('a list * weight)) : ('a list * weight) option = match ls with | [] -> Some(longest) | x :: xs -> let (longest_ls, longest_cost) = longest in let (ls, cost) = x in if cost > longest_cost then find_longest xs x else find_longest xs longest in let all_paths = find_all_paths g a b in match all_paths with | [] -> None | x :: xs -> find_longest xs x ;; |
let open_account (initial_pass: passwd) : bank_account = let update_ref (nref: int ref) (n : int) = nref := n; in let attempts = ref 0 in let balance = ref 0 in let account = ref initial_pass in { update_pass = (fun oldP newP -> if(String.equal oldP !account) then let a = (update_ref attempts 0) in account := newP; else let a = (update_ref attempts (!attempts+1)) in raise wrong_pass); retrieve = (fun pword amt -> if(not (String.equal pword !account)) then let a = (update_ref attempts (!attempts+1)) in raise wrong_pass else if(!attempts >= 5) then let a = (update_ref attempts (!attempts+1)) in raise too_many_failures else match amt with |amt when (amt < 0) -> raise negative_amount |amt when (amt > !balance) -> raise not_enough_balance |_ -> let a = (update_ref attempts 0) in balance := (!balance - amt)); deposit = (fun pword amt -> if(not (String.equal pword !account)) then let a = (update_ref attempts (!attempts+1)) in raise wrong_pass else if(!attempts >= 5) then let a = (update_ref attempts (!attempts+1)) in raise too_many_failures else match amt with |amt when (amt < 0) -> raise negative_amount |_ -> let a = (update_ref attempts 0) in balance := (!balance + amt)); show_balance = (fun pword -> if(not (String.equal pword !account)) then let a = (update_ref attempts (!attempts+1)) in raise wrong_pass else if(!attempts >= 5) then let a = (update_ref attempts (!attempts+1)) in raise too_many_failures else let a = (update_ref attempts 0) in !balance) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let check (v1 : 'a) (v2 : 'a) = ((compare v1 v2) = 0) in let rec helper (edges: ('a * 'a * weight) list) (vertex: 'a) (acc : ('a * weight) list)= match edges with |[] -> acc |hd::tl -> let (v1, v2, w) = hd in match (v1, v2, w) with |(v1, v2, w) when (check v1 vertex) -> helper tl vertex ((v2,w)::acc) |(_, _, _) -> helper tl vertex acc in helper (g.edges) vertex [] ;; |
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 (initial_pass: passwd) : bank_account = let password = ref initial_pass and account_balance = ref 0 and wrong_pass_cnt = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> if old_pass = !password then (wrong_pass_cnt := 0; password := new_pass) else (wrong_pass_cnt := !wrong_pass_cnt + 1; raise wrong_pass) ); retrieve = (fun (pass_prompt: passwd) (amount: int) -> if !wrong_pass_cnt < 5 then if pass_prompt = !password then if amount >= 0 then if amount <= !account_balance then (wrong_pass_cnt := 0; account_balance := !account_balance - amount) else raise not_enough_balance else raise negative_amount else (wrong_pass_cnt := !wrong_pass_cnt + 1; raise wrong_pass) else raise too_many_failures); deposit= (fun (pass_prompt: passwd) (amount: int) -> if !wrong_pass_cnt < 5 then if pass_prompt = !password then if amount >= 0 then (wrong_pass_cnt := 0; account_balance := !account_balance + amount) else raise negative_amount else (wrong_pass_cnt := !wrong_pass_cnt + 1; raise wrong_pass) else raise too_many_failures); show_balance = (fun (pass_prompt: passwd) -> if !wrong_pass_cnt < 5 then if pass_prompt = !password then (wrong_pass_cnt := 0; !account_balance) else (wrong_pass_cnt := !wrong_pass_cnt + 1; raise wrong_pass) else raise too_many_failures); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun (v1, v2, w) a -> if v1 = vertex then (v2,w)::a else a) 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 city = fst(node) and fcost = snd(node) in if city = b then ([b] , fcost) else if (List.mem city visited) then raise Fail else (let (path , cost) = aux_list (neighbours g city) (city::visited) in (fst(node)::path, cost + fcost)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (city,fcost)::tail -> try aux_node (city,fcost) visited with Fail -> aux_list tail 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 city = fst(node) and fcost = snd(node) in if city = b then sc ([b], fcost) else if (List.mem city visited) then fc () else aux_list (neighbours g city) (city::visited) fc (fun (path, cost) -> sc(city::path, cost + fcost)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | o::d -> aux_node o visited (fun () -> aux_list d 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 min x = match x with | [] -> None | [(o,d)] -> Some (o,d) | (o1,d1)::(o2,d2)::tail -> if d1 >= d2 then min((o1,d1)::tail) else min((o2,d2)::tail) in min(find_all_paths g a b);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass and balance = ref 0 and error_count = ref 0 in { update_pass = (fun old_pass new_pass -> if String.equal old_pass !password then password := new_pass else begin error_count := !error_count+1; raise wrong_pass; end ); deposit = (fun input_password amount -> let _ = if !error_count = 5 then raise too_many_failures else if not (String.equal input_password !password) then begin error_count := !error_count+1; raise wrong_pass; end else error_count := 0 in if amount < 0 then raise negative_amount else balance := !balance+amount ); retrieve = (fun input_password amount -> let _ = if !error_count = 5 then raise too_many_failures else if not (String.equal input_password !password) then begin error_count := !error_count+1; raise wrong_pass; end else error_count := 0 in if amount < 0 then raise negative_amount else if !balance < amount then raise not_enough_balance else balance := !balance-amount ); show_balance = (fun input_password -> if !error_count = 5 then raise too_many_failures else if not (String.equal input_password !password) then begin error_count := !error_count+1; raise wrong_pass; end else begin error_count := 0; !balance; end ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun x (v1,v2,w) -> if vertex = v1 then (v2,w)::x else x) [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let acc:(('a list * weight) ref) = ref ([a], 0) in let remove_last (list,_) = let rec helper list = match list with | [] | _::[] -> [] | x::xs -> x::(helper xs) in helper list in let update_acc (vertex, node_weight) = let {contents = (path, path_weight)} = acc in acc := (path@[vertex], node_weight+path_weight) in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if (fst node) = b then !acc else let not_visited visited edge = let (v2,_) = edge in List.for_all (fun x -> not (String.equal v2 x)) visited in let potential_nodes = List.filter (not_visited visited) (neighbours g (fst node)) in aux_list potential_nodes visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (node, weight)::tail -> try begin update_acc (node,weight); aux_node (node,weight) (node::visited); end with Fail -> begin acc := (remove_last !acc, (snd !acc) - weight); aux_list tail visited; end 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) = if (fst node) = b then sc ([],0) else let not_visited visited edge = let (v2,_) = edge in List.for_all (fun x -> not (String.equal v2 x)) visited in let potential_nodes = List.filter (not_visited visited) (neighbours g (fst node)) in aux_list potential_nodes visited fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (node, weight)::tail -> let fail = (fun () -> aux_list tail visited fc sc; ) and succeed = (fun (list,cost) -> sc (node::list, cost+weight)) in aux_node (node,weight) (node::visited) fail succeed; in aux_list [(a,0)] [a] (fun () -> raise Fail) (fun x -> x);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let max = ref ([],0) in let rec longest_path list = match list with | [] -> if (snd !max = 0) then None else Some !max | x::xs -> if (snd x) > (snd !max) then begin max := ((fst x), (snd x)); longest_path xs end else longest_path xs in longest_path (find_all_paths g a b);; |
let open_account (initial_pass: passwd) : bank_account = let money = ref 0 in let password = ref initial_pass in let attempt_count = ref 0 in { update_pass = (fun (old_pswd: passwd) (new_pswd: passwd) -> if old_pswd = !password then let _ = (attempt_count := 0) in password := new_pswd else let _ = (attempt_count := !attempt_count + 1) in raise wrong_pass ); retrieve = (fun (pswd: passwd) (value: int) -> if !attempt_count >= 5 then raise too_many_failures else if pswd = !password then let _ = attempt_count := 0 in if value < 0 then raise negative_amount else if (!money - value) < 0 then raise not_enough_balance else (money := !money - value) else let _ = (attempt_count := !attempt_count + 1) in raise wrong_pass ); deposit = (fun (pswd: passwd) (value: int) -> if !attempt_count >= 5 then raise too_many_failures else if pswd = !password then let _ = attempt_count := 0 in if value < 0 then raise negative_amount else (money := !money + value) else let _ = (attempt_count := !attempt_count + 1) in raise wrong_pass ); show_balance = (fun (pswd: passwd) -> if !attempt_count >= 5 then raise too_many_failures else if pswd = !password then let _ = attempt_count := 0 in !money else let _ = (attempt_count := !attempt_count + 1) in raise wrong_pass ) } ;; let neighbours_tests: ((string graph * string) * (string * weight) list) list = [ ( ({nodes = ["a";"b";"c";"d";"e"]; edges = [("a","b",1);("b","c",2);("e","a",3);("c","e",4)]}, "a"), [("b",1)] ); ( ({nodes = ["a";"b";"c";"d";"e"]; edges = [("a","b",1);("a","c",2);("a","e",3);("a","d",4)] }, "b"), [] ); ];; let g = { nodes = ["a";"b"]; edges = [("a","b",1)] } ;; let a = "a" ;; let b = "b" ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec filter ((f,s,w): 'a * 'a * weight) = f = vertex in let rec update ((f,s,w): 'a * 'a * weight) : ('a * weight) = (s,w) in List.map update (List.filter filter g.edges);; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec sum (visited: ('a * weight) list) : weight = match visited with | [] -> 0 | x::xs -> (snd x) + sum xs in let get_path (visited: ('a * weight) list): ('a list) = List.rev (List.map fst visited) in let rec aux_node (node: 'a * weight) (visited : ('a * weight) list) : ('a list * weight) = if (fst node) = b then (get_path visited, sum visited) else aux_list (neighbours g (fst node)) visited and aux_list (nodes: ('a * weight) list) (visited: ('a * weight) list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> if List.mem (fst x) (List.map fst visited) then aux_list xs visited else try aux_node x (x::visited) with Fail -> aux_list xs visited in aux_list (neighbours g a) [(a, 0)];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec sum (visited: ('a * weight) list) : weight = match visited with | [] -> 0 | x::xs -> (snd x) + sum xs in let get_path (visited: ('a * weight) list): ('a list) = List.rev (List.map fst visited) in let rec aux_node (node: 'a * weight) (visited : ('a * weight) list) fc sc : ('a list * weight) = if (fst node) = b then (get_path visited, sum visited) else aux_list (neighbours g (fst node)) visited fc sc and aux_list (nodes: ('a * weight) list) (visited: ('a * weight) list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> if List.mem (fst x) (List.map fst visited) then aux_list xs visited fc sc else let fail2 = fun () -> aux_list xs visited fc sc in aux_node x (x::visited) fail2 sc in aux_list (neighbours g a) [(a, 0)] (fun () -> raise Fail) (fun x -> x) let find_new_path (g: 'a graph) (a: 'a) (b: 'a) paths: ('a list * weight) = let rec sum (visited: ('a * weight) list) : weight = match visited with | [] -> 0 | x::xs -> (snd x) + sum xs in let get_path (visited: ('a * weight) list): ('a list) = List.rev (List.map fst visited) in let rec aux_node (node: 'a * weight) (visited : ('a * weight) list) fc sc : ('a list * weight) = if (fst node) = b then let path = (get_path visited, sum visited) in if List.mem path paths then aux_list (neighbours g (fst node)) visited fc sc else path else aux_list (neighbours g (fst node)) visited fc sc and aux_list (nodes: ('a * weight) list) (visited: ('a * weight) list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> if List.mem (fst x) (List.map fst visited) then aux_list xs visited fc sc else let fail2 = fun () -> aux_list xs visited fc sc in aux_node x (x::visited) fail2 sc in aux_list (neighbours g a) [(a, 0)] (fun () -> raise Fail) (fun x -> x);; |
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 | _ -> let sorted = List.sort (fun x y -> if (snd x) > (snd y) then 1 else 0) all_paths in Some (List.hd sorted);; |
let open_account (initial_pass: passwd) : bank_account = let pas = ref initial_pass in let incorrect = ref 0 in let balance = ref 0 in { update_pass = (fun pass now -> if pass = !pas then (pas := now; incorrect := 0) else (incorrect := !incorrect + 1; raise wrong_pass;) ); retrieve = (fun pass n -> if !incorrect < 5 then if pass = !pas then if n <= !balance && n >=0 then (incorrect := 0 ; balance := (!balance - n)) else if n > !balance then (incorrect := 0; raise not_enough_balance) else (incorrect := 0; raise negative_amount) else (incorrect := !incorrect + 1; raise wrong_pass) else raise too_many_failures ) ; deposit = (fun pass d -> if !incorrect >= 5 then raise too_many_failures else if pass <> !pas then (incorrect := !incorrect + 1; raise wrong_pass) else if d < 0 then (incorrect := 0; raise negative_amount ) else balance := (!balance + d); incorrect := 0 ) ; show_balance = (fun pass -> if !incorrect >= 5 then raise too_many_failures else if pass <> !pas then (incorrect := !incorrect + 1; raise wrong_pass) else (incorrect := 0; !balance) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = if g.edges = [] || g.nodes = [] then [] else let f edge acc = let (a,b,w) = edge in if a = vertex then (b,w) :: acc else acc in List.fold_right f 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 (e,w) = node in if e = b then b::visited,(acc+w) else if List.mem e visited || e = a then raise Fail else try aux_list (neighbours g e) (e::visited) (acc+w) with Fail -> raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc: weight): ('a list * weight) = match nodes with | [] -> raise Fail | (a',w) :: t -> if a' = b then (b::visited),acc+w else try aux_node (a',w) visited acc with Fail -> aux_list t visited acc in if a = b then [], 0 else let (x,y) = aux_list (neighbours g a) [a] 0 in List.rev(x),y ;; |
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 (e,w) = node in if e = b then sc ([b],w) else if List.mem e visited then fc () else aux_list (neighbours g e) (e::visited) fc (fun (path, cost) -> sc ( e :: path, cost + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc: ('a list * weight)= match nodes with | [] -> fc () | (a',w) ::t -> aux_node (a',w) visited (fun () -> aux_list t 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 n = find_all_paths g a b in let f (x,y) final= let (ls,b) = List.hd final in if y > b || ( y = b && List.length x > List.length ls) then [(x,y)] else final in match n with | [] -> None | _-> let s = List.fold_right f n [List.hd n] in Some(List.hd s) ;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let failures = ref 0 in let balance = ref 0 in let update_pass cur_pass new_pass : unit = if (cur_pass = !pass) then (pass := new_pass; failures := 0) else (failures := (!failures+1); raise wrong_pass) in let retrieve cur_pass amount : unit = if (!failures >= 5) then raise too_many_failures else ( if (cur_pass = !pass) then (if (amount < 0) then raise negative_amount else (if (amount > !balance) then raise not_enough_balance else (balance := (!balance-amount); failures := 0))) else (failures := (!failures+1); raise wrong_pass ) ) in let deposit cur_pass amount : unit = if (!failures >= 5) then raise too_many_failures else( if (cur_pass = !pass) then (if (amount < 0) then raise negative_amount else (balance := (!balance+amount); failures := 0)) else (failures := (!failures+1); raise wrong_pass ) ) in let show_balance cur_pass = if (!failures >= 5) then raise too_many_failures else ( if (cur_pass = !pass) then (failures := 0; !balance ) else (failures := (!failures+1); raise wrong_pass ) ) in let acc = {update_pass; retrieve; deposit; show_balance} in acc ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let is_neighbour a b = let get_v1 (v1,_,_) = v1 in let get_v2 (_,v2,_) = v2 in let get_w (_,_,w) = w in if (vertex = (get_v1 b)) then ((get_v2 b),(get_w b)) :: a else a in List.fold_left is_neighbour [] 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) = if ((fst node) = b) then (((fst node) :: []),(snd node)) else ( let neigh = List.map (fun f -> (fst f ,((snd f) + (snd node)))) (neighbours g (fst node)) in let path = aux_list neigh visited in (fst node :: fst path, snd path) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |hd :: tl -> (try (let rec in_visited l = match l with |[] -> aux_node hd ((fst hd) :: visited) |hd2 :: tl2 -> (if ((fst hd) = hd2) then raise Fail else in_visited tl2) in in_visited 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) sc fc : ('a list * weight) = if ((fst node) = b) then sc (((fst node) :: []),(snd node)) else ( let neigh = List.map (fun f -> (fst f ,((snd f) + (snd node)))) (neighbours g (fst node)) in aux_list neigh visited (fun f -> sc (fst node :: fst f, snd f)) fc ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) sc fc : ('a list * weight) = match nodes with |[] -> fc () |hd :: tl -> (let rec in_visited l = match l with |[] -> aux_node hd ((fst hd) :: visited) sc (fun () -> aux_list tl visited sc fc) |hd2 :: tl2 -> (if ((fst hd) = hd2) then aux_list tl visited sc fc else in_visited tl2) in in_visited visited ) in aux_node (a,0) (a :: []) (fun a -> a) (fun () -> raise Fail);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec rec_path all_paths acc = match all_paths with |[] -> if (acc = ([],0)) then None else Some acc |hd :: tl -> if ((snd hd) > (snd acc)) then rec_path tl hd else rec_path tl acc in rec_path (find_all_paths g a b) ([],0);; |
let open_account (initial_pass: passwd) : bank_account = let balance= ref 0 in let attempts= ref 0 in let pass= ref initial_pass in { update_pass = (fun (correctpass:passwd ) (new_pass: passwd)-> if (correctpass = !pass) then (attempts := 0; pass := new_pass) else (attempts := !attempts + 1; (raise wrong_pass))); deposit = (fun (correctpass: passwd) (amount: int) -> if !attempts >= 5 then raise too_many_failures else if not(correctpass = !pass) then (attempts := !attempts + 1; (raise wrong_pass)) else if amount < 0 then raise negative_amount else (attempts := 0; balance := !balance + amount)); retrieve = (fun (correctpass: passwd) (amount: int) -> if !attempts >= 5 then raise too_many_failures else if (correctpass = !pass) then (if amount > !balance then raise not_enough_balance else if amount < 0 then raise negative_amount else (attempts := 0; balance := !balance - amount)) else (attempts := !attempts + 1; (raise wrong_pass))); show_balance = (fun ((correctpass: passwd)) -> if !attempts >= 5 then raise too_many_failures else if not(correctpass = !pass) then (attempts := !attempts + 1; (raise wrong_pass)) else (attempts := 0; !balance)) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun (neighbours: ('a * weight) list) (edgenodes: ('a * 'a * weight))-> let (v,v2,w) = edgenodes in if not(v = vertex) then neighbours else (v2,w) :: neighbours) []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 (v2, weight) = node in if v2 = b then (([v2]), ( weight)) else if List.mem v2 visited then raise Fail else let (anode, w)= aux_list(neighbours g v2) (visited @ [v2]) in ((v2 :: anode), (w +weight)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (x,weight)::xs -> try (aux_node (x, weight) 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)= 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 local_pass = ref initial_pass in let local_balance = ref 0 in let local_try = ref 0 in {update_pass = (fun passwd1 passwd2 -> if passwd1 = !local_pass then ( local_pass := passwd2; local_try := 0 ) else ( local_try := !local_try + 1; raise wrong_pass ) ); retrieve = (fun passwd amount -> if !local_try >= 5 then ( local_try := !local_try + 1; raise too_many_failures; ); if amount < 0 then raise negative_amount; if passwd = !local_pass then ( local_try := 0; if amount > !local_balance then raise not_enough_balance else local_balance := !local_balance - amount ) else ( local_try := !local_try + 1; raise wrong_pass ) ) ; deposit = (fun passwd amount -> if !local_try >= 5 then ( local_try := !local_try + 1; raise too_many_failures; ); if amount < 0 then raise negative_amount; if passwd = !local_pass then ( local_try := 0; local_balance := !local_balance + amount ) else ( local_try := !local_try + 1; raise wrong_pass ) ) ; show_balance = (fun passwd -> if !local_try >= 5 then ( local_try := !local_try + 1; raise too_many_failures; ); if passwd = !local_pass then ( local_try := 0; !local_balance ) else ( local_try := !local_try + 1; raise wrong_pass ) ) ; } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let get_result (result: ('a * weight) list) (edge: ('a * 'a * weight)) = let (v1, v2, w) = edge in if v1 = vertex then (v2, w)::result else result in List.fold_left get_result [] 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 (v, w) = node in if List.mem v visited then raise Fail else if v = b then (List.rev (v::(List.rev visited)), w) else let (path, weight) = aux_list (neighbours g v) (List.rev (v::(List.rev visited))) in (path, weight+w) 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 (v, w) = node in if v = b then sc v w else if List.mem v visited then fc () else let (l, weight) = sc v w in let sc2 = fun x y -> (List.rev (x::(List.rev l)), y+weight) in aux_list (neighbours g v) l fc sc2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd :: tl -> let fc2 = fun () -> aux_list tl visited fc sc in aux_node hd visited fc2 sc in aux_node (a,0) [] (fun ()->raise Fail) (fun x y -> (List.rev (x::(List.rev [])), 0)) ;; |
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_max paths max = match paths with | [] -> max | hd :: tl -> let (l,w) = hd in match max with | None -> find_max tl (Some hd) | Some (_,w2) -> if w >= w2 then find_max tl (Some (l,w)) else find_max tl max in find_max all_paths None ;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let counter = ref 0 in let dep = ref 0 in { update_pass = (fun pw new_pw -> if !password = pw then (counter := 0; password := new_pw) else (counter := !counter + 1;raise wrong_pass)); retrieve = (fun pw amount -> if !counter = 5 then raise too_many_failures else if !password = pw then if amount < 0 then raise negative_amount else if amount > !dep then raise not_enough_balance else (counter := 0; dep := !dep - amount) else if !password != pw then (counter := !counter + 1;raise wrong_pass)); deposit = (fun pw amount -> if !counter = 5 then raise too_many_failures else if !password = pw then if amount < 0 then raise negative_amount else (counter := 0; dep := !dep + amount) else if !password != pw then (counter := !counter + 1;raise wrong_pass)); show_balance = (fun pw -> if !counter = 5 then raise too_many_failures else if !password = pw then (counter := 0; !dep) else (counter := !counter + 1;raise wrong_pass)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let extract = fun vertex edge -> match edge with | (a,b,w) when a = vertex -> (b,w) in let rec find = fun vertex edge -> match edge with | (a,b,w) when a = vertex -> true | _ -> false in List.map (extract vertex) (List.filter (find vertex) 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) = if List.mem (fst node) visited then raise Fail else if (fst node) = b then (b::visited, snd node) else let tmp = aux_list (neighbours g (fst node)) ((fst node)::visited) in (fst tmp, snd node + snd tmp) 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 if a = b then raise Fail else let result = aux_list (neighbours g a) [a] in (List.rev (fst result),(snd result)) ;; |
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 List.mem (fst node) visited then fc () else if (fst node) = b then sc (b::visited, snd node) else aux_list (neighbours g (fst node)) ((fst node)::visited) fc (fun nd -> sc (fst nd, snd node + snd nd)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> let fc' = fun () -> aux_list xs visited fc sc in aux_node x visited fc' sc in if a = b then raise Fail else let result = aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun input -> input) in (List.rev (fst result),(snd result)) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let l = find_all_paths g a b in let rec find_longest list max_length count max output = match list with | [] -> Some output | x::xs -> if (snd x) > max_length then (find_longest xs (snd x) (count+1) count x) else (find_longest xs max_length (count+1) max output) in if l = [] then None else let out = List.nth l 0 in find_longest l 0 0 0 out ;; |
let open_account (initial_pass: passwd) : bank_account = let passwd = ref initial_pass in let balance = ref 0 in let counter = ref 0 in { update_pass = (fun old_pass new_pass-> if (old_pass <> !passwd) then counter := !counter + 1; if (old_pass = !passwd) then passwd := new_pass else raise wrong_pass; if (!counter > 0) then counter := 0; ); retrieve = (fun pass money-> if (!counter > 4) then raise too_many_failures else if (pass <> !passwd) then counter := !counter + 1; if (pass = !passwd) then if (money >= 0 ) then if (money <= !balance) then balance := !balance - money else raise not_enough_balance else raise negative_amount else raise wrong_pass; if (money <= !balance) then counter := 0; ); deposit = (fun pass money -> if (!counter > 4) then raise too_many_failures else if (pass <> !passwd) then counter := !counter + 1 ; if (pass = !passwd) then if (money >= 0 ) then balance := !balance + money else raise negative_amount else raise wrong_pass ; if (money >= 0) then counter := 0; ); show_balance = (fun pass -> if (!counter > 4) then raise too_many_failures else if (pass <> !passwd) then counter := !counter + 1 ; if (pass = !passwd) then counter := 0; if (pass = !passwd) then !balance else raise wrong_pass; ); } ;; |
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 (initial_pass: passwd) : bank_account = let passwd = ref initial_pass in let balance = ref 0 in let counter = ref 0 in { update_pass = (fun old_pass new_pass-> if (old_pass <> !passwd) then counter := !counter + 1; if (old_pass = !passwd) then passwd := new_pass else raise wrong_pass; if (!counter > 0) then counter := 0; ); retrieve = (fun pass money-> if (!counter > 4) then raise too_many_failures else if (pass <> !passwd) then counter := !counter + 1; if (pass = !passwd) then if (money >= 0 ) then if (money <= !balance) then balance := !balance - money else raise not_enough_balance else raise negative_amount else raise wrong_pass; if (money <= !balance) then counter := 0; ); deposit = (fun pass money -> if (!counter > 4) then raise too_many_failures else if (pass <> !passwd) then counter := !counter + 1 ; if (pass = !passwd) then if (money >= 0 ) then balance := !balance + money else raise negative_amount else raise wrong_pass ; if (money >= 0) then counter := 0; ); show_balance = (fun pass -> if (!counter > 4) then raise too_many_failures else if (pass <> !passwd) then counter := !counter + 1 ; if (pass = !passwd) then counter := 0; if (pass = !passwd) then !balance else raise wrong_pass; ); } ;; let helper1 edge node: ('a * 'a) list = match edge with |(a,b,c) -> if (a = node) then [(b,c)] else [] let helper2 output list = output @ list;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let fun1 x = match x with |(a,b,c) -> if (a = vertex) then true else false in let fun2 x = match x with |(a,b,c) -> (b,c) in List.map fun2 (List.filter (fun1) (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) = 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 (initial_pass: passwd) : bank_account = let balance = ref 0 in let wrong = ref 0 in let pass= ref initial_pass in {update_pass = (fun old neww-> if !pass=old then (let x= (pass:=neww) in wrong:=0) else (let x=(wrong :=!wrong + 1) in raise wrong_pass)); deposit = (fun pw amount -> if (!wrong >= 5) then raise too_many_failures else ( if !pass=pw then let x=wrong:=0 in (if amount<0 then raise negative_amount else balance := !balance + amount) else (let x=(wrong :=!wrong + 1) in raise wrong_pass))); retrieve= (fun pw amount -> if (!wrong >= 5) then raise too_many_failures else (if !pass=pw then let x=wrong:=0 in (if amount<0 then raise negative_amount else (if (!balance - amount >= 0) then balance := !balance - amount else raise not_enough_balance)) else (let x=(wrong :=!wrong + 1) in raise wrong_pass))); show_balance= (fun (pw) -> if (!wrong >= 5) then raise too_many_failures else (if !pass=pw then let x=wrong:=0 in !balance else (let x=(wrong :=!wrong + 1) in raise wrong_pass))); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec aux_node l acc (w: weight) v = match l with |[]-> acc |(v1,v2,w1) :: xs -> if v1=v then aux_node xs (List.append acc [v2, w1]) (w+w1) v else aux_node xs acc w v in aux_node g.edges [] 0 vertex;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let destinations = neighbours g in let rec aux_list (nodes: ('a * weight) list) (visited: 'a list) (w: weight) : ('a list * weight) = match nodes with |[]-> raise Fail |(v,w1) ::xs ->let x=(v,w1) in if v=b then (List.append visited [b]), (w+w1) else (if (List.mem v visited) then try (aux_list xs (visited) (w)) with Fail-> raise Fail else try aux_list (destinations v) (List.append visited [v]) (w+w1) with Fail -> aux_list xs (visited) (w)) in aux_list (destinations a) [a] 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let destinations = neighbours g in let rec aux_list (nodes: ('a * weight) list) (visited: 'a list) (w: weight) fc sc : ('a list * weight) = match nodes with |[]-> fc () |(v,w1) ::xs -> if v=b then sc (visited, b, w, w1) else if (List.mem v visited) then aux_list xs (visited) (w) fc sc else let sc2=sc in let fc2= fun () -> (aux_list (neighbours g v) (List.append visited [v]) (w+w1) fc sc) in aux_list xs (visited) (w) fc2 sc2 in aux_list (destinations a) [a] 0 (fun () -> raise Fail) (fun (visited, b, w, w1)->(List.append visited [b]), (w+w1));; |
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 f l max_path = match l with |[]->max_path |(l1, w1) :: xs-> let (l2,w2)=max_path in if w1>w2 then f xs (l1,w1) else f xs max_path in Some (f l ([], 0));; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let attempts = ref 0 in let balance = ref 0 in let update_pass (old_pass: passwd) (new_pass: passwd) = if (old_pass = !password) then (password := new_pass; attempts := 0) else (attempts := !attempts + 1; (raise wrong_pass)) in let retrieve (pass: passwd) amount = if(!attempts >= 5) then (raise too_many_failures) else if (pass = !password) then (attempts := 0; if (amount < 0) then (raise negative_amount) else if ((!balance - amount) < 0) then (raise not_enough_balance) else balance := !balance - amount ) else (attempts := !attempts + 1; (raise wrong_pass)) in let deposit (pass: passwd) amount = if(!attempts >= 5) then (raise too_many_failures) else if (pass = !password) then (attempts := 0; if (amount < 0) then (raise negative_amount) else balance := !balance + amount; ) else (attempts := !attempts + 1; (raise wrong_pass)) in let show_balance (pass: passwd) = if(!attempts >= 5) then (raise too_many_failures) else if (pass = !password) then (attempts := 0; !balance) else (attempts := !attempts + 1; (raise wrong_pass)) in {update_pass; retrieve; deposit; show_balance} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let findNeigh a (v0, v1, weight) = if v0 = vertex then (v1, weight)::a else a in List.fold_left findNeigh [] 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, weight) = node in if n = b then (visited@[b], weight) else if List.exists (fun a -> n = a) visited then raise Fail else let update_weight (x, x_w) = (x, x_w + weight) in let cur_neigh = List.map update_weight (neighbours g n) in aux_list cur_neigh (visited@[n]) 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, weight) = node in if n = b then sc ([b], weight) else if List.exists (fun a -> n = a) visited then fc () else let cur_neigh = (neighbours g n) in aux_list cur_neigh (n::visited) fc (fun (x, y) -> sc (n::x, y+weight)) 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 (n, weight) -> (n, weight));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match (find_all_paths g a b) with | [] -> None | hd::tl -> Some (List.fold_right (fun ((_,weight1) as first) ((_,weight2) as second) -> if weight1 > weight2 then first else second) tl hd);; |
let open_account (initial_pass: passwd) : bank_account = let attempt = ref 0 in let password = ref initial_pass in let current_balance = ref 0 in { update_pass = (fun cur_pass new_pass -> if cur_pass = !password then (attempt := 0; password := new_pass) else (attempt := !attempt + 1; raise wrong_pass)) ; retrieve = (fun cur_pass x -> if !attempt >= 5 then raise too_many_failures else if cur_pass = !password then if x < 0 then (attempt := 0; raise negative_amount) else if x > !current_balance then (attempt := 0; raise not_enough_balance) else (attempt := 0; current_balance := !current_balance - x) else (attempt := !attempt + 1; raise wrong_pass)) ; deposit = (fun cur_pass x -> if !attempt >= 5 then raise too_many_failures else if cur_pass = !password then if x < 0 then (attempt := 0; raise negative_amount) else (attempt := 0; current_balance := !current_balance + x) else (attempt := !attempt + 1; raise wrong_pass)) ; show_balance = (fun cur_pass -> if !attempt >= 5 then raise too_many_failures else if cur_pass = !password then (attempt := 0; !current_balance) else (attempt := !attempt + 1; raise wrong_pass)) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec get_neighbours edges vertex acc = match edges with | [] -> acc | (v1, v2, w) :: tl -> if v1 = vertex then get_neighbours tl vertex ((v2, w) :: acc) else get_neighbours tl vertex acc in get_neighbours g.edges vertex [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) tw : ('a list * weight) = if List.mem (fst node) (visited) then raise Fail else if (fst node) = b then (List.rev (fst node :: visited), snd node + tw) else aux_list (neighbours g (fst node)) ((fst node) :: visited) (snd node + tw) and aux_list (nodes: ('a * weight) list) (visited: 'a list) tw : ('a list * weight) = match nodes with | [] -> raise Fail | hd :: tl -> try aux_node hd visited tw with Fail -> aux_list tl visited tw 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 tw : ('a list * weight)= if List.mem (fst node) (visited) then fc () else if (fst node) = b then (List.rev (fst node :: visited), snd node + tw) else aux_list (neighbours g (fst node)) ((fst node) :: visited) fc (snd node + tw) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc tw : ('a list * weight) = match nodes with | [] -> fc () | hd :: tl -> let fc2 = fun () -> aux_list tl visited fc tw in aux_node hd visited fc2 tw in aux_node (a, 0) [] (fun () -> raise Fail) 0;; |
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 maximum = let rec get_max acc list = match list with | [] -> acc | (p, w) :: tl -> get_max (max acc w) tl in get_max 0 all_paths in let result = let rec get_longest (list: ('a list * weight) list) = match list with | [] -> None | (p, w) :: tl -> if w = maximum then Some (p, w) else get_longest tl in get_longest all_paths in result;; |
let open_account (initial_pass: passwd) : bank_account = let pswd = ref initial_pass in let bal = ref 0 in let attempts = ref 0 in { update_pass = (fun old_pass new_pass -> if String.equal old_pass !pswd then let _ = attempts := 0 in pswd := new_pass else let _ = attempts := !attempts + 1 in raise wrong_pass); retrieve = (fun pass amount -> if !attempts >= 5 then raise too_many_failures else let new_amount = if !bal - amount < 0 then raise not_enough_balance else if amount < 0 then raise negative_amount else !bal - amount in if String.equal pass !pswd then let _ = attempts := 0 in bal := new_amount else let _ = attempts := !attempts + 1 in raise wrong_pass); deposit = (fun pass amount -> if !attempts >= 5 then raise too_many_failures else let new_amount = if amount < 0 then raise negative_amount else !bal + amount in if String.equal pass !pswd then let _ = attempts := 0 in bal := new_amount else let _ = attempts := !attempts + 1 in raise wrong_pass); show_balance = (fun pass -> if !attempts >= 5 then raise too_many_failures else if String.equal pass !pswd then let _ = attempts := 0 in !bal else let _ = attempts := !attempts + 1 in raise wrong_pass); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun (a,b,c) -> fun ls -> if a = vertex then (b,c) :: ls else ls) 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) = match node with | (n, w) when n = b -> ([b], w) | (n, w) when List.exists ((=) n) visited -> raise Fail | (n, w) -> (try let (path, wt) = aux_list (neighbours g n) (n :: visited) in (n::path, w + wt) 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 -> match nodes with | [] -> raise 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)= match node with | (n, w) when n = b -> sc ([b], w) | (n, w) when List.exists ((=) n) visited -> fc () | (n, w) -> let success = (fun (ls, wt) -> sc (n :: ls, w + wt)) in aux_list (neighbours g n) (n :: visited) fc success and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd :: tl -> let fail = (fun () -> aux_list tl visited fc sc) in aux_node hd visited fail 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 = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let lock = ref false in let failureCount = ref 0 in let currentBalance = ref 0 in let currentPassword = ref initial_pass in { update_pass = (fun old_pass new_pass -> if old_pass <> !currentPassword then (incr failureCount ; raise wrong_pass ) else (failureCount:=0 ; lock:= false ;currentPassword := new_pass)); retrieve = (fun pass rAmount-> if (pass <> !currentPassword)then ( if !failureCount >= 5 then ( lock:= true; raise too_many_failures) else (incr failureCount ; raise wrong_pass ) ) else ( if rAmount < 0 then raise negative_amount else (if (rAmount > !currentBalance )then raise not_enough_balance else (if !lock then raise too_many_failures else (failureCount:=0;currentBalance:=(!currentBalance-rAmount)))))); deposit = (fun pass dAmount-> if pass <> !currentPassword then ( if !failureCount >= 5 then ( lock:= true; raise too_many_failures) else (incr failureCount ; raise wrong_pass ) ) else (if dAmount<0 then raise negative_amount else (if !lock then raise too_many_failures else( failureCount:=0;currentBalance:=(!currentBalance+dAmount))))); show_balance = (fun pass -> if pass <> !currentPassword then ( if !failureCount >= 5 then ( lock:= true; raise too_many_failures) else (incr failureCount ; raise wrong_pass ) ) else( if !lock then raise too_many_failures else (failureCount:=0;!currentBalance))) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let funct = (fun l edge-> (let (a,b,c)=edge in (if (vertex = a) then( (b,c)::l) else l))) in List.fold_left funct [] 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) (sum : int): ('a list * weight) = let (z,t) = node in if z = b then (List.rev_append visited [z], t + sum) else( if List.mem z visited then raise Fail else aux_list (neighbours g z) (z::visited) (sum +t) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (sum : int): ('a list * weight) = match nodes with | []->raise Fail | (i,j) :: list ->try aux_node (i,j) visited sum with Fail -> aux_list list visited sum in aux_list (neighbours g 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) fc sc : ('a list * weight)= let (z,t) = node in if z = b then sc ([z],t) else (if List.mem z visited then fc () else aux_list (neighbours g z) (z :: visited) fc (fun(z2,t2)-> sc (z:: z2,t2+t))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () | (i,j):: tl ->aux_node (i,j) visited (fun () -> aux_list tl visited fc sc) sc in let f1= fun () -> raise Fail in let f2= fun (z2,t2)->(a::z2,t2) in aux_list (neighbours g a) [a] f1 f2;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let find list = match list with |[] -> None | hd :: tl -> Some hd in let listOfAllPaths = find_all_paths g a b in let f = fun(z,t)(z2,t2)->compare t t2 in let sorted = List.sort f listOfAllPaths in find (List.rev sorted);; |
let open_account (initial_pass: passwd) : bank_account = let amount = ref 0 in let pass = ref initial_pass in let time_fail = ref 0 in let if_lock = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> if (old_pass = !pass) then begin pass := new_pass; time_fail := 0; if_lock := 0 end else begin time_fail := !time_fail +1; if (!time_fail >= 5) then (if_lock := 1); raise (wrong_pass) end); retrieve = (fun (the_pass: passwd) (re_amount : int) -> if (!if_lock = 1 ) then raise too_many_failures; if (!pass = the_pass) then if (re_amount < 0) then raise (negative_amount) else if (re_amount <= !amount) then (amount := !amount - re_amount; time_fail := 0) else raise (not_enough_balance) else begin time_fail := !time_fail +1; if (!time_fail >= 5) then (if_lock := 1); raise (wrong_pass) end ); deposit = (fun (the_pass: passwd) (de_amount : int) -> if (!if_lock = 1 ) then raise too_many_failures; if (!pass = the_pass) then if (de_amount < 0) then raise (negative_amount) else (amount := !amount + de_amount; time_fail := 0) else begin time_fail := !time_fail +1; if (!time_fail >= 5) then (if_lock := 1); raise (wrong_pass) end); show_balance = (fun (the_pass: passwd) -> if (!if_lock = 1 ) then raise too_many_failures; if (!pass=the_pass) then (time_fail := 0; !amount) else begin time_fail := !time_fail +1; if (!time_fail >= 5) then (if_lock := 1); raise (wrong_pass) end); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f (acc) (b:('a * 'a * weight)) = match b with | (b1,b2,bw) when b1 = vertex -> ([(b2,bw)] @acc) | _ -> acc 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) (visited : 'a list) : ('a list * weight) = if fst(node) = b then ([fst(node)],snd(node)) else if List.mem (fst(node)) visited then raise Fail else let (p,w ) = aux_list (neighbours (g) (fst(node))) (fst(node)::visited) in (fst(node)::p,snd(node)+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)= let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if fst(node) = b then sc ([fst(node)],snd(node)) else if List.mem (fst(node)) visited then fc () else aux_list (neighbours g (fst(node))) (fst(node)::visited) fc (fun (p, w) -> sc (fst(node)::p,snd(node)+w) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc = match nodes with | [] -> fc () | (node::rest) -> aux_node node visited (fun _ -> aux_list rest visited fc sc) sc in aux_node (a,0) [] (fun () -> raise Fail) (fun res -> res);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let the_max l1 l2 = if (List.length (fst(l1)) >= List.length (fst(l2))) then l1 else l2 in let rec find_max list: ('a list * weight) option = match list with |[] -> None |hd::tl -> begin match find_max tl with |None -> Some hd |Some m -> Some (the_max hd m) end in find_max (find_all_paths g a b);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let incorrect_attempts = ref 0 in let account_blocked = ref false in let check_pass (pass: passwd) f is_update_pass = if pass = !password && not (!account_blocked) then ( incorrect_attempts := 0; f ) else ( if (!incorrect_attempts = 4) && !account_blocked then ( if is_update_pass then raise wrong_pass else raise too_many_failures ) else ( if !incorrect_attempts = 4 then( account_blocked := true; raise wrong_pass; ) else ( incorrect_attempts := !incorrect_attempts + 1; raise wrong_pass; ) ) ) in let update_pass (old_pass: passwd) (new_pass: passwd) = if old_pass = !password then ( password := new_pass; account_blocked := false; incorrect_attempts := 0; ) else check_pass old_pass () true in let deposit (pass: passwd) (amount: int) = let f = if amount < 0 then raise negative_amount else balance := !balance + amount; in check_pass pass f false in let retrieve (pass: passwd) (amount: int) = let f = if amount < 0 then raise negative_amount else ( if amount > !balance then raise not_enough_balance else balance := !balance - amount; ) in check_pass pass f false in let show_balance (pass: passwd) = let f = !balance in check_pass pass f false in { update_pass = update_pass ; retrieve = retrieve; deposit = deposit ; show_balance = show_balance } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f a el = let (from_, to_, weight_) = el in (to_, weight_) :: a in let filter el = let (from_, to_, weight_) = el in from_ = vertex in let edges = List.filter filter g.edges in List.fold_left f [] edges ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.