text
stringlengths 0
601k
|
---|
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec helper (a: string) (b : string) (l: string list) (w: weight) (v: string list) (flag: bool): (string list * weight) = if flag then (l, w) else let theNeighbours = neighbours g a in match theNeighbours with | [] -> raise Fail | x::xs -> ( if (getT11 x) = b then helper (getT11 x) b ((getT11 x)::l) (w + (getT22 x)) v true else ( if( let inList a l = let search x = match x=a with | true -> true | false -> false in List.exists search v in inList (getT11 x) v ) then helper (getT11 x) b ((getT11 x)::l) (w + (getT22 x)) ((getT11 x) ::v) true else ( match xs with | [] -> raise Fail | y::ys -> ( helper (getT11 y) b ((getT11 y)::l) (w + (getT22 y)) ((getT11 y) :: v) true ) ) ) ) in helper a b [a] 0 [a] false;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = None;; |
let open_account (initial_pass: passwd) : bank_account = let pw = ref initial_pass in let log_counter = ref 0 in let bl = ref 0 in { update_pass = (fun opw npw -> if opw = !pw then (log_counter:=0; pw := npw) else (log_counter := !log_counter+1; raise wrong_pass)); retrieve = (fun opw amt -> if opw = !pw then if !log_counter < 5 then ( log_counter:=0; if amt<0 then raise negative_amount else if amt > !bl then raise not_enough_balance else bl := !bl-amt ) else raise too_many_failures else if !log_counter < 5 then ( log_counter := !log_counter+1; raise wrong_pass ) else raise too_many_failures ); deposit = (fun opw amt -> if opw = !pw then if !log_counter < 5 then ( log_counter:=0; if amt<0 then raise negative_amount else (bl := !bl+amt; log_counter:=0) ) else raise too_many_failures else if !log_counter < 5 then ( log_counter := !log_counter+1; raise wrong_pass ) else raise too_many_failures ); show_balance = (fun opw -> if opw = !pw then if !log_counter < 5 then (log_counter:=0;!bl) else raise too_many_failures else if !log_counter < 5 then ( log_counter := !log_counter+1; raise wrong_pass ) else raise too_many_failures ); } ;; let a = open_account "123";; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let get_fst (v1,_,_) = v1 in let p str tp = get_fst tp = str in let elist = List.filter (p vertex) (g.edges) in let f tp = let (_,v2,w) = tp in (v2,w) in List.map f elist ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let length = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (v1,w) = node in if v1 = b then (List.rev (v1::visited), !length+w) else if (List.mem v1 visited) then (length := !length+w; raise Fail) else (length := !length+w; aux_list (neighbours g v1) (v1::visited)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (v1,w)::t -> try aux_node (v1,w) visited with Fail -> (length := !length-w; aux_list t visited) in aux_list (neighbours g a) [a] ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let length = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight) = let (v1,w) = node in if v1 = b then sc ((visited@[v1]), !length+w) else if (List.mem v1 visited) then (length := !length+w; fc ()) else (length := !length+w; aux_list (neighbours g v1) (visited@[v1]) fc (fun (path,cost) -> (path,cost)) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (v1,w)::t -> (aux_node (v1,w) visited (fun () -> aux_list t visited fc sc) sc) in aux_node (a,0) [] (fun () -> raise Fail) (fun t -> t) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match (find_all_paths g a b) with | [] -> None | (p,w)::_ -> Some (p,w) ;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let amount = ref 0 in let count = ref 0 in {update_pass = (fun x y -> (if compare x !pass != 0 then (count := !count+1; raise wrong_pass) else (pass := y; count := 0))); retrieve = (fun l m -> (if !count > 4 then raise too_many_failures else if compare l !pass != 0 then (count := !count+1; raise wrong_pass) else (begin if m > !amount then raise not_enough_balance else if m < 0 then raise negative_amount else amount := !amount - m ; count :=0 end))); deposit = (fun i j -> (if !count > 4 then raise too_many_failures else if compare i !pass != 0 then (count := !count+1; raise wrong_pass) else if j < 0 then raise negative_amount else amount := !amount + j; count :=0)); show_balance = (fun v -> (if !count > 4 then raise too_many_failures else if compare v !pass != 0 then (count := !count+1; raise wrong_pass) else count :=0 ; !amount)); } ;; let a1 = { nodes = ["a"; "b"; "c"; "d"] ; edges = [("a", "b", 21); ("a", "d", 1); ("b", "c", 12); ("a", "c", 7)] } in (a1, "a") , [("b", 21); ("d", 1); ("c",7)] ); (let a2 = { nodes = ["a"; "b"; "c"; "d"] ; edges = [("a", "b", 21); ("a", "d", 1); ("b", "c", 12); ("a", "c", 7)] } in (a2, "c") , [] ); (let a3 = { nodes = ["a"; "b"; "c"; "d"] ; edges = [("a", "b", 21); ("a", "d", 1); ("b", "c", 12); ("a", "c", 7)] } in (a3, "b") , [("c", 12)] ); ];; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let n x y = let (a,b,c) = y in if compare a vertex == 0 then x @ [(b,c)] else x in List.fold_left n [] g.edges ;; let removeFirst (l: 'a list): ('a list)= match l with | [] -> [] | x :: xs -> xs let rec onlyFirst (l : ('a*weight) list) (m : 'a list) : 'a list = match l with | [] -> m | x :: xs -> onlyFirst xs (m @ [fst(x)]) let rec removeDouble (l : ('a*weight) list) (m : ('a*weight) list) : ('a*weight) list = match l with | [] -> m | x :: xs -> (if List.mem (fst(x)) (onlyFirst m []) then removeDouble xs m else removeDouble xs (m @ [x]) ) let rec find g goal start visited path acc = if visited == [] then (path, acc) else (if List.mem ((fst(List.nth visited 0)),(fst(goal)),(snd(goal))) g.edges then find g (List.nth visited 0) start (removeFirst visited) ((fst (List.nth visited 0)) :: path) (acc + snd(goal)) else find g goal start (removeFirst visited) path acc ) ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : ('a * weight) list): ('a list * weight) = if compare (fst(node)) b == 0 then find g node (a, 0) (List.rev (removeDouble visited [])) [fst(node)] 0 else if List.mem node visited then raise Fail else aux_list (neighbours g (fst(node))) (visited @ [node]) and aux_list (nodes: ('a * weight) list) (visited: ('a * weight) list): ('a list * weight) = match nodes with | [] -> raise Fail | x :: xs -> (try aux_node x visited with Fail -> aux_list xs visited) in aux_node (a, 0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : ('a*weight) list) fc sc : ('a list * weight) = if compare (fst(node)) b == 0 then sc node visited else if List.mem node visited then fc () else aux_list (neighbours g (fst(node))) (visited @ [node]) fc sc and aux_list (nodes: ('a * weight) list) (visited: ('a*weight) list) 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 aux_node x visited fail2 suc2 ) in aux_node (a, 0) [] (fun () -> raise Fail) (fun x y -> find g x (a, 0) (List.rev (removeDouble y [])) [fst(x)] 0);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let n = find_all_paths g a b in match n with | [] -> None | _ -> largest n (List.nth n 0);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let failure_count = ref 0 in let balance = ref 0 in { update_pass = (fun oldpassword newpassword -> if (oldpassword = !password) then (password := newpassword; failure_count := 0) else(failure_count := !failure_count + 1;(raise wrong_pass))); retrieve = (fun trypassword retrieveamount -> if (!failure_count > 4) then (raise too_many_failures) else if not (trypassword = !password) then (failure_count := !failure_count + 1; (raise wrong_pass)) else failure_count := 0;(if retrieveamount < 0 then (raise negative_amount) else (if retrieveamount< !balance then (balance := !balance - retrieveamount) else (raise not_enough_balance)))); deposit = (fun trypassword depositamount -> if (!failure_count > 4) then (raise too_many_failures) else if not (trypassword = !password) then (failure_count := !failure_count + 1; (raise wrong_pass)) else failure_count := 0; (if depositamount < 0 then (raise negative_amount) else(balance := !balance + depositamount))); show_balance = (fun trypassword -> if (!failure_count > 4) then (raise too_many_failures) else if not (trypassword = !password) then (failure_count := !failure_count + 1; (raise wrong_pass)) else failure_count := 0;(!balance)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edge l (b, c,d) = if b =vertex then (c,d) :: l else l in List.fold_left edge [] g.edges;; let graph1 = {nodes = ["Victoria"; "Hull"; "Brooks"; "Surrey "]; edges = [("Hull", "Brooks", 15); ("Victoria", "Hull", 12); ("Surrey ", "Hull", 1); ("Hull", "Surrey ", 14); ("Brooks", "Hull", 20); ("Surrey ", "Victoria", 7)]};; let mapper = List.map (fun (vertex,weight) -> (vertex, weight + 3)) (neighbours graph1 "Hull");; |
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,weight = node in let totalpath = if node = b then ([],0) else if not (List.mem node visited) then aux_list (neighbours g node) (node::visited) else raise Fail in let (path,cost) = totalpath in (node::path,cost+weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> (raise Fail) | (vertex,weight):: tl -> try aux_node (vertex,weight) visited with Fail -> aux_list tl visited in aux_node (a,0) [];; let graph9 = {nodes = ["Ferryland"; "Placentia"; "Malartic"; "Revelstoke"]; edges = [("Malartic", "Revelstoke", 9); ("Revelstoke", "Malartic", 4); ("Placentia", "Malartic", 11); ("Ferryland", "Revelstoke", 1); ("Malartic", "Ferryland", 16)]};; let graph8 = {nodes = ["Hay River"; "Revelstoke"; "Waterville"; "Whitehorse"; "Placentia"; "Tuktoyaktuk"; "Batoche"]; edges = [("Placentia", "Waterville", 19); ("Placentia", "Batoche", 14); ("Batoche", "Placentia", 1); ("Tuktoyaktuk", "Hay River", 6); ("Revelstoke", "Hay River", 17); ("Batoche", "Revelstoke", 18); ("Hay River", "Placentia", 8)]};; |
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,weight) = node in if node = b then sc ([node],weight) else if not (List.mem node visited) then aux_list (neighbours g node) (node::visited) (fc) (fun (totalpath,cost) -> sc (node::totalpath, (cost + weight))) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | (vertex,weight)::tl -> let succeed2 = sc in let fail2 = (fun () -> aux_list tl visited fc sc) in aux_node (vertex,weight) visited (fail2) (succeed2) | _ -> fc () in aux_node (a,0) [] (fun () -> raise Fail) (fun x -> x);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let failure_count = ref 0 in let balance = ref 0 in { update_pass = (fun oldpassword newpassword -> if (oldpassword = !password) then (password := newpassword; failure_count := 0) else(failure_count := !failure_count + 1;(raise wrong_pass))); retrieve = (fun trypassword retrieveamount -> if (!failure_count > 4) then (raise too_many_failures) else if not (trypassword = !password) then (failure_count := !failure_count + 1; (raise wrong_pass)) else failure_count := 0;(if retrieveamount < 0 then (raise negative_amount) else (if retrieveamount< !balance then (balance := !balance - retrieveamount) else (raise not_enough_balance)))); deposit = (fun trypassword depositamount -> if (!failure_count > 4) then (raise too_many_failures) else if not (trypassword = !password) then (failure_count := !failure_count + 1; (raise wrong_pass)) else failure_count := 0; (if depositamount < 0 then (raise negative_amount) else(balance := !balance + depositamount))); show_balance = (fun trypassword -> if (!failure_count > 4) then (raise too_many_failures) else if not (trypassword = !password) then (failure_count := !failure_count + 1; (raise wrong_pass)) else failure_count := 0;(!balance)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edge l (b, c,d) = if b =vertex then (c,d) :: l else l in List.fold_left edge [] g.edges;; let graph1 = {nodes = ["Victoria"; "Hull"; "Brooks"; "Surrey "]; edges = [("Hull", "Brooks", 15); ("Victoria", "Hull", 12); ("Surrey ", "Hull", 1); ("Hull", "Surrey ", 14); ("Brooks", "Hull", 20); ("Surrey ", "Victoria", 7)]};; let mapper = List.map (fun (vertex,weight) -> (vertex, weight + 3)) (neighbours graph1 "Hull");; |
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,weight = node in let totalpath = if node = b then ([],0) else if not (List.mem node visited) then aux_list (neighbours g node) (node::visited) else raise Fail in let (path,cost) = totalpath in (node::path,cost+weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> (raise Fail) | (vertex,weight):: tl -> try aux_node (vertex,weight) visited with Fail -> aux_list tl visited in aux_node (a,0) [];; let graph9 = {nodes = ["Ferryland"; "Placentia"; "Malartic"; "Revelstoke"]; edges = [("Malartic", "Revelstoke", 9); ("Revelstoke", "Malartic", 4); ("Placentia", "Malartic", 11); ("Ferryland", "Revelstoke", 1); ("Malartic", "Ferryland", 16)]};; let graph8 = {nodes = ["Hay River"; "Revelstoke"; "Waterville"; "Whitehorse"; "Placentia"; "Tuktoyaktuk"; "Batoche"]; edges = [("Placentia", "Waterville", 19); ("Placentia", "Batoche", 14); ("Batoche", "Placentia", 1); ("Tuktoyaktuk", "Hay River", 6); ("Revelstoke", "Hay River", 17); ("Batoche", "Revelstoke", 18); ("Hay River", "Placentia", 8)]};; |
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,weight) = node in if node = b then sc ([node],weight) else if not (List.mem node visited) then aux_list (neighbours g node) (node::visited) (fc) (fun (totalpath,cost) -> sc (node::totalpath, (cost + weight))) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | (vertex,weight)::tl -> let succeed2 = sc in let fail2 = (fun () -> aux_list tl visited fc sc) in aux_node (vertex,weight) visited (fail2) (succeed2) | _ -> fc () 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 rec maxpath curlist bestpath curmaxweight = match curlist with | [] -> bestpath | hd :: tl -> let (list, weight) = hd in if (weight >= curmaxweight) then (maxpath tl hd weight) else maxpath tl bestpath curmaxweight in let to_b = maxpath (find_all_paths g a b) ([],0) 0 in if (to_b = ([],0)) then None else Some to_b;; |
let open_account (initial_pass: passwd) : bank_account = let pw = ref initial_pass in let balance = ref 0 in let counter = ref 0 in let lock = ref false in { update_pass = (fun (pass : passwd) (new_pw : passwd) -> if not(pass = !pw) then ( if !counter >= 4 then lock := true ; counter := !counter + 1 ; raise (wrong_pass)) else (pw := new_pw ; lock := false ; counter := 0)) ; deposit = (fun (pass : passwd) (amount : int) -> if !lock then raise (too_many_failures) else if not(pass = !pw) then ( if !counter > 4 then lock := true ; (counter := !counter + 1 ; raise (wrong_pass))) else if amount < 0 then raise (negative_amount) else (balance := !balance + amount ; counter := 0) ) ; retrieve = (fun (pass : passwd) (amount : int) -> if !lock then raise (too_many_failures) else if not(pass = !pw) then (counter := !counter + 1 ; if !counter > 4 then lock := true ; raise (wrong_pass)) else if amount < 0 then raise (negative_amount) else if amount > !balance then raise (not_enough_balance) else (balance := !balance - amount ; counter := 0)) ; show_balance = (fun (pass : passwd) -> if !lock then raise (too_many_failures) else if not(pass = !pw) then (counter := !counter + 1 ; if !counter > 4 then lock := true ; raise (wrong_pass)) else (counter := 0 ; !balance) ) } ;; let isOutEdge v (e: 'a * 'a * weight) = let (i, o, w) = e in i = v;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun (e: 'a * 'a * weight) acc -> if isOutEdge vertex e then let (i,o,w) = e in (o,w)::acc else acc) g.edges [] ;; let compute_path (path: 'a list) : ('a list * weight) = let sum = ref 0 in let gPath = ref [] in let res = () List.iter (fun a -> let (n, w) = a in gPath := !gPath @ [n] ; sum := !sum + w ) path ; res = (!gPath, !sum) ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let path = ref [(a, 0)] in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let neighbourList = neighbours g a in match neighbourList with |((node:'a), (w: weight)):: x -> let ((n:'a), (w: weight)) = List.hd x in (path := !path @ [(node, w)]); aux_node (List.hd x) (visited @ [node]) |(z, _):: x -> let ((n:'a), (w: weight)) = List.hd x in aux_node (n, w) (visited @ [z] ) |[] -> compute_weight !path in aux_node (a, 0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let account_balance = ref 0 in let wrongattempts = ref 1 in let updatepass_fun oldpass newpass = if oldpass = !password then (password := newpass; wrongattempts := 1;) else (wrongattempts := !wrongattempts + 1; raise wrong_pass) in let retrive_fun inputpass amount = if !wrongattempts > 5 then raise too_many_failures else if inputpass = !password then (wrongattempts := 1; (if amount < 0 then raise negative_amount else if amount > !account_balance then raise not_enough_balance else account_balance := !account_balance - amount)) else (wrongattempts := !wrongattempts + 1; raise wrong_pass) in let deposit_fun inputpass amount = if !wrongattempts > 5 then raise too_many_failures else if inputpass = !password then (wrongattempts := 1; (if amount < 0 then raise negative_amount else account_balance := !account_balance + amount)) else (wrongattempts := !wrongattempts + 1; raise wrong_pass) in let showbal_fun inputpass = if !wrongattempts > 5 then raise too_many_failures else if inputpass = !password then (wrongattempts := 1; !account_balance) else (wrongattempts := !wrongattempts + 1; raise wrong_pass) in {update_pass = updatepass_fun; retrieve = retrive_fun; deposit = deposit_fun; show_balance = showbal_fun} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec find_outneighbours edges vertex acc = match edges with | [] -> acc | (v1, v2, w) :: xs -> (if v1 = vertex then find_outneighbours xs vertex ((v2, w) :: acc) else find_outneighbours xs vertex acc) in find_outneighbours g.edges vertex [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (x, w) = node in if List.mem x visited then raise Fail else let outneighbours = neighbours g x in let (path_nodes, total_weight) = aux_list outneighbours (x :: visited) in (x :: path_nodes, total_weight + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = let rec iterate_list nodes visited = match nodes with | [] -> raise Fail | (x, w) :: xs -> (if x = b then ([x], w) else try aux_node (x, w) visited with Fail -> iterate_list xs visited) in iterate_list nodes visited in if a = b then ([a], 0) else aux_node (a, 0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight) = let (x, w) = node in if List.mem x visited then fc () else let outneighbours = neighbours g x in aux_list outneighbours (x :: visited) fc (fun (r1, r2) -> sc (x :: r1, w + r2)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = let rec iterate_nodes nodes visited = match nodes with | [] -> fc () | (x, w) :: xs -> (if x = b then sc ([x], w) else try aux_node (x, w) visited fc sc with Fail -> iterate_nodes xs visited) in iterate_nodes nodes visited in aux_node (a, 0) [] (fun () -> raise Fail) (fun r -> r);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let allpaths = find_all_paths g a b in if allpaths = [] then None else let (x :: xs) = allpaths in let rec iterate_paths paths longest = match paths with | [] -> longest | y :: ys -> (let (p1, w1) = y in let (p2, w2) = longest in if w1 > w2 then iterate_paths ys y else iterate_paths ys longest) in Some (iterate_paths xs x);; |
let open_account (initial_pass: passwd) : bank_account = let bal = ref 0 in let pass = ref initial_pass in let count = ref 0 in { update_pass = (fun oldp newp -> if oldp = !pass then ( pass := newp ; count := 0) else (count := !count + 1 ; raise wrong_pass)) ; retrieve = (fun p amt -> if !count < 5 then if p = !pass then if amt < 0 then (count := 0 ; raise negative_amount) else if amt <= !bal then (bal := (!bal - amt) ; count := 0 ) else (count := 0 ; raise not_enough_balance) else (count := !count + 1 ; raise wrong_pass ) else ( count := !count + 1 ; raise too_many_failures )) ; deposit = (fun p amt -> if !count < 5 then if p = !pass then if amt < 0 then (count := 0 ; raise negative_amount) else (bal := (!bal + amt) ; count := 0 ) else (count := !count + 1 ; raise wrong_pass ) else ( count := !count + 1 ; raise too_many_failures )) ; show_balance = fun p -> if !count < 5 then if p = !pass then ( count := 0 ; !bal) else (count := !count + 1 ; raise wrong_pass ) else ( count := !count + 1 ; raise too_many_failures ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f vertex e acc = let (left_v,right_v, w) = e in if left_v = vertex then (right_v,w) :: acc else acc in List.fold_right (f vertex) g.edges [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (n,w) = node in let newW = w in if n = b then ([b] , w) else (if List.mem n visited then raise Fail else let (x,wei) = aux_list (neighbours g n)(n :: visited) in (n :: x , newW + wei) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail | h :: xs -> try aux_node h visited with Fail -> aux_list xs visited in aux_node (a,0) [] ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) succ fail : ('a list * weight) = let (n,w) = node in let newW = w in if n = b then succ ([b] , w) else (if List.mem n visited then fail () else let succ2 = fun (x, wei) -> succ (n :: x , newW + wei) in aux_list (neighbours g n)(n :: visited) succ2 fail ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) succ fail : ('a list * weight) = match nodes with |[] -> fail () | h :: xs -> let fail2 = fun () -> aux_node h visited succ fail in aux_list xs visited succ fail2 in aux_node (a,0) [] (fun x -> x) (fun () -> raise Fail) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec traverse (paths : ('a list * weight) list) = match paths with |[] -> None |[(x,wei)] -> Some (x,wei) |(x,w1):: (y, w2) :: xs -> if w1 >= w2 then traverse ((x,w1) :: xs) else traverse ((y,w2) :: xs) in traverse (find_all_paths g a b);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let failure = ref 0 in let balance = ref 0 in let failure_fun() = failure := (!failure + 1); raise wrong_pass in let update_pass (old_pass: passwd) (new_pass: passwd) = match old_pass = !password with | true -> password := new_pass ; failure := 0; | _ -> failure_fun() in let retrieve (pass: passwd) (amount: int)= match !failure > 4 with | true -> raise too_many_failures | _ -> match pass = !password with | true -> failure := 0; (match amount >= 0 with | false -> raise negative_amount | true -> (match !balance >= amount with | true -> balance := !balance - amount | false -> raise not_enough_balance )) | _ -> failure_fun() in let deposit (pass: passwd) (amount: int) = match !failure > 4 with | true -> raise too_many_failures | _ -> match pass = !password with | true -> failure := 0; (match amount >= 0 with | false -> raise negative_amount | true -> balance := !balance + amount ) | _ -> failure_fun() in let show_balance (pass: passwd) : int = match !failure > 4 with | true -> raise too_many_failures | _ -> match pass = !password with | true -> failure := 0; !balance | _ -> failure_fun() in let (b:bank_account) = { update_pass = update_pass; retrieve = retrieve; deposit = deposit; show_balance = show_balance; } in b ;; let helper (vertex: 'a) (list: ('a * weight) list) (v:('a * 'a * weight)): ('a * weight) list = let (a,b,w) = v in match a = vertex with | true -> (b,w) :: list | _ -> list;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let { nodes = _; edges = e } = g in List.fold_left (helper vertex) [] e;; |
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 (vertex, w) = node in match List.mem vertex visited with | true -> raise Fail | false -> match vertex = b with | true -> ((visited @ [vertex]), weight + w) | false -> aux_list (neighbours g vertex) (visited @ [vertex]) (weight + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (weight : int): ('a list * weight) = match nodes with | [] -> raise Fail | x :: xs -> try aux_node x visited weight with Fail -> aux_list xs visited weight in aux_node (a,0) [] 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (weight : int) fc sc : ('a list * weight)= let (vertex, w) = node in match List.mem vertex visited with | true -> fc() | false -> match vertex = b with | true -> sc ((visited @ [vertex]), weight + w) | false -> aux_list (neighbours g vertex) (visited @ [vertex]) (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() | x :: xs -> let fc2 = (fun() -> aux_list xs visited weight fc sc) in aux_node x visited weight fc2 sc in aux_node (a,0) [] 0 (fun() -> raise Fail) (fun x -> x);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec aux_node (node: 'a * weight) (visited : 'a list) (weight : int) fc : ('a list * weight) list = let (vertex, w) = node in match List.mem vertex visited with | true -> fc() | false -> match vertex = b with | true -> ((visited @ [vertex]), weight + w) :: aux_list (neighbours g vertex) (visited @ [vertex]) (weight + w) fc | false -> aux_list (neighbours g vertex) (visited @ [vertex]) (weight + w) fc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (weight : int) fc : ('a list * weight) list = match nodes with | [] -> fc() | x :: xs -> let fc2 = (fun() -> aux_list xs visited weight fc ) in aux_node x visited weight fc2 in let list = aux_node (a,0) [] 0 (fun() -> []) in let compare (a1 : 'a list * weight) (a2 : 'a list * weight) : ('a list * weight) = let (_,d1) = a1 in let (_,d2) = a2 in match d1 > d2 with | true -> a1 | false -> a2 in match list with | [] -> None | _ -> Some (List.fold_left compare ([],0) list);; |
let open_account (initial_pass: passwd) : bank_account = let current_bal = ref 0 in let current_pass = ref initial_pass in let wrong_att = ref 0 in let update_pass (old_pass : passwd) (new_pass : passwd) : unit = if old_pass = !current_pass then (current_pass := new_pass; wrong_att := 0) else (incr wrong_att; raise wrong_pass) in let deposit (pass : passwd) (amount : int) : unit = if !wrong_att >= 5 then raise too_many_failures else if pass = !current_pass then (wrong_att := 0; if amount >= 0 then current_bal := amount else raise negative_amount ) else (incr wrong_att; raise wrong_pass) in let retrieve (pass: passwd) (take : int) : unit = if !wrong_att >= 5 then raise too_many_failures else if pass = !current_pass then (wrong_att := 0; if 0 <= take && take <= !current_bal then current_bal := !current_bal - take else if take > !current_bal then raise not_enough_balance else raise negative_amount ) else (incr wrong_att; raise wrong_pass) in let show_balance (pass : passwd) : int = if !wrong_att >= 5 then raise too_many_failures else if pass = !current_pass then (wrong_att := 0; !current_bal) else (incr wrong_att; raise wrong_pass) in {update_pass = update_pass; retrieve = retrieve; deposit = deposit; show_balance = show_balance; } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec check_edges e_list v final = match e_list with |(a, b, weight)::tl -> if a = v then check_edges tl vertex (final@[(b,weight)]) else check_edges tl vertex final |[] -> final in check_edges g.edges vertex [] exception Fail1 of string;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec helper edges start finish path tot_weight = match edges with |[]-> raise Fail |(u,v,weight)::tl -> if u = start && v = finish && not (List.mem v path) then (path@[v], tot_weight + weight) else if u = start && not (List.mem v path) then try helper g.edges v finish (path@[v]) (tot_weight + weight) with Fail -> helper tl start finish (path) (tot_weight) else helper tl start finish (path) (tot_weight) in helper g.edges a b [a] 0 ;; let g1 = { nodes = [1;2;3;4;5] ; edges = [(1,2,1);(3,2,1);(1,3,1);(3,4,1);(5,3,1);(2,4,1);(5,4,1);(4,6,1)] } let g2 = { nodes = [1;2;3;4] ; edges = [(4,1,1);(2,1,1);(3,1,1);(1,3,1);(3,4,1)] } let g3 = { nodes = [1;2] ; edges = [(1,2,1)]; };; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (tot_weight : weight) fc : ('a list * weight)= if b <> fst node then aux_list (neighbours g (fst node)) visited tot_weight fc else (List.rev visited, tot_weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (tot_weight : weight) fc : ('a list * weight) = match nodes with | [] -> fc () |h::tl -> if List.mem (fst h) visited then aux_list tl visited tot_weight fc else let h' = (fst h)::visited in let tot_weight' = tot_weight + (snd h) in aux_node h h' tot_weight' (fun()-> aux_list tl visited tot_weight fc) in aux_list (neighbours g a) [a] 0 (fun() -> raise Fail) let test_one_path (g: 'a graph) (a: 'a) (b: 'a) used: ('a list * weight) = let rec helper edges start finish path tot_weight used = match edges with |[]-> raise Fail |(u,v,weight)::tl -> if u = start && v = finish && not (List.mem v path) && not (List.mem (path@[v], tot_weight + weight) used) then (path@[v], tot_weight + weight) else if u = start && not (List.mem v path) then try helper g.edges v finish (path@[v]) (tot_weight + weight) used with Fail -> helper tl start finish (path) (tot_weight) used else helper tl start finish (path) (tot_weight) used in helper g.edges a b [a] 0 used;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let max_len = ref 0 in let max_path = ref [] in let rec move_thru paths = match paths with | (path, weight)::tl -> if weight > !max_len then (max_len := weight; max_path := path; move_thru tl) else move_thru tl | [] -> if !max_len = 0 then None else Some (!max_path, !max_len) in move_thru (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 counter = ref 0 in { update_pass = (fun old new_ -> if old = !password then (password := new_; counter := 0) else (counter := succ !counter ; raise wrong_pass)) ; retrieve = (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 if !balance >= amount then balance := !balance - amount else raise not_enough_balance ) else (counter := succ !counter; 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 := succ !counter ; 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 := succ !counter; raise wrong_pass)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun a (v0, v1, w) -> if v0 = vertex then (v1, w)::a else a) [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((c, w): 'a * weight) ((v_list, v_weight): 'a list * weight) : ('a list * weight) = if c = b then (v_list@[c], w + v_weight) else if List.exists (fun e -> e = c) v_list then raise Fail else aux_list (neighbours g c) (v_list@[c], w + v_weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list * weight) : ('a list * weight) = match nodes with | [] -> raise Fail | h::t -> try aux_node h visited with Fail -> aux_list t visited 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, w): 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if node = b then sc ([node], w) else if List.exists (fun e -> e = node) visited then fc () else aux_list (neighbours g node) (node::visited) fc (fun (next_node, next_weight) -> sc (node::next_node, w + next_weight)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | h::t -> let sc2 = sc in let fc2 = (fun () -> aux_list t visited fc sc) in aux_node h visited fc2 sc2 in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun (next_node, next_weight) -> (a::next_node, next_weight));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let lp = (find_all_paths g a b) |> List.fold_left (fun (l1, w1) (l2,w2) -> if w1 > w2 then (l1, w1) else (l2, w2)) ([], -1) in if (snd lp) != -1 then Some(lp) else None;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in { update_pass = (fun password_prov new_pass -> if password_prov = !password then let _ = (attempts := 0) in password := new_pass else let _ = attempts := !attempts+1 in raise wrong_pass); deposit = (fun password_prov deposit -> if !attempts = 5 then raise too_many_failures else if deposit < 0 then raise negative_amount else if password_prov = !password then let _ = (attempts := 0) in balance := !balance + deposit else let _ = attempts := !attempts+1 in raise wrong_pass); retrieve = (fun password_prov amount -> if !attempts = 5 then raise too_many_failures else if amount < 0 then raise negative_amount else if password_prov = !password then let _ = (attempts := 0) in if !balance - amount < 0 then raise not_enough_balance else balance := !balance - amount else let _ = attempts := !attempts+1 in raise wrong_pass); show_balance = (fun password_prov -> if !attempts = 5 then raise too_many_failures else if password_prov = !password then let _ = (attempts := 0) in !balance else let _ = attempts := !attempts+1 in raise wrong_pass); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec list_neighbours nodes n cont = match nodes with | [] -> cont [] | (v1, v2, w) :: t -> if v1 = n then list_neighbours t n (fun f -> cont ((v2, w) :: f)) else list_neighbours t n cont in list_neighbours g.edges vertex (fun f -> f) ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) total_weight: ('a list * weight) = let (cur_node, cur_weight) = node in if(cur_node = b) then (List.rev (cur_node::visited), total_weight+cur_weight) else if List.exists ((=) cur_node) visited then raise Fail else let cur_neighbours = neighbours g cur_node in match cur_neighbours with |[] -> raise Fail |_ -> aux_list cur_neighbours (cur_node :: visited) (total_weight + cur_weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) total_weight: ('a list * weight) = match nodes with | [] -> raise Fail | hd :: tl -> try aux_node hd visited total_weight with Fail -> aux_list tl visited total_weight in let list = neighbours g a in aux_list list [a] 0 ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) total_weight fc sc : ('a list * weight)= let (cur_node, cur_weight) = node in if(cur_node = b) then sc (List.rev (cur_node::visited), total_weight+cur_weight) else if List.exists ((=) cur_node) visited then fc () else let cur_neighbours = neighbours g cur_node in match cur_neighbours with |[] -> fc () |_ -> aux_list cur_neighbours (cur_node :: visited) (total_weight + cur_weight) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) total_weight fc sc : ('a list * weight) = match nodes with | [] -> fc () |hd :: tl -> let fc2 = fun () -> aux_list tl visited total_weight fc sc in aux_node hd visited total_weight fc2 sc in let list = neighbours g a in aux_list list [a] 0 (fun () -> raise Fail) (fun u -> u) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let comparator (cur_list, cur_weight) max_el = match max_el with |None -> Some(cur_list, cur_weight) |Some(max_list, max_weight) -> if max_weight > cur_weight then Some(max_list, max_weight) else Some(cur_list, cur_weight) in List.fold_right comparator (find_all_paths g a b) None ;; |
let open_account (initial_pass: passwd) : bank_account = let (password: passwd ref) = ref initial_pass in let (balance : int ref) = ref 0 in let (attempts : int ref) = ref 0 in let passcheck (pass: passwd) = attempts := !attempts + 1; if !password <> pass then raise wrong_pass else attempts := 0 in let failcheck = if !attempts > 5 then raise too_many_failures in let negativecheck (amount: int) = if amount < 0 then raise negative_amount in let retrievecheck (amount: int) = if !balance - amount < 0 then raise not_enough_balance in let attemptcheck (attempt: int ref) = if !attempt >= 5 then raise too_many_failures in let update_pass initial_pass (new_pass: passwd) : unit = passcheck initial_pass; password := new_pass in let deposit pass (amount: int) : unit = attemptcheck attempts; failcheck; passcheck pass; negativecheck amount; balance := !balance + amount in let retrieve pass (amount: int) : unit = attemptcheck attempts; failcheck; passcheck pass; negativecheck amount; retrievecheck amount; balance := !balance - amount in let show_balance pass : int = attemptcheck attempts; failcheck; passcheck pass; !balance in {update_pass; deposit; retrieve; show_balance} ;; let testgraph = {nodes = ["Souris"; "Whitehorse"; "Vancouver"; "Bonavista"; "Saguenay"; "Placentia"]; edges = [("Souris", "Bonavista", 3); ("Whitehorse", "Placentia", 6); ("Whitehorse", "Saguenay", 10); ("Placentia", "Saguenay", 7); ("Whitehorse", "Vancouver", 17); ("Vancouver", "Placentia", 6); ("Vancouver", "Bonavista", 8)]} in [ ( (testgraph, "Whitehorse") , (["Saguenay", 10; "Vancouver", 17; "Placentia", 6]) ); ( (testgraph, "Souris") , (["Bonavista", 3]) ); ( (testgraph, "Saguenay") , ([]) ) ];; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc (x,y,z) -> if x = vertex then (y,z) :: acc else acc) [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (x,y) = node in if b = x then ([x],y) else if not (List.exists (fun z -> x = z) visited) then let (path, weight) = aux_list (neighbours g x) (x :: visited) in (x :: path, weight + y) else 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 * weight) (visited : 'a list) fc sc : ('a list * weight) = let (x,y) = node in if b = x then sc ([x],y) else if not (List.exists (fun z -> x = z) visited) then aux_list (neighbours g x) (x :: visited) fc (fun (path, weight) -> sc (x :: path, weight + y)) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x :: xs -> let sc2 = sc in let fc2 = fun () -> aux_list xs visited fc sc in aux_node x 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 rec finder paths a b = match paths with | [] -> Some (a, b) | (x,y) :: xs -> if y >= b then finder xs x y else finder xs a b in let paths = find_all_paths g a b in if paths = [] then None else finder paths [] 0 ;; |
let open_account (initial_pass: passwd) : bank_account = let right_passwd = ref initial_pass in let attempts = ref 0 in let balance = ref 0 in let new_account:bank_account={ update_pass = (fun (old_one:passwd)(new_one:passwd)-> if old_one= right_passwd.contents then begin attempts:=0; right_passwd:=new_one end else begin attempts.contents<-attempts.contents+1; raise wrong_pass end ); retrieve = (fun (entered_passwd:passwd) (money:int)-> if attempts.contents >= 5 then raise too_many_failures else if entered_passwd = right_passwd.contents then begin attempts:=0; (if money >= 0 then if money <= balance.contents then balance.contents<-balance.contents-money else raise not_enough_balance else raise negative_amount) end else begin attempts.contents<-attempts.contents+1; raise wrong_pass end ); deposit = (fun entered_passwd money -> if attempts.contents >= 5 then raise too_many_failures else if entered_passwd = right_passwd.contents then if money>= 0 then begin attempts:=0; balance.contents<-balance.contents+money end else raise negative_amount else begin attempts.contents<-attempts.contents+1; raise wrong_pass end ); show_balance = (fun entered_passwd -> if attempts.contents >= 5 then raise too_many_failures else if entered_passwd = right_passwd.contents then begin attempts:=0; balance.contents end else begin attempts.contents<-attempts.contents+1; raise wrong_pass end ); } in new_account ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper edges output = match edges with | [] -> output | (v1,v2,distance) :: others -> if v1=vertex then helper others ((v2,distance)::output) else helper others output in helper (g.edges) [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux visited vertices path acc = match vertices with | [] -> raise Fail | (x,weight)::rest -> if List.mem x visited then aux visited rest path acc else if x=b then ((path@[b]),acc + weight) else try aux (x::visited) rest path acc with Fail -> aux (x::visited) (neighbours g x) (path@[x]) (acc+weight) in aux [] (neighbours g a) [a] 0 ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux visited vertices path suc = match vertices with | [] -> raise Fail | (x,weight)::rest -> if List.mem x visited then aux visited rest path suc else if x=b then ((path@[b]),suc weight) else try aux (x::visited) rest path suc with Fail -> let suc2 = fun r -> suc (r+weight) in aux (x::visited) (neighbours g x) (path@[x]) (suc2) in aux [] (neighbours g a) [a] (fun n -> n);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper (all : ('a list * weight) list) longest weight = match all with | [] -> Some longest | (l,w)::rest -> if w >= weight then helper rest (l,w) w else helper rest longest weight in let all_paths = find_all_paths g a b in match all_paths with | [] -> None | _ -> helper all_paths ([],0) 0;; |
let helper x = if x>=5 then raise too_many_failures;; |
let open_account (initial_pass:passwd): bank_account = let pw = ref initial_pass in let count = ref 0 in let balance = ref 0 in {update_pass=(fun old_pw new_pw -> if old_pw = !pw then (pw:= new_pw ; count := 0) else if !count >= 5 then raise wrong_pass else (count := (!count +1) ; raise wrong_pass)); deposit =(fun input_pw dep_amount -> helper !count ; if input_pw <> !pw then (count:= (!count +1); raise wrong_pass) else if dep_amount < 0 then raise negative_amount else (balance := (!balance+ dep_amount) ; count:=0)); retrieve =(fun input_pw ret_amount -> helper !count; if input_pw <> !pw then (count:= (!count +1); raise wrong_pass ) else if ret_amount < 0 then raise negative_amount else if ret_amount > !balance then raise not_enough_balance else (balance := (!balance - ret_amount) ; count:=0)) ; show_balance= (fun input_pw -> helper !count; if input_pw <> !pw then (count:= (!count +1); raise wrong_pass) else (count :=0 ; !balance)) ; } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper node (edge_list: ('a * 'a * weight) list) lst = match edge_list with |(v1, v2 ,w):: tl -> if v1 = node then helper node tl ((v2,w) :: lst) else helper node tl lst |[] -> lst in helper vertex g.edges [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (g: 'a graph) (nodes: ('a * weight) list) (dest: 'a) (visited: 'a list) (acc:int): ('a list * weight) = match nodes with |[] -> raise Fail |hd::tl -> let (v,w) = hd in if v = dest then (List.rev(dest::visited),acc+w) else try if (List.mem v visited) then aux_node g tl dest visited acc else aux_node g (neighbours g v) dest (v::visited) (acc+w) with Fail -> aux_node g tl dest visited acc in aux_node g (neighbours g a) b [a] 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (g: 'a graph) (nodes: ('a * weight) list) (dest: 'a) (visited: 'a list) (acc:int) fc sc: ('a list * weight) = match nodes with |[] -> fc () |hd::tl -> let (v,w) = hd in if v = dest then sc (List.rev(dest::visited),acc+w) else let suc2 = sc in let fail2 = fun()-> aux_node g tl dest visited acc fc sc in if (List.mem v visited) then aux_node g tl dest visited acc fail2 suc2 else aux_node g (neighbours g v) dest (v::visited) (acc+w) fail2 suc2 in aux_node g (neighbours g a) b [a] 0 (fun () -> raise Fail) (fun x -> x);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let lst = find_all_paths g a b in let rec helper lst max (answer: ('a list * weight) option) = match lst with |[] -> answer |hd::tl -> let (_,weight) = hd in if weight>max then let answer = Some hd in let max = weight in helper tl max answer else helper tl max answer in helper lst 0 None;; |
let open_account (initial_pass: passwd) : bank_account = let failure_counter = ref 0 in let password = ref initial_pass in let balance = ref 0 in {update_pass = (fun passwd new_passwd -> if passwd <> !password then raise wrong_pass else begin failure_counter := 0; password := new_passwd end ); retrieve = (fun passwd amount -> if !failure_counter >= 5 then raise too_many_failures else begin if passwd <> !password then begin incr failure_counter; raise wrong_pass; end else begin failure_counter := 0; if amount < 0 then raise negative_amount else let new_balance = !balance - amount in if new_balance < 0 then raise not_enough_balance else balance := new_balance end end ); deposit = (fun passwd amount -> if !failure_counter >= 5 then raise too_many_failures else begin if passwd <> !password; then begin incr failure_counter; raise wrong_pass; end else begin failure_counter := 0; if amount < 0 then raise negative_amount else balance := !balance + amount end end ); show_balance = (fun passwd -> if !failure_counter >= 5 then raise too_many_failures else begin if passwd <> !password then begin incr failure_counter; raise wrong_pass; end else failure_counter := 0; !balance end ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun l (n1, n2, w) -> if n1 = vertex then (n2, w)::l else l) [] 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_input, w_input) = node in let neighbour_list = neighbours g n_input in aux_list neighbour_list visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight)= match nodes with |[]-> raise Fail |(n,w)::rest->if List.mem n visited then aux_list rest visited else if n=b then (List.append visited [b], 0) else try aux_node (n,w) (n::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 (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (n_input, w_input) = node in let neighbour_list = neighbours g n_input in aux_list neighbour_list visited 0 0 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[]-> raise Fail |(n,w)::rest->if List.mem n visited then aux_list rest visited 0 0 else if n=b then (List.append visited [b], 0) else try aux_node (n,w) (n::visited) 0 0 with Fail -> aux_list rest visited 0 0 in aux_node (a,0) [] 0 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 counter = ref 0 in let password = ref initial_pass in let balance = ref 0 in {update_pass = (fun oldpwd newpwd -> if oldpwd = !password then let _ = (counter := 0) in (password := newpwd) else let _ = (counter := (!counter + 1)) in raise wrong_pass) ; deposit=(fun pwd amount -> if pwd = !password && (!counter < 5) then let _ = (counter := 0) in if amount < 0 then raise negative_amount else balance := (!balance + amount) else let _ = (counter := (!counter + 1)) in if !counter >= 6 then raise too_many_failures else raise wrong_pass); retrieve = (fun pwd amount -> if pwd = !password && (!counter<5) then let _ = (counter := 0) in if amount < 0 then raise negative_amount else if amount >= 0 && (!balance >= amount) then balance := (!balance - amount) else raise not_enough_balance else let _ = (counter := (!counter + 1)) in if !counter >= 6 then raise too_many_failures else raise wrong_pass); show_balance = (fun pwd -> if pwd = !password && (!counter < 5) then let _ = (counter := 0) in !balance else let _ = (counter := (!counter + 1)) in if !counter >= 6 then raise too_many_failures else raise wrong_pass) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec search l n= match l with |x::xs-> (let (v,v1,w) = x in if v = vertex then search xs ((v1,w) :: n) else if v != vertex then search xs n else search xs n) |_ -> n in search g.edges [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (w: weight) : ('a list * weight) = if List.mem (fst(node)) visited then raise Fail else if fst(node) = b then (List.rev(fst(node)::visited),(w+snd(node))) else (aux_list (neighbours g (fst(node))) (fst(node)::visited) (w+snd(node))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w: weight): ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try aux_node x visited w with Fail -> aux_list xs visited w 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) w fc sc : ('a list * weight)= if List.mem (fst(node)) visited then fc() else if fst(node) = b then (List.rev(fst(node)::visited),(w+snd(node))) else (aux_list (neighbours g (fst(node))) (fst(node)::visited) (w+snd(node)) fc sc) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w:weight) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> let sc2=sc in let fc2 = fun() -> aux_node x visited w fc sc in aux_list xs visited w fc2 sc2 in aux_list (neighbours g a) (a::[]) 0 (fun () -> raise Fail) (fun r -> r);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = if List.length(find_all_paths g a b) = 0 then None else let rec compare path longest j i= match path with | [] -> Some(List.nth (find_all_paths g a b) i) | x::xs -> if snd(x) <= longest then compare xs longest (j+1) i else compare xs (snd(x)) (j+1) (j) in compare (find_all_paths g a b) 0 0 0;; |
let open_account (initial_pass: passwd) : bank_account = let counter = ref 0 in let password = ref initial_pass in let balance = ref 0 in {update_pass = (fun oldpwd newpwd -> if oldpwd = !password then let _ = (counter := 0) in (password := newpwd) else let _ = (counter := (!counter + 1)) in raise wrong_pass) ; deposit=(fun pwd amount -> if pwd = !password && (!counter < 5) then let _ = (counter := 0) in if amount < 0 then raise negative_amount else balance := (!balance + amount) else let _ = (counter := (!counter + 1)) in if !counter >= 6 then raise too_many_failures else raise wrong_pass); retrieve = (fun pwd amount -> if pwd = !password && (!counter<5) then let _ = (counter := 0) in if amount < 0 then raise negative_amount else if amount >= 0 && (!balance >= amount) then balance := (!balance - amount) else raise not_enough_balance else let _ = (counter := (!counter + 1)) in if !counter >= 6 then raise too_many_failures else raise wrong_pass); show_balance = (fun pwd -> if pwd = !password && (!counter < 5) then let _ = (counter := 0) in !balance else let _ = (counter := (!counter + 1)) in if !counter >= 6 then raise too_many_failures else raise wrong_pass) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec search l n= match l with |x::xs-> (let (v,v1,w) = x in if v = vertex then search xs ((v1,w) :: n) else if v != vertex then search xs n else search xs n) |_ -> n in search g.edges [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (w: weight) : ('a list * weight) = if List.mem (fst(node)) visited then raise Fail else if fst(node) = b then (List.rev(fst(node)::visited),(w+snd(node))) else (aux_list (neighbours g (fst(node))) (fst(node)::visited) (w+snd(node))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w: weight): ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try aux_node x visited w with Fail -> aux_list xs visited w 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) w fc sc : ('a list * weight)= if List.mem (fst(node)) visited then fc() else if fst(node) = b then (List.rev(fst(node)::visited),(w+snd(node))) else (aux_list (neighbours g (fst(node))) (fst(node)::visited) (w+snd(node)) fc sc) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w:weight) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> let sc2=sc in let fc2 = fun() -> aux_node x visited w fc sc in aux_list xs visited w fc2 sc2 in aux_list (neighbours g a) (a::[]) 0 (fun () -> raise Fail) (fun r -> r);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = if List.length(find_all_paths g a b) = 0 then None else let rec compare path longest j i= match path with | [] -> Some(List.nth (find_all_paths g a b) i) | x::xs -> if snd(x) <= longest then compare xs longest (j+1) i else compare xs (snd(x)) (j+1) (j) in compare (find_all_paths g a b) 0 0 0;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let pass_attempts = ref 0 in { update_pass = (fun old_p new_p -> if old_p = !password then (pass_attempts := 0; password := new_p) else (pass_attempts:= !pass_attempts + 1; raise wrong_pass)); deposit = (fun pass amount -> if !pass_attempts > 4 then raise too_many_failures else if pass = !password then (pass_attempts := 0; (if amount < 0 then raise negative_amount else balance := !balance + amount )) else (pass_attempts := !pass_attempts + 1; raise wrong_pass)); retrieve = (fun pass amount -> if !pass_attempts > 4 then raise too_many_failures else if pass = !password then (pass_attempts := 0; if amount < 0 then raise negative_amount else if amount > !balance then raise not_enough_balance else balance := !balance - amount) else (pass_attempts := !pass_attempts + 1; raise wrong_pass)); show_balance = (fun pass -> if !pass_attempts > 4 then raise too_many_failures else if pass = !password then (pass_attempts := 0; !balance) else (pass_attempts := !pass_attempts + 1; raise wrong_pass)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun (in_n, out_n, w) l -> if in_n = vertex then (out_n, w) :: l else l) g.edges [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec process_node (node: 'a * weight) (visited : 'a list) (w_acc: int): ('a list * weight) = let (value, weight) = node in if value = b then (List.rev (value::visited), w_acc+weight) else if List.mem value visited then raise Fail else process_list (neighbours g value) (value::visited) (w_acc + weight) and process_list (nodes: ('a * weight) list) (visited: 'a list) (w_acc: int): ('a list * weight) = match nodes with | [] -> raise Fail | hd::nodes -> try process_node hd visited w_acc with Fail -> process_list nodes visited w_acc in process_list (neighbours g a) [a] 0 ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec process_node (node: 'a * weight) (visited : 'a list) fc (w_acc: int): ('a list * weight)= let (value, weight) = node in if value = b then (List.rev (value::visited), w_acc+weight) else if List.mem value visited then fc () else process_list (neighbours g value) (value::visited) fc (w_acc + weight) and process_list (nodes: ('a * weight) list) (visited: 'a list) fc (w_acc: int): ('a list * weight) = match nodes with | [] -> fc () | hd::nodes -> let fc2 = fun () -> process_list nodes visited fc w_acc in process_node hd visited fc2 w_acc in process_list (neighbours g a) [a] (fun () -> raise Fail) 0 ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in let max = 0 in let final_path = None in let rec find_max paths max final_path= match paths with | [] -> final_path | hd::remaining -> let (path, weight) = hd in if weight > max then find_max remaining weight (Some hd) else find_max remaining max final_path in find_max paths max final_path ;; |
let open_account (initial_pass: passwd) : bank_account = let current_pass = ref initial_pass in let current_balance = ref 0 in let pass_count = ref 0 in {update_pass = (fun pass new_pass -> if pass = !current_pass then begin current_pass := new_pass; pass_count := 0 end else begin pass_count := !pass_count + 1; raise wrong_pass end); retrieve = (fun pass amount -> if !pass_count >= 5 then raise too_many_failures else if pass = !current_pass then begin pass_count := 0; if amount < 0 then raise negative_amount else if amount > !current_balance then raise not_enough_balance else current_balance := !current_balance - amount end else begin pass_count := !pass_count + 1; raise wrong_pass end); deposit = (fun pass amount -> if !pass_count >= 5 then raise too_many_failures else if pass = !current_pass then begin pass_count := 0; if amount < 0 then raise negative_amount else current_balance := !current_balance + amount end else begin pass_count := !pass_count + 1; raise wrong_pass end); show_balance = (fun pass -> if !pass_count >= 5 then raise too_many_failures else if pass = !current_pass then begin pass_count := 0; !current_balance end else begin pass_count := !pass_count + 1; raise wrong_pass end)} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let find_edge l (x, y, w) = if x = vertex then (y, w) :: l else l in List.fold_left find_edge [] g.edges ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((node, weight) : 'a * weight) (visited : 'a list) : ('a list * weight) = if node = b then ([node], weight) else if List.mem node visited then raise Fail else let (path, cost) = aux_list (neighbours g node) (node :: visited) in (node :: path, cost + weight) 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, weight) : 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if node = b then sc ([node], weight) else if List.mem node visited then fc () else let sc2 = fun (path, cost) -> sc (node :: path, cost + weight) in aux_list (neighbours g node) (node :: visited) fc sc2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | h :: t -> let fc2 = (fun () -> aux_list t visited fc sc) in aux_node h visited fc2 sc in aux_node (a,0) [] (fun () -> raise Fail) (fun x -> x) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in match paths with | [] -> None | h :: t -> let rec longest_path paths ((longest, max_cost) : 'a list * weight) : 'a list * weight = match paths with | [] -> (longest, max_cost) | (path, cost) :: t -> if cost > max_cost then longest_path t (path, cost) else longest_path t (longest, max_cost) in Some (longest_path paths h) ;; |
let open_account (initial_pass: passwd) : bank_account = let current_pass = ref initial_pass in let current_balance = ref 0 in let pass_count = ref 0 in {update_pass = (fun pass new_pass -> if pass = !current_pass then begin current_pass := new_pass; pass_count := 0 end else begin pass_count := !pass_count + 1; raise wrong_pass end); retrieve = (fun pass amount -> if !pass_count >= 5 then raise too_many_failures else if pass = !current_pass then begin pass_count := 0; if amount < 0 then raise negative_amount else if amount > !current_balance then raise not_enough_balance else current_balance := !current_balance - amount end else begin pass_count := !pass_count + 1; raise wrong_pass end); deposit = (fun pass amount -> if !pass_count >= 5 then raise too_many_failures else if pass = !current_pass then begin pass_count := 0; if amount < 0 then raise negative_amount else current_balance := !current_balance + amount end else begin pass_count := !pass_count + 1; raise wrong_pass end); show_balance = (fun pass -> if !pass_count >= 5 then raise too_many_failures else if pass = !current_pass then begin pass_count := 0; !current_balance end else begin pass_count := !pass_count + 1; raise wrong_pass end)} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let find_edge l (x, y, w) = if x = vertex then (y, w) :: l else l in List.fold_left find_edge [] g.edges ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.