text
stringlengths
0
601k
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f = fun (v1, v2, i) l -> if v1 = vertex then (v2, i) :: l else l 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) : ('a list * weight) = let f w (a, weight) = (a, weight + w) in let (n, w) = node in if List.mem n visited then raise Fail else match (n, w) with | (x1, _) when b = n -> (List.rev (n :: visited), w) | _ -> try aux_list (List.map (f w) (neighbours g n)) (n::visited) with Fail -> raise Fail and aux_list (nodes : ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (t,w) :: xs -> try aux_node (t, w) (visited) with Fail -> aux_list xs visited in aux_list (neighbours g a) [a];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let f w (a, weight) = (a, weight + w) in let (n, w) = node in if List.mem n visited then fc () else match (n, w) with | (x1, _) when b = n -> (List.rev (n :: visited), w) | _ -> sc (List.map (f w) (neighbours g n)) (n::visited) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> raise Fail | (t,w) :: xs -> aux_node (t, w) (visited) (fun () -> aux_list xs visited fc sc) (fun neighbors visited -> aux_list neighbors visited fc sc) in aux_list (neighbours g a) [a] (fun () -> Fail) (fun r -> r);;
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 (l_max, m) = get_max_path l in if m = (-1) then None else Some (l_max, m);;
let open_account (initial_pass: passwd) : bank_account = let account_password = ref initial_pass in let num_auth_failures = ref 0 in let balance = ref 0 in let update_pass (acct_p : passwd) (new_p : passwd) = if (acct_p = !account_password) then begin account_password := new_p ; if (!num_auth_failures >= 5) then num_auth_failures := 0 end else begin num_auth_failures := !num_auth_failures + 1; raise wrong_pass end in let retrieve (acct_p : passwd) (amount : int) = if (!num_auth_failures >= 5) then begin num_auth_failures := !num_auth_failures + 1; raise too_many_failures end else ( if (acct_p = !account_password) then if (amount < 0) then begin num_auth_failures := 0 ; raise negative_amount end else (if (!balance < amount) then begin num_auth_failures := 0 ; raise not_enough_balance end else begin num_auth_failures := 0 ; balance := !balance - amount; end) else begin num_auth_failures := !num_auth_failures + 1; raise wrong_pass end) in let deposit (acct_p : passwd) (amount : int) = if (!num_auth_failures >= 5) then begin num_auth_failures := !num_auth_failures + 1; raise too_many_failures end else ( if (acct_p = !account_password) then if (amount < 0) then begin num_auth_failures := 0 ; raise negative_amount end else begin num_auth_failures := 0 ; balance := !balance + amount end else begin num_auth_failures := !num_auth_failures + 1; raise wrong_pass end) in let show_balance (acct_p : passwd) = if (!num_auth_failures >= 5) then begin num_auth_failures := !num_auth_failures + 1; raise too_many_failures end else ( if (acct_p = !account_password) then begin num_auth_failures := 0 ; !balance end else begin num_auth_failures := !num_auth_failures + 1; raise wrong_pass end) in {update_pass = update_pass; retrieve = retrieve; deposit = deposit; show_balance = show_balance } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edge_list = g.edges in let neighbors_list = [] in let rec compile_neighbor_list edges acc = match edges with | [] -> acc | edge::t -> let (v,out,w) = edge in if (v = vertex) then compile_neighbor_list t ((out, w)::acc) else compile_neighbor_list t acc in compile_neighbor_list edge_list neighbors_list ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec in_visited (node: 'a) (visited: 'a list) : bool = List.exists (fun x -> (x = node)) visited in let rec remove_n (n: 'a) (w: int) (acc : ('a list * weight)) (new_list : ('a list)) = let (l,tw) = acc in match l with | [] -> (new_list, tw) | x::t -> if (x = n) then remove_n n w (t, tw-w) new_list else let new_list = x::new_list in remove_n n w (t, tw) new_list in let rec aux_node (node: 'a * weight) (visited : 'a list) (start : 'a) (dest : 'a) (acc : ('a list * weight)): ('a list * weight) = let (n,w) = node in let new_visited = List.append [n] visited in if (n = dest) then let (l,tw) = acc in (n::l, tw+w) else let n_neighbors = neighbours g n in let (l,tw) = acc in let acc = (n::l, tw+w) in match n_neighbors with | [] -> raise Fail | _ -> aux_list n_neighbors new_visited start dest acc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (start : 'a) (dest : 'a) (acc : ('a list * weight)) : ('a list * weight) = match nodes with | [] -> raise Fail | ((n,w)::t) -> if (in_visited n visited) then aux_list t visited start dest acc else try aux_node (n,w) visited start dest acc with Fail -> begin let acc = remove_n n w acc [] in aux_list t visited start dest acc end in if (a = b) then ([a],0) else let a_neighbors = neighbours g a in if (a_neighbors = []) then raise Fail else let first_node = List.nth a_neighbors 0 in let (path_list,w) = aux_node first_node [a] a b ([a], 0) in let (x::t) = path_list in if (x <> a) then begin let (new_list, w3) = remove_n a 0 (path_list, w) [] in let new_path_list = List.append [a] new_list in (new_path_list, w3) end else (path_list, w) ;;
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 account_password = ref initial_pass in let num_auth_failures = ref 0 in let balance = ref 0 in let update_pass (acct_p : passwd) (new_p : passwd) = if (acct_p = !account_password) then begin account_password := new_p ; if (!num_auth_failures >= 5) then num_auth_failures := 0 end else begin num_auth_failures := !num_auth_failures + 1; raise wrong_pass end in let retrieve (acct_p : passwd) (amount : int) = if (!num_auth_failures >= 5) then begin num_auth_failures := !num_auth_failures + 1; raise too_many_failures end else ( if (acct_p = !account_password) then if (amount < 0) then begin num_auth_failures := 0 ; raise negative_amount end else (if (!balance < amount) then begin num_auth_failures := 0 ; raise not_enough_balance end else begin num_auth_failures := 0 ; balance := !balance - amount; end) else begin num_auth_failures := !num_auth_failures + 1; raise wrong_pass end) in let deposit (acct_p : passwd) (amount : int) = if (!num_auth_failures >= 5) then begin num_auth_failures := !num_auth_failures + 1; raise too_many_failures end else ( if (acct_p = !account_password) then if (amount < 0) then begin num_auth_failures := 0 ; raise negative_amount end else begin num_auth_failures := 0 ; balance := !balance + amount end else begin num_auth_failures := !num_auth_failures + 1; raise wrong_pass end) in let show_balance (acct_p : passwd) = if (!num_auth_failures >= 5) then begin num_auth_failures := !num_auth_failures + 1; raise too_many_failures end else ( if (acct_p = !account_password) then begin num_auth_failures := 0 ; !balance end else begin num_auth_failures := !num_auth_failures + 1; raise wrong_pass end) in {update_pass = update_pass; retrieve = retrieve; deposit = deposit; show_balance = show_balance } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edge_list = g.edges in let neighbors_list = [] in let rec compile_neighbor_list edges acc = match edges with | [] -> acc | edge::t -> let (v,out,w) = edge in if (v = vertex) then compile_neighbor_list t ((out, w)::acc) else compile_neighbor_list t acc in compile_neighbor_list edge_list neighbors_list ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec in_visited (node: 'a) (visited: 'a list) : bool = List.exists (fun x -> (x = node)) visited in let rec remove_n (n: 'a) (w: int) (acc : ('a list * weight)) (new_list : ('a list)) = let (l,tw) = acc in match l with | [] -> (new_list, tw) | x::t -> if (x = n) then remove_n n w (t, tw-w) new_list else let new_list = x::new_list in remove_n n w (t, tw) new_list in let rec aux_node (node: 'a * weight) (visited : 'a list) (start : 'a) (dest : 'a) (acc : ('a list * weight)): ('a list * weight) = let (n,w) = node in let new_visited = List.append [n] visited in if (n = dest) then let (l,tw) = acc in (n::l, tw+w) else let n_neighbors = neighbours g n in let (l,tw) = acc in let acc = (n::l, tw+w) in match n_neighbors with | [] -> raise Fail | _ -> aux_list n_neighbors new_visited start dest acc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (start : 'a) (dest : 'a) (acc : ('a list * weight)) : ('a list * weight) = match nodes with | [] -> raise Fail | ((n,w)::t) -> if (in_visited n visited) then aux_list t visited start dest acc else try aux_node (n,w) visited start dest acc with Fail -> begin let acc = remove_n n w acc [] in aux_list t visited start dest acc end in if (a = b) then ([a],0) else let a_neighbors = neighbours g a in if (a_neighbors = []) then raise Fail else let first_node = List.nth a_neighbors 0 in let (path_list,w) = aux_node first_node [a] a b ([a], 0) in let (x::t) = path_list in if (x <> a) then begin let (new_list, w3) = remove_n a 0 (path_list, w) [] in let new_path_list = List.append [a] new_list in (new_path_list, w3) end else (path_list, w) ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec in_visited (node: 'a) (visited: 'a list) : bool = List.exists (fun x -> (x = node)) visited in 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 = let all_paths = find_all_paths g a b in let rec find_longest (list : ('a list * weight) list) (cur_longest: int) (cur_longest_index : int) (cur_index : int) = match list with | [] -> cur_longest_index | (nodes, w)::t -> if ((List.length nodes) > cur_longest) then let new_cur_longest = (List.length nodes) in let new_cur_longest_index = cur_index in let new_index = cur_index + 1 in find_longest t new_cur_longest new_cur_longest_index new_index else let new_index = cur_index + 1 in find_longest t cur_longest cur_longest_index new_index in if (all_paths = []) then None else let longest_index = find_longest all_paths 0 0 0 in let longest = List.nth all_paths longest_index in let some_longest = Some longest in some_longest;;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let attemps = ref 0 in let balance = ref 0 in let security p f = if !attemps >= 5 then raise too_many_failures else (if p = !pass then (attemps := 0; f ()) else (attemps := !attemps+1 ; raise wrong_pass)) in let update_pass prev current = if prev <> !pass then (attemps := !attemps+1 ; raise wrong_pass) else (attemps := 0 ; pass := current) in let deposit p money = let f () = if money < 0 then raise negative_amount else balance := !balance + money in security p f in let retrieve p money = let f () = if money < 0 then raise negative_amount else let temp = !balance - money in match temp with | t when t < 0 -> raise not_enough_balance | _ -> balance := !balance - money in security p f in let show_balance p = let _ = security p (fun () -> ()) in !balance in {update_pass; retrieve; deposit; show_balance};;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let elist = g.edges in let f (init: ('a *weight) list) (edge: ('a * 'a * weight)) : ('a *weight) list = let (v1, v2, w) = edge in match v1 with | v when v=vertex -> (v2, w) :: init | _ -> init in List.fold_left f [] elist;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let nb = neighbours g a in let rec aux_node (node: 'a * weight) (visited : 'a list) (weight: int): ('a list * weight) = let (v, w) = node in if (List.mem v visited) then raise Fail else if v=b then (visited @ [v], weight + w) else aux_list (neighbours g v) (visited @ [v]) (weight+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (weight : int) : ('a list * weight) = match nodes with | [] -> raise Fail | node :: l -> try aux_node node visited weight with Fail -> aux_list l visited weight in aux_list nb [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 List.mem v visited then fc () else if v=b then sc (visited@[v]) w else aux_list (neighbours g v) (visited @ [v]) fc (fun l' w' -> sc l' (w+w')) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | node :: l -> aux_node node visited (fun () -> aux_list l visited fc sc) sc in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun l w -> (l, w));;
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 f p1 p2 = let (l1, w1) = p1 and (l2, w2) = p2 in if w1 < w2 then p2 else p1 in match all_paths with | [] -> None | path :: pl -> Some (List.fold_left f path pl);;
let open_account (initial_pass: passwd) : bank_account = let incorrect_count = ref 0 in let cur_pass = ref initial_pass in let balance = ref 0 in let pass_validator (pass: passwd): bool = if !incorrect_count >= 5 then raise too_many_failures else if pass = !cur_pass then let _ = incorrect_count := 0 in true else let _ = incorrect_count := !incorrect_count + 1 in raise wrong_pass in let update_pass (old_pass: passwd) (new_pass: passwd) = if old_pass = !cur_pass then let _ = cur_pass := new_pass in incorrect_count := 0 else let _ = incorrect_count := !incorrect_count + 1 in raise wrong_pass in let retrieve (pass: passwd) (num: int) = if pass_validator pass then if num < 0 then raise negative_amount else if num > !balance then raise not_enough_balance else balance := !balance - num in let deposit (pass: passwd) (num: int) = if pass_validator pass then if num < 0 then raise negative_amount else balance := !balance + num in let show_balance (pass: passwd): int = let _ = pass_validator pass in !balance in {update_pass; retrieve; deposit; show_balance} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let filter (u, _, _) = (u = vertex) in let filtered = List.filter filter g.edges in List.map (fun (_, v, w) -> (v, w)) filtered;;
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 let path, w2 = aux_list (neighbours g a) (a :: visited) in (a :: path, w + w2) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | n :: tl -> let a, _ = n in if List.mem a visited then aux_list tl visited else let list1, w1 = aux_node n visited in if List.hd (List.rev list1) = b then (list1, w1) else aux_list tl (a :: visited) | _ -> ([], 0) in let res_list, res_w = aux_node (a, 0) [] in if res_w = 0 then raise Fail else (res_list, res_w);;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec do_all f lst = match lst with | [] -> ([], 0) | x :: xs -> let res = f x in if res = ([], 0) then do_all f xs else res in let rec dfs (node: 'a * weight) (visited: 'a list) sc : ('a list * weight) = let u, _ = node in if u = b then sc ([], 0) else let f elem = let v, w2 = elem in if List.mem v visited then ([], 0) else let sc2 = fun (r: (('a list) * weight)) -> let p, w3 = r in sc (v :: p, w2 + w3) in dfs elem (v :: visited) sc2 in do_all f (neighbours g u) in let suc r = let p, w = r in (a :: p, w) in let res = dfs (a, 0) [a] suc in if res = ([], 0) then raise Fail else res;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let max u acc = let p1, w1 = acc in let p2, w2 = u in if w2 > w1 then (p2, w2) else (p1, w1) in let paths = find_all_paths g a b in match paths with | [] -> None | _ -> Some (List.fold_left (max) ([], 0) paths );;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let attempts = ref 5 in let balance = ref 0 in let update_pass (old_passwd: passwd) (new_passwd: passwd) : unit = if old_passwd = !password then let temp = password := new_passwd in attempts := 5 else let temp = attempts := !attempts - 1 in raise (wrong_pass) in let retrieve (pass: passwd) (amount: int) : unit = if !attempts <= 0 then raise (too_many_failures) else if pass = !password then let temp = attempts := 5 in if amount < 0 then raise (negative_amount) else if !balance < amount then raise (not_enough_balance) else balance := !balance - amount else let temp = attempts := !attempts - 1 in raise (wrong_pass) in let deposit (pass: passwd) (amount: int) : unit = if !attempts <= 0 then raise (too_many_failures) else if pass = !password then let temp = attempts := 5 in if amount < 0 then raise (negative_amount) else balance := !balance + amount else let temp = attempts := !attempts - 1 in raise (wrong_pass) in let show_balance (pass: passwd) : int = if !attempts <= 0 then raise (too_many_failures) else if pass = !password then let temp = attempts := 5 in !balance else let temp = attempts := !attempts - 1 in raise (wrong_pass) in {update_pass; retrieve; deposit; show_balance} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec neighbour_helper (edges: ('a * 'a * weight) list) (vertex: 'a) (acc: ('a * weight) list) : ('a * weight) list = match edges with | [] -> acc | edge::rest -> let (a, b, c) = edge in if a = vertex then neighbour_helper rest vertex ((b, c)::acc) else neighbour_helper rest vertex acc in neighbour_helper g.edges vertex [] ;; let prune_visited (neighbors : ('a * weight) list) (visited : 'a list) : ('a * weight) list = let rec helper (neighbors : ('a * weight) list) (visited : 'a list) (newNeighbors: ('a * weight) list) : ('a * weight) list = match neighbors with | [] -> newNeighbors | neighbor::rest -> let (_val, _weight) = neighbor in if List.mem _val visited then helper rest visited newNeighbors else helper rest visited (neighbor::newNeighbors) in helper neighbors visited [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let pathWeight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (_val, _weight) = node in let temp = pathWeight := !pathWeight + _weight in if _val = b then (_val::visited, !pathWeight) else let pruned_visitors = prune_visited (neighbours g _val) visited in aux_list pruned_visitors (_val::visited) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | edge::rest -> let curWeight = !pathWeight in let (_val, _weight) = edge in try aux_node (_val, _weight) (visited) with | Fail -> let temp = pathWeight := curWeight in (aux_list rest (visited)) in let (_nodes, _weight) = aux_node (a, 0) [] in (List.rev _nodes, _weight) ;;
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 (_val, _weight) = node in if _val = b then sc () else let pruned_visitors = prune_visited (neighbours g _val) visited in aux_list pruned_visitors (visited) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | edge::rest -> let (_val, _weight) = edge in let fc = (fun () -> aux_list rest (_val::visited) fc sc) in let sc = (fun () -> let (_visited, _totalWeight) = sc () in (_val::_visited, _totalWeight + _weight)) in aux_node edge (_val::visited) fc sc in let (_nodes, _weight) = aux_node (a, 0) (a::[]) (fun () -> raise Fail) (fun () -> (a::[], 0)) in (List.rev _nodes, _weight) ;; let pruned_prev_neighbours (g: 'a graph) (vertex: 'a) (visited: 'a list) : 'a list = let rec neighbour_helper (edges: ('a * 'a * weight) list) (vertex: 'a) (acc: 'a list) : 'a list = match edges with | [] -> acc | edge::rest -> let (a, b, c) = edge in if b = vertex && not (List.mem a visited) then neighbour_helper rest vertex (a::acc) else neighbour_helper rest vertex acc in neighbour_helper g.edges vertex [] ;; let rec find_edge_weight (edges: ('a * 'a * weight) list) (a: 'a) (b: 'b) : weight = match edges with | [] -> raise Fail | edge::rest -> let (src, dst, value) = edge in if src = a && dst = b then value else find_edge_weight rest a b ;; let rec calc_path_weight (g: 'a graph) (nodes: 'a list) : weight = match nodes with | node1::node2::rest -> (find_edge_weight g.edges node1 node2) + (calc_path_weight g (node2::rest)) | _ -> 0 ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec return_longest_path (paths : ('a list * weight) list) (longest: ('a list * weight) option) : ('a list * weight) option = match paths with | [] -> longest | path::rest -> match longest with | None -> return_longest_path rest (Some path) | Some long -> let (_nodes, _weight) = path in let (_longestNodes, _longestWeight) = long in if _weight > _longestWeight then return_longest_path rest (Some path) else return_longest_path rest longest in return_longest_path (find_all_paths g a b) None ;;
let open_account (initial_pass: passwd) : bank_account = let passwd = ref initial_pass in let balance = ref 0 in let count = ref 0 in { update_pass = (fun a b -> if a = !passwd then (count := 0; passwd := b) else (count := !count + 1; raise wrong_pass)) ; retrieve = (fun a b -> if !count <= 4 then if a = !passwd then if b >= 0 then if !balance - b >= 0 then (count := 0; balance := !balance - b) else raise not_enough_balance else raise negative_amount else (count := !count + 1; raise wrong_pass) else raise too_many_failures) ; deposit = (fun a b -> if !count <= 4 then if a = !passwd then if b >= 0 then (count := 0; balance := !balance + b) else raise negative_amount else (count := !count + 1; raise wrong_pass) else raise too_many_failures) ; show_balance = (fun a -> if !count <= 4 then if a = !passwd then (count := 0; !balance) else (count := !count + 1; raise wrong_pass) else raise too_many_failures) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let get_next x y = let v1, v2, w = x in if v1 = vertex then (v2, w)::y else y in List.fold_right (get_next) 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 name, weight = node in if name = b then (visited, sum) else aux_list (neighbours g name) visited sum and aux_list (nodes: ('a * weight) list) (visited: 'a list) (sum: int) : ('a list * weight) = match nodes with | (name, weight) :: xs when not (List.exists (fun z -> z = name) visited) -> (try aux_node (name, weight) (visited @ [name]) (sum + weight) with Fail -> aux_list xs visited sum) | _ :: xs -> aux_list xs visited sum | _ -> raise Fail 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) (sum: int) fc sc : ('a list * weight)= let name, weight = node in if name = b then sc (visited, sum) else aux_list (neighbours g name) visited sum fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (sum: int) fc sc : ('a list * weight) = match nodes with | (name, weight) :: xs when not (List.exists (fun z -> z = name) visited) -> aux_node (name, weight) (visited @ [name]) (sum + weight) (fun () -> aux_list xs visited sum fc sc) sc | _ :: xs -> aux_list xs visited sum fc sc | _ -> fc () 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 if_longer x y = let new_sites, new_val = x in match y with | Some n -> let old_sites, old_val = n in if new_val > old_val then Some x else y | None -> Some x in List.fold_right (if_longer) (find_all_paths g a b) None;;
let count = ref 0;; let makecounter () = { increase = (fun () -> incr count; !count) ; reset = (fun () -> count := 0) } let check_attempts (c: counter) = let attempt = c.increase () in if attempt < 5 then raise wrong_pass else count := attempt ; raise too_many_failures;;
let open_account (initial_pass: passwd) : bank_account = let current_pass = ref initial_pass in let balance = ref 0 in let c = makecounter () in let update_pass : passwd -> passwd -> unit = fun old_pass new_pass -> if old_pass = !current_pass then current_pass := new_pass else check_attempts c in let deposit : passwd -> int -> unit = fun pass money -> if pass = !current_pass then if money >= 0 then balance := !balance + money else raise negative_amount else try check_attempts c with | too_many_failures -> raise too_many_failures in let retrieve : passwd -> int -> unit = fun pass money -> let c = makecounter () in if pass = !current_pass then if money < 0 then raise negative_amount else if money > !balance then raise not_enough_balance else balance := !balance - money else try check_attempts c with | too_many_failures -> raise too_many_failures in let show_balance : passwd -> int = fun pass -> let c = makecounter () in if pass = !current_pass then !balance else try check_attempts c with | too_many_failures -> raise too_many_failures in let update = fun x y -> update_pass x y in let r = fun x y -> retrieve x y in let d = fun x y -> deposit x y in let s = fun x -> show_balance x in let acc = { update_pass = update ; retrieve = r; deposit = d; show_balance = s} in acc ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let l = ref [] in match g with | {nodes; edges} -> List.fold_left (fun x y -> match y with | (v1, v2, w) when v1 = vertex -> let pair = (v2, w) in l := [pair] @ !l ; !l | _ -> if List.mem vertex nodes then l:= [] @ !l else l:= []; !l ) (l:= []; !l) edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let path_nodes = ref [a] in let path_weight = ref 0 in let n = neighbours g a in let visited = [a] in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = match node with | (x, w) when List.mem x visited -> raise Fail | (x, w) -> if x = b then let path = List.rev !path_nodes in (path, !path_weight) else let n = neighbours g x in aux_list n visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (x, w)::tl -> path_nodes := [x] @ !path_nodes; path_weight := w + !path_weight; try aux_node (x, w) visited with Fail -> let visited = [x] @ visited in path_nodes := List.tl !path_nodes; path_weight := !path_weight - w; aux_list tl visited in aux_list n visited;;
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 pwd = ref initial_pass in let balance = ref 0 in let tries = ref 0 in let comparator a b = if (b<0) then raise negative_amount else (if b> !a then raise not_enough_balance else a := !a-b) in { update_pass = (fun x (y:passwd)-> if (!pwd = x) then (tries := 0 ;pwd := y) else (tries := !tries+1; raise (Msg "Wrong password")) ); retrieve = (fun x y -> (if !tries>4 then raise too_many_failures else (if !pwd != x then (tries := !tries + 1;raise wrong_pass) else (tries :=0; comparator balance y) ))); deposit = (fun x y-> (if !tries > 4 then raise too_many_failures else (if x = !pwd then (tries := 0; (if (y<0) then raise (negative_amount) else balance := !balance+y)) else (tries := !tries+1 ; raise wrong_pass)))); show_balance = (fun x -> if !tries > 4 then raise too_many_failures else (if x = !pwd then (tries := 0 ;!balance) else (tries := !tries+1; raise wrong_pass))); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let aux_fil x (a,_,_) = if a=x then true else false in let l1 = List.filter (aux_fil vertex) g.edges in let f1 (_,b,c) = (b,c) in List.map f1 l1;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let aux5 (a,b1) = a in let aux6 (a,b) = b in let aux2 a (b,c) = if a=b then true else false in let aux1 a b = if a = b then true else false in let rec aux4 l1 acc1 acc2 = match l1 with | [] -> (acc1,acc2) | h::t -> aux4 t ( ((aux5 h)::acc1)) ((aux6 h)+acc2) in let aux3 (a,b) = a in let rec filter_all l1 l2 acc= match l1 with | []->acc | h::t -> if List.exists (aux1 h) l2 then filter_all t l2 acc else filter_all t l2 (h::acc) in let rec aux_node target l1 visited = match l1 with | [] -> raise Fail | h::t -> if (aux2 target h) then h::visited else try aux_node target (t) visited with Fail -> aux_node target (filter_all (neighbours g (aux3 h)) (h::visited) []) (h::visited) in let path = aux_node b (neighbours g a) [(a,0)] in aux4 path [] 0;;
let find_path' (g: 'a graph) (a: 'a) (target: 'a) = let aux1 (x1,_) = x1 in let aux2 (_,y2) = y2 in let aux3 (a,a1) b = if a = b then true else false in let rec aux4 acc1 acc2 l1 = match l1 with | [] -> (acc1,acc2) | h::t -> aux4 ((aux1 h)::acc1) ((aux2 h)+acc2) t in let aux5 z4 z5 (z1,z2,z3)= (z1=z4 && z2=z5) in let aux6 (l1,sum) = List.hd (List.rev l1) in let aux7 l2 a b= List.filter (aux5 a b) l2 in let aux8 (a,b) (m1,m2,m3) = (a@[target],b+m3) in let rec filter_all l1 l2 acc= match l1 with | []->acc | h::t -> if List.exists (aux3 h) l2 then filter_all t l2 acc else filter_all t l2 (h::acc) in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc = if ((aux1 node)=target) then sc [(a,0)] else aux_list (filter_all (neighbours g (aux1 node)) visited []) ((aux1 node)::visited) fc (fun r-> sc (node::r)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc = match nodes with | []-> fc () | h::t -> aux_node h visited (fun ()->aux_list t (visited) fc sc) sc in let ans = aux_list (neighbours g a) [a] (fun ()->raise Fail) (aux4 [] 0) in let ele = aux6 ans in let add = aux7 g.edges ele target in aux8 ans (List.hd add);;
let find_all_paths (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) list = let f1 (a,b) = a in let pred1 string1 string2 (s1,s2,_) = (string1=s1 && string2=s2) in let fun1 (_,_,i1) = i1 in let rec make_up_aux graph target acc list1 = match list1 with |[]-> acc |h::t -> if (h=target) then acc else make_up_aux graph target (fun1 (List.find (pred1 h (List.hd t)) graph.edges)+acc) t in let make_up graph target list1 = (list1, (make_up_aux graph target 0 list1)) in let rec filter_all l1 l2 acc= let p x y = x=y in match l1 with | []->acc | h::t -> if List.exists (p h) l2 then filter_all t l2 acc else filter_all t l2 (h::acc) in;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let fun1 (_,i1) = i1 in let paths = find_all_paths g a b in let rec aux l1 acc = match l1 with | [] -> if acc= ([],0) then None else Some acc | h::t -> if (fun1 h)>(fun1 acc) then aux t h else aux t acc in aux paths ([],0);;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let counter = ref 0 in let update_pass (old_pass: passwd) (new_pass: passwd) : unit = let p = !pass in if(String.equal old_pass p) then let c = counter := 0 in pass := new_pass else let c = counter := !counter + 1 in raise wrong_pass in let deposit (old_pass: passwd) (cash: int) : unit = if(!counter > 4) then raise too_many_failures else let p = !pass in if(String.equal old_pass p) then let c = counter := 0 in if(cash >= 0) then balance := !balance + cash else raise negative_amount else let d = counter := !counter + 1 in raise wrong_pass in let retrieve (old_pass: passwd) (cash: int) : unit = if(!counter > 4) then raise too_many_failures else let p = !pass in if(String.equal old_pass p) then let c = counter := 0 in if(cash >= 0) then ( if cash <= !balance then balance:= !balance - cash else raise not_enough_balance ) else raise negative_amount else let d = counter := !counter + 1 in raise wrong_pass in let show_balance old_pass = if(!counter > 4) then raise too_many_failures else let p = !pass in if(String.equal old_pass p) then let c = counter := 0 in !balance else let d = counter := !counter + 1 in raise wrong_pass in {update_pass; retrieve; deposit; show_balance} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let has_edge ((v1, v2, w): 'a * 'a * weight) : bool = String.equal v1 vertex in let get_v_w (edge: 'a * 'a * weight) : ('a * weight) = let (v1, v2, w) = edge in (v2, w) in let edge_list = List.filter has_edge g.edges in List.map get_v_w edge_list ;; let rec get_weight n b = let neighbor = List.hd n in let (v, w) = neighbor in if(String.equal v b) then w else get_weight (List.tl n) b ;;
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(String.equal v b) then ([v], w) else try let (rest, mg) = aux_list (neighbours g v) visited in (v :: rest, w + mg) with Fail -> aux_list (neighbours g a) visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = if(List.length nodes == 0) then raise Fail else let curr = List.hd nodes in let (v, w) = curr in if(List.exists (String.equal v) visited) then aux_list (List.tl nodes) visited else let new_vis = v :: visited in aux_node curr new_vis in let neighbors = neighbours g a in let (n_list, _) = List.split neighbors in let (r1, r2) = aux_list neighbors ([a]) in if(List.exists (String.equal b) n_list) then ([a; b], get_weight neighbors b) else (a :: r1, r2) ;;
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 = try let all_paths = find_all_paths g a b in get_longest_path all_paths all_paths 0 with Fail -> None ;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let failedPwd = ref 0 in let authorizeC (pwd: passwd) (operation: 'a -> unit) input = if (!failedPwd = 5) then raise too_many_failures else let _ = failedPwd := 0 in match !password with | p when p = pwd -> (operation input) | _ -> failedPwd := (!failedPwd + 1); raise wrong_pass in let authorizeS (pwd: passwd) (operation: unit -> 'a) input = if (!failedPwd >= 5) then raise too_many_failures else let _ = failedPwd := 0 in match !password with | p when p = pwd -> (operation input) | _ -> failedPwd := !failedPwd + 1; raise wrong_pass in let changePassword oldPwd newPwd = match !password with | p when p = oldPwd -> password := newPwd; failedPwd := 0; | _ -> raise wrong_pass in let changeBalance operator delta = if delta < 0 then raise negative_amount else let currentBalance = !balance in balance := (operator currentBalance delta) in let retrieveMoney amount = let currentBalance = !balance in if amount > currentBalance then raise not_enough_balance else changeBalance (-) amount in { update_pass = changePassword; retrieve = (fun pwd -> authorizeC pwd retrieveMoney); deposit = (fun pwd -> authorizeC pwd (changeBalance (+))); show_balance = (fun pwd -> authorizeS pwd (fun _ -> !balance) ()); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let { edges } = g in let outboundEdges = List.filter (fun edge -> let (n1, _, _) = edge in n1 = vertex) edges in List.fold_right (fun edge list -> (let (_, n2, w) = edge in [(n2, w)]@list)) outboundEdges [] ;;
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 (nod, weight) = node in match nod with | n when n = b -> ([nod], weight) | _ -> ( let nbs = neighbours g nod in let (path, w) = aux_list nbs ([nod] @ visited) in match path with | _::_ -> ([nod] @ path, w + weight) | [] -> ([], 0) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list): ('a list * weight) = let unduplicated = List.filter (fun arg -> let (nod, _) = arg in List.for_all (fun vis -> vis <> nod) visited ) nodes in let procd = List.map (fun arg -> aux_node arg visited) unduplicated in let eligible = List.filter (fun a -> let (p, _) = a in List.length p > 0) procd in match eligible with | hd::_ -> hd | [] -> ([], 0) in let (path, w) = aux_node (a, 0) [] in match path with | _::_ -> (path, w) | _ -> 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)= 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 check_attempts (attempt : int) = if attempt >= 5 then raise too_many_failures;;
let open_account (initial_pass: passwd) : bank_account = let cur_pass = ref initial_pass in let num_attempt = ref 0 in let cur_balance = ref 0 in let my_bank_account = { update_pass = ( fun old_pass new_pass -> if old_pass <> !cur_pass then let _ = (num_attempt := !num_attempt + 1) in raise wrong_pass else let _ = (num_attempt := 0) in cur_pass := new_pass ) ; retrieve = ( fun pass amount -> check_attempts !num_attempt; if (pass = !cur_pass) then let _ = (num_attempt := 0) in if (amount < 0) then raise negative_amount else if (!cur_balance - amount < 0) then raise not_enough_balance else cur_balance := !cur_balance - amount else let _ = (num_attempt := !num_attempt + 1) in raise wrong_pass ) ; deposit = ( fun pass dep -> check_attempts !num_attempt; if pass = !cur_pass then let _ = (num_attempt := 0) in if (dep < 0) then raise negative_amount else cur_balance := !cur_balance + dep else let _ = (num_attempt := !num_attempt + 1) in raise wrong_pass ) ; show_balance = ( function pass -> check_attempts !num_attempt; if !cur_pass = pass then let _ = (num_attempt := 0) in !cur_balance else let _ = (num_attempt := !num_attempt + 1) in raise wrong_pass ) } in my_bank_account ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f l (a, b, c) = if a = vertex then (b,c) :: l else l 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) (path: 'a list) (w: weight) : ('a list * weight) = let (cur_node, cur_weight) = node in let path = path @ [cur_node] in let w = w + cur_weight in let visited = cur_node :: visited in if cur_node = b then (path, w) else let neigh = neighbours g cur_node in match neigh with | [] -> raise Fail | x::xs -> aux_list neigh visited path w and aux_list (nodes: ('a * weight) list) (visited: 'a list) (path: 'a list) (w: weight) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> let (cur_node, _) = x in if (List.mem cur_node visited) then aux_list xs visited path w else try aux_node x visited path w with Fail -> aux_list xs visited path w 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) (path : 'a list) (w : weight) fc sc : ('a list * weight)= let (cur_node, cur_weight) = node in let path = path @ [cur_node] in let w = w + cur_weight in let visited = cur_node :: visited in if cur_node = b then sc (path, w) else let neigh = neighbours g cur_node in match neigh with | [] -> fc () | x::xs -> let suc2 = sc in let fail2 = fun () -> aux_list neigh visited path w fc sc in aux_list neigh visited path w fail2 suc2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) (path : 'a list) (w : weight) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> let (cur_node, _) = x in if (List.mem cur_node visited) then aux_list xs visited path w fc sc else let suc2 = sc in let fail2 = fun () -> aux_list xs visited path w fc sc in aux_node x visited path w fail2 suc2 in let failure = (fun () -> raise Fail) in let success = (fun x -> x) in aux_node (a,0) [] [] 0 failure success;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let ls = find_all_paths g a b in let rec get_largest (ls : ('a list * weight) list) (path : ('a list * weight) option) (weight : weight) : ('a list * weight) option = match ls with | [] -> path | x::xs -> let (_, cur_weight) = x in if cur_weight > weight then let cur_path' = Some x in get_largest xs cur_path' cur_weight else get_largest xs path weight in get_largest ls None 0;;
let open_account(initial_pass: passwd) : bank_account = let n = ref 0 and pass = ref initial_pass and count = ref 0 in { update_pass = (fun oldpass -> fun newpass -> if oldpass <> !pass then (count := !count + 1; raise wrong_pass) else (pass := newpass; count := 0)) ; retrieve = (fun pwd -> fun amount -> if !count = 3 then raise too_many_failures else if pwd <> !pass then (count := !count + 1; raise wrong_pass) else (if amount > !n then raise not_enough_balance else n := !n - amount ; count := 0)); deposit = (fun pwd -> fun amount -> if !count = 3 then raise too_many_failures else if pwd <> !pass then raise (count := !count + 1; raise wrong_pass) else n:= !n + amount; count := 0); show_balance = (fun pwd -> if !count = 3 then raise too_many_failures else if pwd <> !pass then raise (count := !count + 1; raise wrong_pass) else (count := 0; !n)) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f = (fun (v1,v2,w) acc -> if v1 = vertex then (v2,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 add = (fun (l,wt) (v,w) -> (v::l, wt+w) )in let rec aux_node (v,w) (visited : 'a list) : ('a list * weight) = if (List.exists (fun vis -> vis = v ) visited) then raise Fail else if (v=b) then ([],0) else aux_list (neighbours g v) (v::visited) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |(v,w)::t -> try (add (aux_node (v,w) visited) (v,w)) with Fail -> aux_list t (v::visited) in aux_list [(a, 0)] [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (v,w) (visited : 'a list) fc sc : ('a list * weight)= if (List.exists (fun vis -> vis = v ) visited) then fc () else if (v=b) then sc ([],0) else aux_list (neighbours g v) (v::visited) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |(v,w)::t -> aux_node (v,w) visited (fun () -> aux_list t (v::visited) fc sc) (fun (l,wt) -> sc (v::l, w+wt) ) in aux_list [(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 |x::t -> Some (List.fold_left (fun (l1, w1) (l2, w2) -> if (w2<w1) then (l1, w1) else (l2,w2)) x t);;
let open_account (p: passwd) : bank_account = let balance = ref 0 in let password = ref p in let attempts = ref 0 in let checkPass (p: passwd): bool = if (!attempts < 3) then if (p = !password) then (attempts := 0; true) else (attempts := !attempts+1; raise wrong_pass) else raise wrong_pass in { update_pass = (fun (oldP: passwd) (newP: passwd) -> if (oldP = !password) then (password := newP; attempts := 0) else (attempts := !attempts+1; raise wrong_pass)); retrieve = (fun (p: passwd) (amount: int) -> if (checkPass p) then if (amount <= !balance) then balance := !balance - amount else raise not_enough_balance); deposit = (fun (p: passwd) (amount: int) -> if (checkPass p) then balance := !balance + amount); show_balance= (fun (p: passwd) -> if (checkPass p) then !balance else raise negative_amount); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f = (fun (v1,v2,w) acc -> if v1 = vertex then (v2,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 add = (fun (l,wt) (v,w) -> (v::l, wt+w) )in let rec aux_node (v,w) (visited : 'a list) : ('a list * weight) = if (List.exists (fun vis -> vis = v ) visited) then raise Fail else if (v=b) then ([],0) else aux_list (neighbours g v) (v::visited) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |(v,w)::t -> try (add (aux_node (v,w) visited) (v,w)) with Fail -> aux_list t (v::visited) in aux_list [(a, 0)] [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (v,w) (visited : 'a list) fc sc : ('a list * weight)= if (List.exists (fun vis -> vis = v ) visited) then fc () else if (v=b) then sc ([],0) else aux_list (neighbours g v) (v::visited) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |(v,w)::t -> aux_node (v,w) visited (fun () -> aux_list t (v::visited) fc sc) (fun (l,wt) -> sc (v::l, w+wt) ) in aux_list [(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 |x::t -> Some (List.fold_left (fun (l1, w1) (l2, w2) -> if (w2<w1) then (l1, w1) else (l2,w2)) x t);;
let open_account (initial_pass: passwd) : bank_account = let acc_data = {balance = ref 0 ; pass = ref initial_pass ; passwd_attempts = ref 0 ; too_many_fails_flag = ref false} in let is_too_many_fails ()= if !(acc_data.passwd_attempts) = 5 then acc_data.too_many_fails_flag := true; in let reset () = (acc_data.passwd_attempts := 0; acc_data.too_many_fails_flag:= false) in let bad_pass () = (acc_data.passwd_attempts := !(acc_data.passwd_attempts) +1; is_too_many_fails (); raise wrong_pass) in { update_pass = (fun (given_pass:passwd) (new_pass:passwd) : unit -> (if given_pass = !(acc_data.pass) then (acc_data.pass := new_pass; reset ()) else bad_pass ())); retrieve = (fun (given_pass:passwd) (amount_to_take: int) : unit -> if !(acc_data.too_many_fails_flag) then raise too_many_failures else (if given_pass = !(acc_data.pass) then (reset() ; (if amount_to_take < 0 then raise negative_amount else (if !(acc_data.balance) >= amount_to_take then acc_data.balance := (!(acc_data.balance) - amount_to_take) else raise not_enough_balance) )) else bad_pass ())) ; deposit = (fun (given_pass:passwd) (amount_to_give: int) : unit -> (if !(acc_data.too_many_fails_flag) then raise too_many_failures else (if given_pass = !(acc_data.pass) then (reset(); if amount_to_give < 0 then raise negative_amount else acc_data.balance := (!(acc_data.balance) + amount_to_give);) else (bad_pass () )))) ; show_balance = (fun (given_pass:passwd): int -> (if !(acc_data.too_many_fails_flag) then raise too_many_failures else ( if given_pass = !(acc_data.pass) then (reset(); !(acc_data.balance);) else (bad_pass())))) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let h = List.filter (fun x -> let (v1,_,_) = x in v1 = vertex) (g.edges) in List.map (fun c -> let (_,v2,w) = c in (v2,w)) h;; let add_to_visited (l:'a list) (x:'a):unit = x::l; ();;
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 node_name = fst node in let node_weight = snd node in if (fst node) = b then ([node_name], node_weight) else let x = neighbours g (node_name) in match x with |[] -> raise Fail |h::[] -> if List.exists (fun x -> x = fst h) visited then raise Fail else let cx = (aux_node h (visited @ [node_name])) in (node_name :: (fst cx), (snd (cx) + node_weight)); |h::tl as ss-> (let eee = aux_list x (visited @ [node_name]) in (node_name :: (fst eee), snd eee + node_weight)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |[h] -> if List.exists (fun x -> x = fst h) visited then raise Fail else aux_node h visited |h::tl -> try if List.exists (fun x -> x = fst h) visited then aux_list tl visited else aux_node h visited; with Fail -> aux_list tl visited; in let x2 = neighbours g a in match x2 with |[] -> raise Fail |[h] -> let h4 = aux_list x2 [a] in (a :: (fst h4), snd h4) ; |h::tl as zz-> let lo = aux_list x2 [a] in (a :: (fst lo), snd lo);;
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 node_name = fst node in let node_weight = snd node in if (fst node) = b then ([node_name], node_weight) else let x = neighbours g (node_name) in match x with |[] -> fc () |[h] -> if List.exists (fun x -> x = fst h) visited then fc () else sc node (aux_node h (visited @ [node_name]) fc sc) |h::h2::tl-> if List.exists (fun x -> x = fst h) visited then aux_node h2 visited fc sc else let eee = aux_list x (visited @ [node_name]) fc sc in sc node eee and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |[h] -> if List.exists (fun x -> x = fst h) visited then fc () else (aux_node h visited fc sc) |h::tl -> if List.exists (fun x -> x = fst h) visited then aux_list tl visited fc sc else let fc2 = (fun () -> aux_list tl (visited @ [fst h]) fc sc) in (aux_node h visited fc2 sc); in let x2 = neighbours g a in match x2 with |[] -> raise Fail |[h] -> let h4 = aux_node h [a] (fun () -> raise Fail) (fun h6 (x:('a list*weight)) -> ((fst h6) :: (fst x), (snd x) + (snd h6))) in (a :: (fst h4), snd h4) ; |h::tl as zz-> let lo = aux_list x2 [a] (fun () -> raise Fail) (fun h6 (x:('a list*weight)) -> ((fst h6) :: (fst x)), (snd x) + (snd h6)) in (a:: (fst lo), snd lo);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let x = find_all_paths g a b in if x = [] then None else let initial_max_path= ([],0) in let rec helper (all_paths:('a list * int) list) (max_path: ('a list * int)) : ('a list * int) option = match all_paths with |[]-> Some (max_path) |h::tl -> if (snd max_path) > (snd h) then helper tl max_path else helper tl h in helper x initial_max_path;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let num_attempts = ref 0 in { update_pass = (fun (oldp:passwd) (newp:passwd) -> if oldp <> !password then let _ = num_attempts := !num_attempts + 1 in raise wrong_pass else let _ = password := newp in num_attempts := 0 ); retrieve = (fun (pass: passwd) (amt:int) -> if !num_attempts > 4 then raise too_many_failures else if pass <> !password then let _ = num_attempts := !num_attempts + 1 in raise wrong_pass else if amt < 0 then raise negative_amount else match amt with |x when !balance < amt -> let _ = num_attempts := 0 in raise not_enough_balance |_ -> let _ = balance := !balance - amt in num_attempts := 0 ); deposit = (fun (pass: passwd) (amt:int) -> if !num_attempts > 4 then raise too_many_failures else if pass <> !password then let _ = num_attempts := !num_attempts + 1 in raise wrong_pass else if amt < 0 then let _ = num_attempts := 0 in raise negative_amount else let _ = balance := !balance + amt in num_attempts := 0 ); show_balance= (fun (pass: passwd) -> if !num_attempts > 4 then raise too_many_failures else if pass <> !password then let _ = num_attempts := !num_attempts + 1 in raise wrong_pass else let _ = num_attempts := 0 in !balance ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let ls = List.fold_left (fun init_list x -> match x with |(v1,v2,w) when v1 = vertex -> (v2,w)::init_list |_ -> init_list) [] g.edges in ls ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = if a = b then raise Fail else let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (city,w) = node in if city = b && not(List.mem city visited) then ([city],w) else if List.mem city visited then raise Fail else let (ls,wei) = aux_list (neighbours g city) (city::visited) in (city::ls,wei+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |(city,w)::tl -> try aux_node (city,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) = if a = b then raise Fail else let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (city,w) = node in if city = b && not(List.mem city visited) then sc ([city],w) else if List.mem city visited then fc () else let sc' = (fun (ls,wei) -> sc(city::ls,wei+w)) in aux_list (neighbours g city) (city::visited) fc sc' and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |(city,w)::tl -> let sc2 = sc in let fc2 = fun () -> aux_list tl visited fc sc in aux_node (city,w) visited fc2 sc2 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 = let allpaths = find_all_paths g a b in if List.length allpaths = 0 then None else let p = List.sort (fun ((xs,xi):('a list * int)) ((xs,yi):('a list * int)) -> if xi > yi then -5 else if xi = yi then 0 else 5) allpaths in Some(List.nth p 0) ;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let num_attempts = ref 0 in { update_pass = (fun (oldp:passwd) (newp:passwd) -> if oldp <> !password then let _ = num_attempts := !num_attempts + 1 in raise wrong_pass else let _ = password := newp in num_attempts := 0 ); retrieve = (fun (pass: passwd) (amt:int) -> if !num_attempts > 4 then raise too_many_failures else if pass <> !password then let _ = num_attempts := !num_attempts + 1 in raise wrong_pass else if amt < 0 then raise negative_amount else match amt with |x when !balance < amt -> let _ = num_attempts := 0 in raise not_enough_balance |_ -> let _ = balance := !balance - amt in num_attempts := 0 ); deposit = (fun (pass: passwd) (amt:int) -> if !num_attempts > 4 then raise too_many_failures else if pass <> !password then let _ = num_attempts := !num_attempts + 1 in raise wrong_pass else if amt < 0 then let _ = num_attempts := 0 in raise negative_amount else let _ = balance := !balance + amt in num_attempts := 0 ); show_balance= (fun (pass: passwd) -> if !num_attempts > 4 then raise too_many_failures else if pass <> !password then let _ = num_attempts := !num_attempts + 1 in raise wrong_pass else let _ = num_attempts := 0 in !balance ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let ls = List.fold_left (fun init_list x -> match x with |(v1,v2,w) when v1 = vertex -> (v2,w)::init_list |_ -> init_list) [] g.edges in ls ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = if a = b then raise Fail else let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (city,w) = node in if city = b && not(List.mem city visited) then ([city],w) else if List.mem city visited then raise Fail else let (ls,wei) = aux_list (neighbours g city) (city::visited) in (city::ls,wei+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |(city,w)::tl -> try aux_node (city,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) = if a = b then raise Fail else let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (city,w) = node in if city = b && not(List.mem city visited) then sc ([city],w) else if List.mem city visited then fc () else let (ls,wei) = aux_list (neighbours g city) (city::visited) fc sc in (city::ls,wei+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |(city,w)::tl -> let sc2 = sc in let fc2 = fun () -> aux_list tl visited fc sc in aux_node (city,w) visited fc2 sc2 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 = let allpaths = find_all_paths g a b in if List.length allpaths = 0 then None else let p = List.sort (fun ((xs,xi):('a list * int)) ((xs,yi):('a list * int)) -> if xi > yi then -5 else if xi = yi then 0 else 5) allpaths in Some(List.nth p 0) ;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let counter = ref 0 in {update_pass = (fun old newPassword -> if old = !password then (counter := 0; password := newPassword) else (counter := !counter + 1; raise wrong_pass)); retrieve = (fun pass amount -> if !counter >= 5 then raise too_many_failures else (if pass = !password then (counter:=0; if !balance < amount then (raise not_enough_balance) else if amount < 0 then (raise negative_amount) else (balance := !balance - amount)) else (counter := !counter + 1; raise wrong_pass))); deposit = (fun pass amount -> if !counter >= 5 then raise too_many_failures else (if pass = !password then (counter:=0; if amount < 0 then (raise negative_amount) else (balance := !balance + amount)) else (counter := !counter + 1; raise wrong_pass) )); show_balance = (fun pass -> if !counter >= 5 then raise too_many_failures else (if pass = !password then (counter:=0; !balance) else (counter:= !counter + 1; raise wrong_pass))); };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = 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: 'a * weight) (visited : 'a list) : ('a list * weight) = if (fst node) = b then ([b], (snd node)) else if List.mem (fst node) visited then raise Fail else (let (path, cost) = aux_list (neighbours g (fst node)) ((fst node) :: visited) in ((fst node) :: path, cost + (snd node))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (v,w) :: nodes -> try aux_node (v,w) visited with Fail -> aux_list nodes 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)= if (fst node) = b then sc ([b], (snd node)) else if List.mem (fst node) visited then fc () else aux_list (neighbours g (fst node)) ((fst node) :: visited) fc (fun (path, cost) -> sc ((fst node) :: path, cost + (snd node))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | nodetrial :: others -> aux_node nodetrial visited (fun () -> aux_list others visited fc sc) sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun l -> l);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max paths = match paths with | [] -> None | [(path, weight)] -> Some (path, weight) | (path, weight) :: (path2, weight2) :: tl -> if weight < weight2 then (max ((path2, weight2) :: tl)) else (max ((path, weight) :: tl)) in max (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let account = ref initial_pass in let balance = ref 0 in let pass_fails = ref 0 in { update_pass = (fun (old_pass:passwd) (new_pass:passwd) -> if !account = old_pass then (account := new_pass; pass_fails := 0 ) else ( pass_fails := !pass_fails + 1; raise wrong_pass ) ); deposit = (fun (pass:passwd) (amount:int) -> if !pass_fails >= 5 then raise too_many_failures else if !account = pass then (pass_fails := 0; if amount < 0 then raise negative_amount else balance := !balance + amount) else ( pass_fails := !pass_fails + 1; raise wrong_pass ) ); retrieve = (fun (pass:passwd) (amount:int) -> if !pass_fails >= 5 then raise too_many_failures else if !account = pass then (pass_fails := 0; if amount < 0 then raise negative_amount else if !balance - amount >= 0 then balance := !balance - amount else raise not_enough_balance) else ( pass_fails := !pass_fails + 1; raise wrong_pass ) ); show_balance = (fun (pass:passwd) -> if !pass_fails >= 5 then raise too_many_failures else if !account = pass then (pass_fails := 0; !balance) else ( pass_fails := !pass_fails + 1; raise wrong_pass ) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun ls v -> let (v1, v2, w) = v in if v1 = vertex then (v2,w)::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) (weight : int) : ('a list * weight) = let (n,w) = node in if n = b then (visited@[n], weight+w) else if List.mem n visited then raise Fail else aux_list (neighbours g n) (visited@[n]) (weight+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (weight : int) : ('a list * weight) = match nodes with | [] -> raise Fail | v::vs -> let (n,w) = v in if n = b then (visited@[n], weight+w) else try aux_list vs visited weight with Fail -> aux_node v visited weight 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) (weight : int) fc sc : ('a list * weight)= let (n,w) = node in if n = b then sc (visited@[n], weight+w) else if List.mem n visited then fc () else aux_list (neighbours g n) (visited@[n]) (weight+w) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (weight : int) fc sc : ('a list * weight) = match nodes with | [] -> fc () | v::vs -> let (n, w) = v in if n = b then sc (visited@[n], weight+w) else let suc2 = sc in let fail2 = fun () -> aux_node v visited weight fc sc in aux_list vs visited weight fail2 suc2 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 = match find_all_paths g a b with | [] -> None | path::paths -> let rec max_weight (cur_max, rest) = match rest with | [] -> Some cur_max | path'::paths' -> let (n',w') = path' in let (n, w) = cur_max in let cur_max' = if w' > w then path' else cur_max in let rest'=paths' in max_weight (cur_max', rest') in max_weight (path, paths);;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let fail_count = ref 0 in { update_pass = (fun old_p new_p -> match old_p with | _ when old_p = !password -> fail_count := 0; password := new_p | _ -> fail_count := !fail_count + 1; raise wrong_pass ) ; retrieve = (fun p amount -> match p with | _ when !fail_count = 5 -> raise too_many_failures | _ when p = !password -> fail_count := 0; if amount < 0 then raise negative_amount else if !balance < amount then raise not_enough_balance else balance := !balance - amount | _ -> fail_count := !fail_count + 1; raise wrong_pass ) ; deposit = (fun p amount -> match p with | _ when !fail_count = 5 -> raise too_many_failures | _ when p = !password -> fail_count := 0; if amount < 0 then raise negative_amount else balance := !balance + amount | _ -> fail_count := !fail_count + 1; raise wrong_pass ) ; show_balance = (fun p -> match p with | _ when !fail_count = 5 -> raise too_many_failures | _ when p = !password -> fail_count := 0; !balance | _ -> fail_count := !fail_count + 1; raise wrong_pass ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let {nodes; edges} = g in List.fold_left (fun neighbour_list edge -> let (v1, v2, w) = edge in if v1 = vertex then (v2, w) :: neighbour_list else neighbour_list ) [] 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 * weight) list = let (n, _) = node in let paths = neighbours g n in aux_list paths visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a * weight) list = match nodes with | [] -> raise Fail | node :: ns -> try match node with | (n, _) when n = b -> [node] | (n, _) when List.mem n visited -> aux_list ns (n :: visited) | (n, _) -> node :: aux_node node (n :: visited) with Fail -> let (n, _) = node in aux_list ns (n :: visited) in if a = b then ([a], 0) else let paths = neighbours g a in let path = aux_list paths [a] in ( List.fold_left ( fun path node -> let (next, _) = node in path @ [next] ) [a] path, List.fold_left ( fun total_w node -> let (_, w) = node in total_w + w ) 0 path ) ;;
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 * weight) list = let (n, _) = node in let paths = neighbours g n in aux_list paths visited fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a * weight) list = match nodes with | [] -> fc | node :: ns -> match node with | (n, _) when List.mem n visited -> let sc2 = sc in let fc2 = fc in aux_list ns (n :: visited) fc2 sc2 | (n, _) -> if n = b then sc @ [node] else let sc2 = sc @ [node] in let fc2 = aux_list ns (n :: visited) fc sc in aux_node node (n :: visited) fc2 sc2 in if a = b then ([a], 0) else let paths = neighbours g a in let path = aux_list paths [a] [] [] in if path = [] then raise Fail else ( List.fold_left ( fun path node -> let (next, _) = node in path @ [next] ) [a] path, List.fold_left ( fun total_w node -> let (_, w) = node in total_w + w ) 0 path ) ;;