text
stringlengths 0
601k
|
---|
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (fun (_, v2, w) -> (v2, w)) (List.filter (fun (v1, _, _) -> v1 = vertex) g.edges);; |
let find_all_paths (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) list = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) list = if (List.mem (fst node) visited) then [] else if ((fst node) = b) then [([fst node], snd node)] else List.map (fun x -> (fst node :: fst x, (snd node + snd x))) (aux_list (neighbours g (fst node)) (visited @ [fst node])) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) list = match nodes with | x :: xs -> aux_node x visited @ aux_list xs visited | _ -> [] in aux_node (a, 0) [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = match (find_all_paths g a b) with | x :: xs -> x | _ -> 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)= if (List.mem (fst node) visited) then fc () else if ((fst node) = b) then sc ([fst node], snd node) else (aux_list (neighbours g (fst node)) (visited @ [fst node])) (fun () -> fc ()) (fun (nodes, weight) -> sc ([fst node] @ nodes, weight + (snd node))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | x :: xs -> let suc2 = sc in let fail2 = fun () -> aux_node x visited fc sc in aux_list xs visited fail2 suc2 | _ -> fc () in aux_node (a, 0) [] (fun () -> raise Fail) (fun (nodes, weight) -> (nodes, weight));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match List.rev (List.sort (fun a b -> if snd a > snd b then 1 else if snd b > snd a then -1 else 0) (find_all_paths g a b)) with | x :: xs -> Some(x) | _ -> None;; |
let open_account (initial_pass: passwd) : bank_account = let pw = ref initial_pass in let bal = ref 0 in let wrongPasswordCounter = ref 0 in {update_pass = (fun (old_pass : passwd) (new_pass : passwd) -> if old_pass = !pw then (pw := new_pass; wrongPasswordCounter := 0;) else (wrongPasswordCounter := !wrongPasswordCounter + 1; raise wrong_pass)); retrieve = (fun (pass : passwd) (amount : int) -> if !wrongPasswordCounter >= 5 then raise too_many_failures else if pass = !pw then (wrongPasswordCounter := 0; if amount < 0 then raise negative_amount else if amount > !bal then raise not_enough_balance else bal := !bal - amount) else (wrongPasswordCounter := !wrongPasswordCounter + 1; if !wrongPasswordCounter > 5 then raise too_many_failures else raise wrong_pass)); deposit = (fun (pass : passwd) (amount : int) -> if !wrongPasswordCounter >= 5 then raise too_many_failures else if pass = !pw then (wrongPasswordCounter := 0; if amount < 0 then raise negative_amount else bal := !bal + amount) else (wrongPasswordCounter := !wrongPasswordCounter + 1; if !wrongPasswordCounter > 5 then raise too_many_failures else raise wrong_pass)); show_balance = (fun (pass : passwd) -> if !wrongPasswordCounter >= 5 then raise too_many_failures else if pass = !pw then (wrongPasswordCounter := 0; !bal) else (wrongPasswordCounter := !wrongPasswordCounter + 1; if !wrongPasswordCounter > 5 then raise too_many_failures else raise wrong_pass)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = getNeighborsAndWeights 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) (weightAcc : weight) : ('a list * weight) = if fst(node) = b then (visited @ [fst(node)], weightAcc + snd(node)) else if List.mem (fst(node)) visited then raise Fail else aux_list (neighbours g (fst(node))) (visited @ [fst(node)]) (weightAcc + snd(node)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (weightAcc : int) : ('a list * weight) = match nodes with | [] -> raise Fail | cur::rest -> try aux_node cur visited weightAcc with Fail -> aux_list rest visited weightAcc 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) (weightAcc : weight) fc sc : ('a list * weight)= if fst(node) = b then (visited @ [fst(node)], weightAcc + snd(node)) else if List.mem (fst(node)) visited then fc() else aux_list (neighbours g (fst(node))) (visited @ [fst(node)]) fc sc (weightAcc + snd(node)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc (weightAcc : weight) : ('a list * weight) = match nodes with | [] -> fc() | cur::rest -> aux_node cur visited weightAcc (fun () -> aux_list rest visited fc sc weightAcc) sc in aux_node (a, 0) [] 0 (fun () -> raise Fail) (fun a -> a);; |
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 allPaths = [] then None else notimplemented();; |
let open_account (initial_pass: passwd) : bank_account = let pw = ref initial_pass in let bal = ref 0 in let wrongPasswordCounter = ref 0 in {update_pass = (fun (old_pass : passwd) (new_pass : passwd) -> if old_pass = !pw then (pw := new_pass; wrongPasswordCounter := 0;) else (wrongPasswordCounter := !wrongPasswordCounter + 1; raise wrong_pass)); retrieve = (fun (pass : passwd) (amount : int) -> if !wrongPasswordCounter >= 5 then raise too_many_failures else if pass = !pw then (wrongPasswordCounter := 0; if amount < 0 then raise negative_amount else if amount > !bal then raise not_enough_balance else bal := !bal - amount) else (wrongPasswordCounter := !wrongPasswordCounter + 1; if !wrongPasswordCounter > 5 then raise too_many_failures else raise wrong_pass)); deposit = (fun (pass : passwd) (amount : int) -> if !wrongPasswordCounter >= 5 then raise too_many_failures else if pass = !pw then (wrongPasswordCounter := 0; if amount < 0 then raise negative_amount else bal := !bal + amount) else (wrongPasswordCounter := !wrongPasswordCounter + 1; if !wrongPasswordCounter > 5 then raise too_many_failures else raise wrong_pass)); show_balance = (fun (pass : passwd) -> if !wrongPasswordCounter >= 5 then raise too_many_failures else if pass = !pw then (wrongPasswordCounter := 0; !bal) else (wrongPasswordCounter := !wrongPasswordCounter + 1; if !wrongPasswordCounter > 5 then raise too_many_failures else raise wrong_pass)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = getNeighborsAndWeights 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) (weightAcc : weight) : ('a list * weight) = if fst(node) = b then (visited @ [fst(node)], weightAcc + snd(node)) else if List.mem (fst(node)) visited then raise Fail else aux_list (neighbours g (fst(node))) (visited @ [fst(node)]) (weightAcc + snd(node)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (weightAcc : int) : ('a list * weight) = match nodes with | [] -> raise Fail | cur::rest -> try aux_node cur visited weightAcc with Fail -> aux_list rest visited weightAcc 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) (weightAcc : weight) fc sc : ('a list * weight)= if fst(node) = b then (visited @ [fst(node)], weightAcc + snd(node)) else if List.mem (fst(node)) visited then fc() else aux_list (neighbours g (fst(node))) (visited @ [fst(node)]) fc sc (weightAcc + snd(node)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc (weightAcc : weight) : ('a list * weight) = match nodes with | [] -> fc() | cur::rest -> aux_node cur visited weightAcc (fun () -> aux_list rest visited fc sc weightAcc) sc in aux_node (a, 0) [] 0 (fun () -> raise Fail) (fun a -> a);; |
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 allPaths = [] then None else let firstPath = List.nth allPaths 0 in let rec findMaxWeight (currentMaxPath: 'a list * weight) (listOfPaths: ('a list * weight) list) = match listOfPaths with | [] -> Some (currentMaxPath) | hd :: tl -> if snd(hd) > snd(currentMaxPath) then findMaxWeight hd tl else findMaxWeight currentMaxPath tl in findMaxWeight firstPath allPaths;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let money = ref 0 in let fail = ref 0 in { update_pass = (fun passwd1 passwd2 -> if passwd1 <> !password then (fail := (!fail + 1); raise wrong_pass) else (password := passwd2; fail := 0)); deposit = (fun pass depo -> if !fail >= 5 then raise too_many_failures else if pass <> !password then (fail := (!fail + 1); raise wrong_pass) else if depo < 0 then (fail := 0; raise negative_amount) else (fail := 0; money := (!money + depo))); retrieve = (fun pass retri -> if (!fail >= 5) then raise too_many_failures else if pass <> !password then (fail := (!fail + 1); raise wrong_pass) else match retri with | _ when retri < 0 -> (fail := 0; raise negative_amount) | _ -> (if (!money - retri) < 0 then (fail := 0; raise not_enough_balance) else (fail := 0; money := (!money - retri)))); show_balance = (fun pass -> if !fail >= 5 then raise too_many_failures else if pass <> !password then (fail := (!fail + 1); raise wrong_pass) else (fail := 0; !money)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun lst node -> let (v,x,y) = node in if vertex = v then (x,y)::lst else lst) [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let w = ref 0 in let path = ref [] in let all_w = ref [] in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (cur_n, cur_w) = node in path := !path @ [cur_n]; w := !w + cur_w; all_w := !all_w@[cur_w]; if b = cur_n then (!path, !w) else aux_list (neighbours g cur_n) (cur_n::visited); and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> ( match List.rev !path, List.rev !all_w with | [], [] -> raise Fail | [], last_w::rest_w -> w := !w - last_w; all_w := List.rev rest_w; raise Fail | _::t_path, [] -> path := List.rev t_path; raise Fail | _::t_path, last_w::rest_w -> w := !w - last_w; all_w := List.rev rest_w; path := List.rev t_path; raise Fail ) | (n,n_w)::ns -> if List.mem n visited then aux_list ns visited else try aux_node (n,n_w) visited with Fail -> aux_list ns (n::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 (cur_n, _) = node in if b = cur_n then sc ([], 0) else aux_list (neighbours g cur_n) (cur_n::visited) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc ([], 0) | (n,n_w)::ns -> if List.mem n visited then aux_list ns visited fc sc else aux_node (n,n_w) visited (fun x -> aux_list ns visited fc sc) (fun t -> let (t_l, t_w) = (sc t) in (t_l@[n], t_w+n_w)) in aux_node (a, 0) [] (fun x -> raise Fail) (fun y -> let (y_l, y_w) = y in (y_l@[a], y_w));; ( match List.rev !path, List.rev !all_w with | [], [] -> raise Fail | [], last_w::rest_w -> w := !w - last_w; all_w := List.rev rest_w; raise Fail | _::t_path, [] -> path := List.rev t_path; raise Fail | _::t_path, last_w::rest_w -> w := !w - last_w; all_w := List.rev rest_w; path := List.rev t_path; raise Fail ) | (n,n_w)::ns -> (if List.mem n visited then match ns with | [] -> !all_path | _ -> aux_list ns visited else (try (aux_node (n,n_w) visited; match ns with | [] -> !all_path | _ -> aux_list ns (n::visited)) with Fail -> try aux_list ns (n::visited) with Fail -> !all_path)) in try aux_node (a, 0) [] with Fail -> !all_path;;*);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = try Some (List.nth (List.sort (fun x y -> compare (snd y) (snd x)) (find_all_paths g a b)) 0) with Failure _ -> None;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let money = ref 0 in let fail = ref 0 in { update_pass = (fun passwd1 passwd2 -> if passwd1 <> !password then (fail := (!fail + 1); raise wrong_pass) else (password := passwd2; fail := 0)); deposit = (fun pass depo -> if !fail >= 5 then raise too_many_failures else if pass <> !password then (fail := (!fail + 1); raise wrong_pass) else if depo < 0 then (fail := 0; raise negative_amount) else (fail := 0; money := (!money + depo))); retrieve = (fun pass retri -> if (!fail >= 5) then raise too_many_failures else if pass <> !password then (fail := (!fail + 1); raise wrong_pass) else match retri with | _ when retri < 0 -> (fail := 0; raise negative_amount) | _ -> (if (!money - retri) < 0 then (fail := 0; raise not_enough_balance) else (fail := 0; money := (!money - retri)))); show_balance = (fun pass -> if !fail >= 5 then raise too_many_failures else if pass <> !password then (fail := (!fail + 1); raise wrong_pass) else (fail := 0; !money)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun lst node -> let (v,x,y) = node in if vertex = v then (x,y)::lst else lst) [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let w = ref 0 in let path = ref [] in let all_w = ref [] in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (cur_n, cur_w) = node in path := !path @ [cur_n]; w := !w + cur_w; all_w := !all_w@[cur_w]; if b = cur_n then (!path, !w) else aux_list (neighbours g cur_n) (cur_n::visited); and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> ( match List.rev !path, List.rev !all_w with | [], [] -> raise Fail | [], last_w::rest_w -> w := !w - last_w; all_w := List.rev rest_w; raise Fail | _::t_path, [] -> path := List.rev t_path; raise Fail | _::t_path, last_w::rest_w -> w := !w - last_w; all_w := List.rev rest_w; path := List.rev t_path; raise Fail ) | (n,n_w)::ns -> if List.mem n visited then aux_list ns visited else try aux_node (n,n_w) visited with Fail -> aux_list ns (n::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 (cur_n, _) = node in if b = cur_n then sc ([], 0) else aux_list (neighbours g cur_n) (cur_n::visited) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc ([], 0) | (n,n_w)::ns -> if List.mem n visited then aux_list ns visited fc sc else aux_node (n,n_w) visited (fun x -> aux_list ns visited fc sc) (fun t -> let (t_l, t_w) = (sc t) in (t_l@[n], t_w+n_w)) in aux_node (a, 0) [] (fun x -> raise Fail) (fun y -> let (y_l, y_w) = y in (y_l@[a], y_w));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = try Some (List.nth (List.sort (fun x y -> compare (snd y) (snd x)) (find_all_paths g a b)) 0) with Failure _ -> None;; |
let open_account (initial_pass: passwd) : bank_account = let passCount = ref 0 in let password = ref initial_pass in let balance = ref 0 in let incrCount () = passCount := !passCount + 1; raise wrong_pass in let reset () = passCount := 0 in { update_pass = (fun (input : passwd) (newPass : passwd) -> if (compare input (!password)) <> 0 then incrCount () else password := newPass ; reset () ) ; deposit = (fun (input : passwd) (amount : int) -> if !passCount >= 5 then raise too_many_failures else if (compare input (!password)) <> 0 then incrCount () else if amount < 0 then raise negative_amount else balance := !balance + amount ; reset () ) ; retrieve = (fun (input : passwd) (amount : int) -> if !passCount >= 5 then raise too_many_failures else if (compare input (!password)) <> 0 then incrCount () else if amount < 0 then raise negative_amount else match (!balance - amount) with | x when x < 0 -> raise not_enough_balance ; reset () | _ -> balance := !balance - amount ; reset () ) ; show_balance = (fun (input : passwd) -> if !passCount >= 5 then raise too_many_failures else if (compare input (!password)) <> 0 then incrCount () else reset () ; !balance ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = if not (List.mem vertex (g.nodes)) then [] else if (List.length g.edges = 0) then [] else let help_neighbours (edge : ('a * 'a * weight)) (ngbs : ('a * weight) list) = match edge with | (x, n, w) when (compare x vertex) = 0 -> (n, w)::ngbs | (x, _, _) when (compare x vertex) <> 0 -> ngbs in List.fold_right help_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) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | hd::tl -> let (n, w) = hd in if not (List.mem n visited) then aux_node hd visited else aux_list tl visited 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 = if (find_all_paths g a b) = [] then None else let rec longest (paths : ('a list * weight) list) (max : weight) (p : 'a list * weight) = match paths with | [] -> if max = 0 then None else Some(p) | (x, cur)::xs -> if cur > max then longest xs cur (x, cur) else longest xs max p in longest (find_all_paths g a b) 0 ([], 0) ;; |
let open_account (initial_pass: passwd) : bank_account = let max_wrong_pass = 5 in let wrong_passes = ref 0 in let password = ref initial_pass in let money = ref 0 in let too_many_failures () = if !wrong_passes >= max_wrong_pass then raise too_many_failures in let password_not_eq password' = if password' <> !password then (incr wrong_passes; raise wrong_pass) in let negative_amount quantity = if quantity < 0 then raise negative_amount in {update_pass = (fun old' new' -> password_not_eq old' ; password := new' ; wrong_passes := 0); deposit = (fun password' quantity -> too_many_failures () ; password_not_eq password' ; negative_amount quantity ; wrong_passes := 0 ; money := !money + quantity); retrieve = (fun password' quantity -> too_many_failures () ; password_not_eq password' ; negative_amount quantity ; if !money - quantity < 0 then raise not_enough_balance ; wrong_passes := 0 ; money := !money - quantity); show_balance = (fun password' -> too_many_failures () ; password_not_eq password' ; wrong_passes := 0 ; !money)} ;; let graph1 = {nodes = ["New York"; "Boston"]; edges = []} in let node = "New York" in graph1, node), []; (let graph2 = {nodes = ["New York"; "Boston"; "Shanghai"]; edges = [("New York", "Boston", 1000); ("New York", "Shanghai", 2000)]} in let node = "New York" in graph2, node), [("Boston", 1000); ("Shanghai", 2000)]; (let graph3 = {nodes = ["New York"; "Boston"]; edges = ["New York", "Boston", 1000]} in let node = "New York" in graph3, node), ["Boston", 1000] ];; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.(g.edges |> filter (fun (s, _, _) -> s = vertex) |> map (fun (_, d, weight) -> d, weight));; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let invalid_value = -1 in let rec aux_node (vertex, weight: 'a * weight) (visited : 'a list) : ('a list * weight) = match vertex = b with | true -> [b], weight | _ when not (List.mem vertex visited) -> let visited = vertex :: visited in let adjs = neighbours g vertex in let path, path_weight = aux_list adjs visited in (match path with | [] -> path, path_weight | _::_ -> let path = vertex :: path in let path_weight = weight + path_weight in path, path_weight) | _ -> ([], invalid_value) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = List.fold_left (function | (_::_) as path, path_weight -> fun _ -> path, path_weight | [], path_weight -> fun node -> aux_node node visited) ([], invalid_value) nodes in;; |
let neighbours = neighbours g a in let path, path_weight = aux_list neighbours [a] in let path = if path = [] then raise Fail else a :: path in path, path_weight;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let invalid_value = -1 in let rec aux_node (vertex, weight: 'a * weight) (visited : 'a list) fc sc = match vertex = b with | true -> sc ([b], weight) | _ when not (List.mem vertex visited) -> let visited = vertex :: visited in let adjs = neighbours g vertex in aux_list adjs visited fc (fun (path, path_weight) -> match path with | [] -> sc (path, path_weight) | _::_ -> let path = vertex :: path in let path_weight = weight + path_weight in sc (path, path_weight)) | _ -> sc ([], invalid_value) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc = match nodes with | [] -> sc ([], invalid_value) | node :: nodes -> aux_node node visited fc (fun (path, path_weight) -> match path with | _::_ -> sc (path, path_weight) | [] -> aux_list nodes visited fc sc) in;; |
let neighbours = neighbours g a in let fc () = raise Fail in aux_list neighbours [a] fc (fun (path, path_weight) -> if path = [] then fc () else a :: path, path_weight);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = List.fold_left (function | None -> fun x -> Some x | (Some (cur_list, cur_weight) as cur_path) -> function | (path_list, path_weight) when cur_weight <= path_weight -> Some (path_list, path_weight) | _ -> cur_path) None (find_all_paths g a b);; |
let open_account (initial_pass: passwd) : bank_account = let n = ref 0 in let pass= ref initial_pass in let balance= ref 0 in { update_pass= (fun (curpass:passwd) (newpass:passwd)-> if curpass = !pass then begin n := 0; pass := newpass end else begin n := !n+1; raise wrong_pass end ) ; retrieve=(fun inpass a -> if !n = 5 then raise too_many_failures else if not (inpass = !pass) then begin n := !n+1;raise wrong_pass end else if a < 0 then begin n:=0; raise negative_amount end else if a > !balance then begin n:=0 ; raise not_enough_balance end else begin n:=0; balance := !balance-a end ) ; deposit=(fun inpass a -> if !n = 5 then raise too_many_failures else if not (inpass = !pass) then begin n := !n+1;raise wrong_pass end else if a < 0 then begin n:=0; raise negative_amount end else begin n:=0 ;balance := !balance+a end); show_balance=(fun inpass -> if !n = 5 then raise too_many_failures else if not (inpass = !pass) then begin n := !n+1; raise wrong_pass end else begin n := 0; !balance end) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = if g.edges=[] then [] else if g.nodes=[] then [] else let f (p:('a * 'a * weight)) l = let (v1,v2,w)=p in if v1=vertex then let (c:int) = w in ((v2,c)::l) else l in List.fold_right f g.edges [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let n= ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (x,k)= node in if x = b then begin n:=!n + k; (visited @ [x], !n) end else if List.exists (fun y -> x=y) visited then begin n:=!n + k;raise Fail end else if neighbours g x=[] then begin n:=!n + k;raise Fail end else begin n:=!n + k; aux_list (neighbours g x) (visited@[x]) end and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | []-> raise Fail | (x,k)::xs-> try aux_node (x,k) (visited) with Fail -> begin n:=!n - k;aux_list xs visited end in let l1 = neighbours g a in aux_list l1 [a];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let n=ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (x,k)= node in if x = b then begin (fun (x,k) ((l:'a list), (n:weight))-> (l@[x],n+k)) end else if List.exists (fun y -> x=y) visited then begin (fun tuple -> tuple) end else if neighbours g x=[] then (fun tuple -> tuple) else begin aux_list (neighbours g x) (visited@[x]) (fun (l,k) ->(l,k)) (fun (x,k) (l, n)-> (l@[x],n+k)) end notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented () let filter l1 l2= let rec fil l1 l2 l3 = match l1 with |[]->l3 |x::xs -> let (y,k)=x in if List.exists (fun p -> y=p) l2 then fil xs l2 l3 else fil xs l2 l3@[x] in fil l1 l2 [] let neighbors node graph except: 'a list= let rec aux = function | [] -> [] | (l,r,_)::tail -> if l = node then r::(aux tail) else aux tail in List.filter (fun x -> not (List.mem x except)) (aux graph);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec longest list = match list with |[] -> None |(x,k)::[]-> Some (x,k) |(x,k)::(y,n)::xs -> if n>=k then longest ((y,n)::xs) else longest ((x,k)::xs) in longest (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 failures = ref 0 in { update_pass = ( fun (old_pass: passwd) (new_pass: passwd) -> if !password = old_pass then (password := new_pass; failures := 0) else (failures := (!failures + 1); raise wrong_pass) ) ; retrieve = ( fun (pass: passwd) (amount: int) -> if !failures < 5 then ( if !password = pass then ( failures := 0; if amount <= !balance then ( if amount >= 0 then (balance := (!balance - amount); failures := 0) else raise negative_amount ) else raise not_enough_balance ) else (failures := (!failures + 1); raise wrong_pass) ) else raise too_many_failures ) ; deposit = ( fun (pass: passwd) (amount: int) -> if !failures < 5 then ( if !password = pass then ( failures := 0; if amount >= 0 then (balance := (!balance + amount); failures := 0) else raise negative_amount ) else (failures := (!failures + 1); raise wrong_pass) ) else raise too_many_failures ) ; show_balance = ( fun (pass: passwd) : int -> if !failures < 5 then ( if !password = pass then (failures := 0; !balance) else (failures := (!failures + 1); raise wrong_pass) ) else raise too_many_failures ) ; } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.rev (List.fold_left (fun acc edge -> match edge with | (v, o, weight) -> if v = vertex then (o, weight)::acc else acc | _ -> acc ) [] g.edges);; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (n, w) = node in if n = b then (n::[], w) else (try let (l, w1) = aux_list (neighbours g n) visited in (n::l, w + w1) with Fail -> raise Fail) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (n, w)::t -> if List.exists (fun v -> v = n) visited then aux_list t (visited) else (try aux_node (n, w) (n::visited) with Fail -> if t = [] then raise Fail else aux_list t (n::visited)) in aux_node (a, 0) (a::[]);; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (n, w) = node in if n = b then sc (n::[], w) else aux_list (neighbours g n) (visited) (fun f -> fc Fail) (fun (lc, wc) -> sc (n::lc, w + wc)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc Fail | (n, w)::t -> if List.exists (fun v -> v = n) visited then aux_list t (visited) (fun f -> fc Fail) sc else aux_node (n, w) (n::visited) (fun f -> if t = [] then fc Fail else aux_list t (visited) (fun f -> fc Fail) sc) sc in aux_node (a, 0) (a::[]) raise (fun r -> r);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let longest = ref (None : ('a list * weight) option) in let rec aux maximum l = match l with |[] -> maximum |x :: xs -> aux (max maximum x) xs in try Some (List.find (fun (l1, w1) -> w1 = (aux 0 (List.map (fun (l, w) -> w) (find_all_paths g a b)))) (find_all_paths g a b)) with Not_found -> None;; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let count = ref 0 in let current_pass = ref initial_pass in { update_pass = (fun old_p new_p -> if (!current_pass = old_p) then (current_pass := new_p ; count := 0) else (count := (!count + 1); raise wrong_pass)); deposit = (fun pass dep -> if (pass = !current_pass) then if (!count <5) then (count := 0; (if (dep > 0 ) then balance := (!balance + dep) else raise negative_amount)) else raise too_many_failures else (count := (!count + 1); if (!count >5) then raise too_many_failures else raise wrong_pass)); retrieve= (fun pass retr -> if ( pass = !current_pass) then if (!count <5) then (count := 0; (if (retr <0) then raise negative_amount else if (!balance >= retr) then balance := (!balance - retr) else raise not_enough_balance)) else raise too_many_failures else (count := (!count + 1); if (!count >5) then raise too_many_failures else raise wrong_pass)); show_balance = ( fun pass -> if ( pass = !current_pass) then if (!count <5) then (count := 0 ; !balance) else raise too_many_failures else (count := (!count + 1); if (!count >5) then raise too_many_failures else raise wrong_pass)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f = (fun(x,y,z) acc ->(y,z)::acc) in let init = [] in let a = List.filter (fun(v1,_,_) -> vertex =v1) g.edges in List.fold_right f a init let get_node = fun(v,w) -> v;; let get_weight = fun(v,w) -> w;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited:'a list) : ('a list * weight) = if (get_node node) = b then ([],0) else if List.length (neighbours g (get_node node)) = 0 then raise Fail else aux_list (neighbours g (get_node node)) ((get_node node)::(visited)) and aux_list (nodes: ('a * weight) list) (visited:'a list ) : ('a list * weight) = match nodes with |[] -> raise Fail |x::xs -> if List.mem (get_node x) visited then aux_list xs visited else try ((get_node x)::(get_node (aux_node x visited)), (get_weight x) +(get_weight (aux_node x visited))) with Fail -> (aux_list xs visited) in let res = aux_node (a,0) [] in (a::((get_node res)), (get_weight res));; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let path =([],0) in let rec aux_node (node: 'a * weight) (visited : 'a list) (path: ('a list * weight)) fc sc : ('a list * weight)= if (get_node node) = b then sc ((get_node node)::(get_node path) , (get_weight node) + (get_weight path)) else ( if List.length (neighbours g (get_node node)) = 0 then fc () else aux_list (neighbours g (get_node node)) ((get_node node)::(visited)) ((get_node node)::(get_node path) , (get_weight node) + (get_weight path)) fc sc ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (path: ('a list * weight)) fc sc : ('a list * weight) = match nodes with |[] -> fc () |x::xs -> let sc2 = sc in let fail2 = fun () -> (aux_list xs visited path fc sc) in if List.mem (get_node x) visited then (aux_list xs visited path fc sc) else (aux_node x visited path fail2 sc2) in let res = aux_node (a, 0) [] ([],0) (fun () -> raise Fail) (fun x -> x) in (List.rev (get_node res), (get_weight res));; |
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 if all_paths = [] then None else let f = (fun x y -> if (get_weight y) >= (get_weight x) then y else x ) in Some ( List.fold_left f ([], 0) all_paths);; |
let open_account (initial_pass: passwd) : bank_account = let pass= ref initial_pass in let wrongs= ref 0 in let balance= ref 0 in { update_pass=(fun (old:passwd) (nu:passwd) -> if (old = !pass) then let _ = wrongs:=0 in pass := nu else let _ = wrongs:=!wrongs +1 in raise wrong_pass ) ; retrieve=(fun (p: passwd) (amt:int) :unit -> if (p = !pass && !wrongs < 5) then let _ = wrongs:=0 in if (amt<0) then raise negative_amount else if (!balance >= amt) then (balance := !balance - amt) else raise not_enough_balance else if (!wrongs < 5) then let _ = wrongs:= !wrongs +1 in raise wrong_pass else raise too_many_failures ) ; deposit=(fun (p: passwd) (amt: int) -> if (p = !pass && !wrongs < 5) then let _ = wrongs:=0 in if (amt<0) then raise negative_amount else balance := (!balance + amt) else if (!wrongs < 5) then let _ = wrongs:= !wrongs +1 in raise wrong_pass else raise too_many_failures ); show_balance=(fun (p: passwd) -> if (p = !pass && !wrongs < 5) then let _ = wrongs:=0 in !balance else if (!wrongs < 5) then let _ = wrongs:= !wrongs +1 in raise wrong_pass else raise too_many_failures ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right ( fun (e2:('a*'a*weight)) acc -> match e2 with | (v1,v2,w) when (v1=vertex) -> (v2,w)::acc | _ -> acc ) (g.edges) [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (a,w)=node in let ns=(neighbours (g) (fst(node))) in if (not(List.mem (fst(node)) visited) ) then if (List.exists (fun x->fst(x) = b) (ns)) then let lE=(List.find (fun x->fst(x) = b) (ns)) in ( (visited@[fst(node)])@[fst(lE)] ,snd(node)+snd(lE) ) else aux_list ( List.map (fun x->(fst(x),snd(x)+snd(node))) (ns) ) (visited@[fst(node)]) else if (not(List.mem (fst(node)) visited)) then aux_list ( List.map (fun x->(fst(x),snd(x)+snd(node))) (ns) ) (visited@[fst(node)]) *) else raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | n::nn -> if (fst(n)=b) then (visited@[fst(n)],snd(n)) else try (aux_node (n) (visited)) with Fail -> aux_list nn 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 (a,w)=node in let ns=(neighbours (g) (fst(node))) in if (not(List.mem (fst(node)) visited) ) then if (List.exists (fun x->fst(x) = b) (ns)) then let lE=(List.find (fun x->fst(x) = b) (ns)) in ( (visited@[fst(node)])@[fst(lE)] ,snd(node)+snd(lE) ) else sc (node,ns) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) (fc: unit-> ('a list * weight) ) (sc: unit -> ('a list * weight) ) : ('a list * weight) = match nodes with | [] -> raise Fail | n::nn -> if (fst(n)=b) then (visited@[fst(n)],snd(n)) else aux_node (n) (visited) ( fun() -> aux_list (nn) (visited) (fc) (sc) ) ( fun (node,ns) -> aux_list ( List.map (fun x->(fst(x),snd(x)+snd(node))) (ns) ) (visited@[fst(node)]) (fc) (sc) ) in aux_list (neighbours g a) ([a]) (fun () -> ([],0) ) (fun ()-> ([],0) ) ;; |
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 = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in { update_pass = (fun old_pass new_pass -> if old_pass = !account then (attempts := 0; account := new_pass) else (attempts := !attempts + 1; raise wrong_pass)); retrieve = (fun pass withdraw -> if !attempts < 5 then if pass = !account then if withdraw >= 0 then if withdraw <= !balance then (attempts := 0; balance := !balance - withdraw) else raise not_enough_balance else (attempts := 0; raise negative_amount) else (attempts := !attempts + 1; raise wrong_pass) else raise too_many_failures); deposit = (fun pass amount -> if !attempts < 5 then if pass = !account then if amount >= 0 then (attempts := 0; balance := !balance + amount) else (attempts := 0; raise negative_amount) else (attempts := !attempts + 1; raise wrong_pass) else raise too_many_failures); show_balance = (fun pass -> if !attempts < 5 then if pass = !account then (attempts := 0; !balance) else (attempts := !attempts + 1; raise wrong_pass) else raise too_many_failures) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let out_neigh acc e = match e with | (v, out, w) when v = vertex -> (out, w) :: acc | _ -> acc in List.fold_left out_neigh [] g.edges ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let n, w = node in if List.mem n visited then raise Fail else if n = b then ([n], w) else let next = aux_list (neighbours g n) (n :: visited) in (n :: fst next, w + snd next) 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 let path = aux_list (neighbours g a) [a] in (a :: fst path, snd 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 list * weight)= let n, w = node in if List.mem n visited then fc () else if n = b then sc ([n], w) else let succ = (fun nd -> sc (n :: fst nd, w + snd nd)) in aux_list (neighbours g n) (n :: visited) fc succ and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x :: xs -> let fail = fun () -> aux_node x visited fc sc in aux_list xs visited fail sc in let add_a = fun r -> let n, w = r in (a :: n, 0 + w) in aux_list (neighbours g a) [a] (fun () -> raise Fail) add_a ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in match paths with | [] -> None | _ -> let weights = fun x y -> snd x - snd y in List.nth_opt (List.rev (List.sort weights paths)) 0 ;; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let pwd = ref initial_pass in let failures = ref 0 in { update_pass = (fun pwd1 pwd2 -> if pwd1 = !pwd then (failures := 0; pwd := pwd2 ) else (failures := !failures + 1; raise wrong_pass)); retrieve = (fun pwd1 money -> if !failures >= 5 then raise too_many_failures else if pwd1 = !pwd then (failures := 0; if money < 0 then raise negative_amount else if !balance - money < 0 then raise not_enough_balance else balance := !balance - money) else (failures := !failures + 1; raise wrong_pass)); deposit = (fun pwd1 money -> if !failures >= 5 then raise too_many_failures else if pwd1 = !pwd then (failures := 0; if money < 0 then raise negative_amount else balance := !balance + money) else (failures := !failures + 1; raise wrong_pass)); show_balance = (fun pwd1 -> if !failures >= 5 then raise too_many_failures else if pwd1 = !pwd then (failures := 0; !balance) else (failures := !failures + 1; raise wrong_pass)) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun x (a,b,c) -> if a = vertex then x@[(b,c)] else x) [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a) (w: weight) (visited : 'a list) : ('a list * weight) = if List.exists (fun a -> a = node) visited then raise Fail else node :: visited; aux_list (neighbours g a) visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (a,w)::xs -> try aux_node a w visited with Fail -> aux_list xs 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 )(w: 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 x = ref initial_pass in let balance = ref 0 in let count = ref 0 in let negative b = (if b < 0 then raise negative_amount) in let enough b = (if b > !balance then raise not_enough_balance) in let check a = (if a <> !x || !count >= 5 then (count:= !count + 1; if !count <= 5 then raise wrong_pass else raise too_many_failures) else count := 0) in { update_pass = (fun a b -> if a = !x then (count := 0; x := b) else (count:= !count + 1; raise wrong_pass)); deposit = (fun a b -> check a; negative b; balance := !balance + b); retrieve = (fun a b -> check a; negative b; enough b; balance := !balance - b); show_balance = (fun a -> check a; !balance) } ;; let get_x xyz = let (x,y,z) = xyz in x;; let get_y xyz = let (x,y,z) = xyz in y;; let get_z xyz = let (x,y,z) = xyz in z;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let l = ref [] in let d a = (a :: !l) in let c a = l := d a in let x b = (if (get_x b) = vertex then c ((get_y b),(get_z b))) in let _ = List.iter x g.edges in !l ;; let get_first xy = let (x,y) = xy in x;; let get_last xy = let (x,y) = xy in y;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited: ('a list * weight)) : ('a list * weight) = if (get_first node) = b then ((get_first node) :: (get_first visited), (get_last visited) + (get_last node)) else (if List.length (neighbours g (get_first node)) = 0 then raise Fail else aux_list (neighbours g (get_first node)) (((get_first node) :: (get_first visited)), (get_last visited) + (get_last node)) ) and aux_list (nodes: ('a * weight) list) (visited: ('a list * weight)) : ('a list * weight) = match nodes with | [] -> raise Fail | x :: xs -> if List.exists (fun a -> a = (get_first x)) (get_first visited) then (aux_list xs visited) else try (aux_node x visited) with Fail -> (aux_list xs visited) in let res = aux_node (a, 0) ([], 0) in ((List.rev (get_first res)), (get_last res)) ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited: ('a list * weight)) fc sc : ('a list * weight)= if (get_first node) = b then sc ((get_first node) :: (get_first visited), (get_last visited) + (get_last node)) else ( if List.length (neighbours g (get_first node)) = 0 then fc () else aux_list (neighbours g (get_first node)) (((get_first node) :: (get_first visited)), (get_last visited) + (get_last node)) fc sc ) and aux_list (nodes: ('a * weight) list) (visited: ('a list * weight)) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x :: xs -> let suc2 = sc in let fail2 = fun () -> (aux_list xs visited fc sc) in if List.exists (fun a -> a = (get_first x)) (get_first visited) then (aux_list xs visited fc sc) else aux_node x visited fail2 suc2 in let res = aux_node (a, 0) ([], 0) (fun () -> raise Fail) (fun x -> x) in ((List.rev (get_first res)), (get_last res)) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all = find_all_paths g a b in if all = [] then None else Some (List.fold_left (fun x y -> if (get_last y) >= (get_last x) then y else x) ([], 0) all );; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let nb_fail = ref 0 in let balance = ref 0 in { update_pass = (fun old_pass new_pass -> if (old_pass = !pass) then (nb_fail := 0; pass := new_pass) else (nb_fail := !nb_fail + 1; raise wrong_pass) ); retrieve = (fun old_pass amount -> if(!nb_fail >= 5) then (raise too_many_failures) else if (not (old_pass = !pass)) then (nb_fail := !nb_fail + 1; raise wrong_pass) else if (amount > !balance || amount < 0) then (nb_fail := 0; if amount >= 0 then raise not_enough_balance else raise negative_amount) else ( nb_fail := 0; balance := !balance - amount; ) ); deposit = (fun old_pass amount -> if(!nb_fail >= 5) then (raise too_many_failures) else if (not (old_pass = !pass)) then (nb_fail := !nb_fail + 1; raise wrong_pass) else if (amount < 0) then (nb_fail := 0; raise negative_amount) else ( nb_fail := 0; balance := !balance + amount; ) ); show_balance = (fun old_pass -> if(!nb_fail >= 5) then (raise too_many_failures) else if (not (old_pass = !pass)) then (nb_fail := !nb_fail + 1; raise wrong_pass) else ( nb_fail := 0; !balance; ) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun lst edge -> let (v, neigh, weight) = edge in if v = vertex then (neigh, weight) :: lst else lst) [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a) (visited : 'a list) : ('a * weight) list = if List.length (List.filter ((=) node) visited) > 0 || node = b then [] else let newe = List.filter (fun n -> let (neigh, _, _) = n in neigh = node) g.edges in List.map (fun n -> let (_, next, w) = n in (next, w)) newe and find_path (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = if nodes = [] then raise Fail else let curr :: rest = nodes in let (n, w) = curr in if (n = b) then ([n], w) else let adj = aux_node n visited in try let path = find_path adj (n::visited) in let (cl, tw) = path in (n::cl, tw+w) with Fail -> if rest = [] then raise Fail else find_path rest visited in try let path = find_path (aux_node a []) [a] in let (cl, tw) = path in (a::cl, tw) with Fail -> raise Fail;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a) (visited : 'a list) : ('a * weight) list = if List.length (List.filter ((=) node) visited) > 0 || node = b then [] else let newe = List.filter (fun n -> let (neigh, _, _) = n in neigh = node) g.edges in List.map (fun n -> let (_, next, w) = n in (next, w)) newe and path (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = if nodes = [] then fc () else let curr :: rest = nodes in let (n, w) = curr in if (n = b) then sc ([n], w) else let adj = aux_node n visited in let fail = fun () -> path rest visited fc sc in let success = (fun nextList -> let (m, y) = nextList in sc (n::m, w+y)) in path adj (n::visited) fail success in let neigs = (aux_node a []) in if (neigs = []) then raise Fail else path neigs [a] (fun () -> raise Fail) (fun nextList -> let (m, y) = nextList in (a::m, 0+y));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec longest_path (lst: ('a list * weight) list) (longest: ('a list * weight)) (cost: int) : ('a list * weight) = match lst with | [] -> longest | x::xs -> let (p, c) = x in if c > cost then longest_path xs x c else longest_path xs longest cost in let all_paths = find_all_paths g a b in if all_paths = [] then None else Some (longest_path all_paths ([], 0) 0);; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let password = ref initial_pass in let failure_count = ref 0 in let check_failures = (fun () -> if !failure_count >= 5 then raise too_many_failures) in let check_password = (fun (pass: passwd) -> if pass <> !password then (failure_count := !failure_count + 1; raise wrong_pass) else failure_count := 0) in let check_negative_amount = (fun (amount: int) -> if amount < 0 then raise negative_amount) in { update_pass = (fun (old_p: passwd) (new_p: passwd) -> check_password old_p; password := new_p ); retrieve = (fun (p: passwd) (a: int) -> check_failures (); check_password p; check_negative_amount a; if a > !balance then raise not_enough_balance else balance := !balance - a ); deposit = (fun (p: passwd) (a: int) -> check_failures (); check_password p; check_negative_amount a; balance := !balance + a ); show_balance = (fun (p: passwd) -> check_failures (); check_password p; !balance ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let matched_vertex = fun (v1, v2, w) acc -> if vertex = v1 then (v2, w) :: acc else acc in List.fold_right matched_vertex g.edges [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((node_a, node_w): 'a * weight) (visited : 'a list) : ('a list * weight) = if List.mem node_a visited then raise Fail else if node_a = b then (visited @ [node_a], node_w) else;; |
let neighbours = List.map (fun (x, y) -> (x, y + node_w) ) (neighbours g node_a) in try aux_list neighbours (visited @ [node_a]) with Fail -> raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x :: xs -> try aux_node x visited with Fail -> aux_list xs visited in aux_node (a, 0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((node_a, node_w): 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if List.mem node_a visited then fc () else if node_a = b then sc (visited @ [node_a], node_w) else;; |
let neighbours = List.map (fun (x, y) -> (x, y + node_w) ) (neighbours g node_a) in aux_list neighbours (visited @ [node_a]) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x :: xs -> let fc2 = fun () -> aux_list xs visited fc sc in aux_node x visited fc2 sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun x -> x);; |
let find_all_paths (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) list = let rec aux_node ((node_a, node_w): 'a * weight) (visited : 'a list) : ('a list * weight) list = if List.mem node_a visited then [] else if node_a = b then [([node_a], node_w)] else;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in match paths with | [] -> None | x :: xs -> let rec find_max remaining_paths longest_path = match remaining_paths with | [] -> longest_path | p :: ps -> let new_longest = if snd p > snd longest_path then p else longest_path in find_max ps new_longest in Some (find_max xs x);; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let {nodes = _; edges = edgesList } = g in List.fold_left (fun listAcc (startV, endV, weight) -> if startV = vertex then (listAcc @ [(endV, weight)]) else listAcc) [] edgesList ;; |
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 (nodePath, totalCost) = aux_list (neighbours g (fst node)) ((fst node) :: visited) in ((fst node) :: nodePath, (snd node) + totalCost) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | h :: t -> try aux_node h visited with Fail -> aux_list t 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 ([fst node], snd node) else if List.mem (fst node) visited then fc () else let sc' = (fun (path, cost) -> sc ((fst node) :: path, (snd node) + cost)) in aux_list (neighbours g (fst node)) ((fst node) :: visited) fc sc' and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | h :: t -> let sc' = sc in let fc' = fun () -> aux_list t visited fc sc in aux_node h visited fc' sc' in aux_node (a, 0) [] (fun () -> raise Fail) (fun s -> s);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec findLongest allPaths longestPath longestLen : ('a list * weight) option = match allPaths with | [] -> Some longestPath | (path, w) :: t -> if w > longestLen then findLongest t (path, w) w else findLongest t longestPath longestLen in let paths = find_all_paths g a b in if (List.length paths) = 0 then None else findLongest paths ([], 0) (-1);; |
let new_status_manager (max_number_failures : int) : bank_account_status_manager = let counter = ref 0 and status = ref Unlocked in { show = (fun () -> !status); reset = ( fun () -> begin counter := 0; status := Unlocked end ); tick = ( fun () -> if !status = Unlocked then begin counter := !counter + 1; if !counter = max_number_failures then status := Locked; end ); } ;; |
let open_account (initial_pass : passwd) : bank_account = let balance = ref 0 in let pass = ref initial_pass in let status_manager = new_status_manager(5) in { update_pass = ( fun current_pass new_pass -> if !pass = current_pass then begin status_manager.reset(); pass := new_pass end else begin status_manager.tick(); raise wrong_pass end ); retrieve = ( fun used_pass amount -> match status_manager.show() with | Unlocked -> begin if !pass = used_pass then begin status_manager.reset(); if amount < 0 then raise negative_amount else if amount > !balance then raise not_enough_balance else balance := !balance - amount end else begin status_manager.tick(); raise wrong_pass end end | Locked -> raise too_many_failures ); deposit = ( fun used_pass amount -> match status_manager.show() with | Unlocked -> begin if !pass = used_pass then begin status_manager.reset(); if amount < 0 then raise negative_amount else balance := !balance + amount end else begin status_manager.tick(); raise wrong_pass end end | Locked -> raise too_many_failures ); show_balance = ( fun used_pass -> match status_manager.show() with | Unlocked -> begin if !pass = used_pass then begin status_manager.reset(); !balance end else begin status_manager.tick(); raise wrong_pass end end | Locked -> raise too_many_failures ); } ;; |
let neighbours (g : 'a graph) (vertex : 'a) : ('a * weight) list = List.map (fun (_, v, w) -> (v, w)) (List.filter (fun (u, _, _) -> (u = vertex)) g.edges);; |
let find_path (g : 'a graph) (a : 'a) (b : 'a) : ('a list * weight) = let rec aux_node ((c, v) : 'a * weight) (visited : 'a list) : ('a list * weight) = if c = b then ([b], v) else if not (List.mem c visited) then let (p, w) = aux_list (neighbours g c) (c::visited) in (c::p, w+v) else raise Fail and aux_list (nodes : ('a * weight) list) (visited : 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | node::rest -> try aux_node node visited with Fail -> aux_list rest visited in aux_node (a, 0) [];; |
let find_path' (g : 'a graph) (a : 'a) (b : 'a) : ('a list * weight) = let rec aux_node ((c, v) : 'a * weight) (visited : 'a list) (fc : unit -> ('a list * weight)) (sc : ('a list * weight) -> ('a list * weight)) : ('a list * weight) = if c = b then sc ([b], v) else if not (List.mem c visited) then aux_list (neighbours g c) (c::visited) fc ( fun (p, w) -> sc (c::p, v+w) ) else fc () and aux_list (nodes : ('a * weight) list) (visited : 'a list) (fc : unit -> ('a list * weight)) (sc : ('a list * weight) -> ('a list * weight)) : ('a list * weight) = 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 (p, w) -> (p, w));; |
let find_longest_path (g : 'a graph) (a : 'a) (b : 'a) : ('a list * weight) option = let paths = find_all_paths g a b in match paths with | [] -> None | _ -> Some ( List.fold_left ( fun (p, w) (q, v) -> if w > v then (p, w) else (q, v) ) ([], -1) paths );; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let numAttempts = ref 0 in let balance = ref 0 in let password_checker pass bank_account_func = if pass = !password then (numAttempts := 0; bank_account_func ()) else (numAttempts := !numAttempts + 1; raise wrong_pass) in let attempt_Checker pass bank_account_func = if !numAttempts = 5 then raise too_many_failures else password_checker pass bank_account_func in let update_pass currentPass newPass = password_checker currentPass (fun () -> password := newPass) in let retrieve pass cashOut = attempt_Checker pass (fun() -> if cashOut < 0 then raise negative_amount else if !balance < cashOut then raise not_enough_balance else if !balance >= cashOut then balance := !balance - cashOut) in let deposit pass cashIn = attempt_Checker pass (fun () -> if cashIn < 0 then raise negative_amount else balance := !balance + cashIn) in let show_balance pass = attempt_Checker pass (fun () -> !balance) in {update_pass; retrieve; deposit; show_balance} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = [];; |
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 ();; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.