text
stringlengths 0
601k
|
---|
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (v2, w) = node in let list_of_neighbours = neighbours g v2 in if v2 = b then ([b], w) else if List.mem v2 visited then raise Fail else if list_of_neighbours = [] then raise Fail else (let (neighbours, total) = aux_list list_of_neighbours (visited @ [v2]) in (v2 :: neighbours, w+total)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (vertex2,weight) :: tl -> try aux_node (vertex2,weight) visited with Fail -> aux_list tl visited in aux_node (a,0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (v2, w) = node in let list_of_neighbours = neighbours g v2 in if v2 = b then sc([b], w) else if not(List.mem v2 visited) then let acc = visited @ [v2] in let sc_func = (fun (neighbours, total) -> sc (v2 :: neighbours, w+total)) in aux_list list_of_neighbours acc fc sc_func else if list_of_neighbours = [] then fc() else fc() and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc() | (vertex, weight) :: tl -> let f = fun () -> aux_list tl visited fc sc in if vertex = b then sc([b], weight) else if List.mem vertex visited then aux_list tl visited fc sc else aux_node (vertex, weight) (visited) f 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 rec longest l = match l with | [] -> None | [(v2, w)] -> Some (v2, w) | a_list -> let f = fun a b -> let (v1, w1) = a in let (v2, w2) = b in if w1 < w2 then 1 else if w1 = w2 then 0 else -1 in let l = List.sort f a_list in Some (List.hd l) in longest (find_all_paths g a b);; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let password = ref initial_pass in let errors = ref 0 in { update_pass = (fun (old_pass : passwd) -> fun (new_pass : passwd) -> if old_pass = !password then password := new_pass else (errors := !errors + 1; raise wrong_pass)); retrieve = (fun (pass: passwd) -> fun (amt_withdraw : int) -> if !errors >= 5 then raise too_many_failures else if pass = !password then ( if amt_withdraw > !balance then (errors := 0; raise not_enough_balance) else if amt_withdraw < 0 then (errors := 0; raise negative_amount ) else (errors := 0; balance := !balance - amt_withdraw) ) else (errors := !errors + 1; raise wrong_pass)); deposit = (fun (pass: passwd) -> fun (amt_deposit : int) -> if !errors >= 5 then raise too_many_failures else if pass = !password then ( if amt_deposit < 0 then (errors := 0; raise negative_amount) else (errors := 0; balance := !balance + amt_deposit) ) else (errors := !errors + 1; raise wrong_pass)); show_balance = (fun (pass: passwd) -> if !errors >= 5 then raise too_many_failures else if pass = !password then (errors:=0 ; !balance) else (errors := !errors + 1; raise wrong_pass)) } ;; let graph = { nodes = ["a"; "b"; "c"; "d"]; edges = [("a", "b", 1); ("a", "c", 2); ("c", "d", 1); ("d", "a", 0); ("b", "c", (-2))] } in ((graph, "a"), [("b", 1); ("c", 2)]) ); (let graph = { nodes = ["a"; "b"; "c"; "d"]; edges = [("a", "b", 1); ("a", "c", 2); ("c", "d", 1)] } in ((graph, "a"), [("b", 1); ("c", 2)]) ); (let graph = { nodes = ["a"; "b"; "c"; "d"]; edges = [("a", "a", 0); ("a", "c", 2); ("c", "a", 3); ("c", "d", 1); ("d", "a", 2)] } in ((graph, "a"), [("a", 0); ("c", 2)]) ); (let graph = { nodes = ["a"; "b"; "c"]; edges = [("a", "b", 1); ("c", "b", 2)] } in (graph, "b"), []) ; (let graph = { nodes = ["a"]; edges = [] } in (graph, "b"), []); (({nodes = []; edges = []},"b"), []) ];; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc (edge : ('a * 'a * weight)) -> let (a, b, w) = edge in if (a = vertex) then ((b,w)::acc) else acc) [] g.edges ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if List.exists (fun n -> let (content, weight) = node in n = content) visited then raise Fail else let (content, weight) = node in if content = b then ([content], weight) else let (lst, wtot) = aux_list (neighbours g content) (content::visited) in (content :: lst, weight + wtot) 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)= notimplemented() in notimplemented() let helper_find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if List.exists (fun n -> let (content, weight) = node in n = content) visited then raise Fail else let (content, weight) = node in if content = b then ([content], weight) else let (lst, wtot) = aux_list (neighbours g content) (content::visited) in (content :: lst, weight + wtot) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> ([],0) | h::t -> try aux_node h visited with Fail -> aux_list t visited in aux_node (a,0) [] ;; let rec attach_neighbours g path b neighbour_list = match neighbour_list with | [] -> [] | h::t -> let (nodes, weight_path) = path in let (node, weight_node) = h in if List.exists (fun x -> x = node) nodes then (attach_neighbours g path b t) else let curhd = List.nth (nodes@[node]) (List.length (nodes@[node]) -1) in if curhd = b then (nodes@[node], weight_path+weight_node)::(attach_neighbours g path b t) else let (end_path, end_weight) = helper_find_path g curhd b in [(nodes@end_path, weight_path+weight_node+end_weight)] @ (attach_neighbours g (nodes@[node], weight_path+weight_node) b (neighbours g curhd))@ (attach_neighbours g path b t) ;; let rec no_duplicates path = match path with | [] -> false | h::t -> if List.exists (fun x -> x=h) t then true else no_duplicates t ;; let rec remove_duplicates lst acc = match lst with | [] -> acc | h::t -> if List.mem h t then remove_duplicates t acc else remove_duplicates t (h::acc) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec longest paths path = match paths with | [] -> if (snd path) <= (-60) then None else Some path | h::t -> let (newpath, newweight) = h in if (newweight) > (snd path) then (longest (t) (h)) else longest t path in longest (find_all_paths g a b) ([], (-60)) ;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in let check = (fun (in_pass: passwd) -> ( if (!attempts >= 5) then raise too_many_failures else if (in_pass = !pass) then let _ = (attempts := 0) in true else let _ = (attempts := !attempts + 1) in raise wrong_pass )) in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> if old_pass = !pass then let _ = (attempts := 0) in pass := new_pass else let _ = (attempts := !attempts + 1) in raise wrong_pass); deposit = (fun (in_pass: passwd) (amount: int) -> if (check in_pass) then if amount >= 0 then balance := !balance + amount else raise negative_amount); retrieve = (fun (in_pass: passwd) (amount: int) -> if (check in_pass) then if amount >= 0 then if amount <= !balance then balance := !balance - amount else raise not_enough_balance else raise negative_amount ); show_balance = (fun (in_pass: passwd) -> if (check in_pass) then !balance else raise wrong_pass) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (fun (v1,v2,w) -> (v2,w)) (List.filter (fun (v1,v2,w) -> v1 = 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 index = fst node in let w = snd node in if index = b then ([b], w) else if List.exists ((=) index) visited then raise Fail else let (path, nw) = aux_list (neighbours g index) (visited @ [index]) in (index :: path, nw + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (node: 'a * weight) :: rem -> try aux_node node visited with Fail -> aux_list rem 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 index = fst node in let w = snd node in if index = b then sc ([b], w) else if List.exists ((=) index) visited then fc() else aux_list (neighbours g index) (visited @ [index]) (fc) (fun (path, nw) -> sc (index :: path, nw + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc() | (node: 'a * weight) :: rem -> aux_node node visited (fun () -> aux_list rem visited fc sc) sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun nodes: ('a list * weight) -> nodes);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all = find_all_paths g a b in match all with | [] -> None | _ -> Some (List.hd (List.sort (fun (path1: ('a list * weight)) (path2: ('a list * weight)) -> (snd path2) - (snd path1)) all));; |
let new_balance() : balances = let balance = ref 0 in { get_balance = (fun () -> !balance); set_balance = (:=) balance; };; let new_counter () : my_counter = let counter = ref 0 in { tick = (fun () -> counter := !counter + 1; !counter); reset = (fun() -> counter := 0); current_counter = (fun() -> !counter) };; let update_pass password_var count (a: passwd) (b: passwd) = if a = !password_var then let _ = count.reset() in password_var := b else let _ = count.tick() in raise wrong_pass ;; let retrieve count bal password_var (a: passwd) (b: int) = if count.current_counter() = 5 then raise too_many_failures else if b < 0 && a = !password_var then raise negative_amount else if a = !password_var then if bal.get_balance() >= b then let _ = count.reset() in bal.set_balance (bal.get_balance() - b) else let _ = count.reset() in raise not_enough_balance else let _ = count.tick() in raise wrong_pass ;; let deposit count bal password_var (a: passwd) (b: int) = if count.current_counter() = 5 then raise too_many_failures else if b < 0 && a = !password_var then raise negative_amount else if a = !password_var then let _ = count.reset() in bal.set_balance (bal.get_balance() + b) else let _ = count.tick() in raise wrong_pass ;; let show_balance count bal password_var (a: passwd) = if count.current_counter() = 5 then raise too_many_failures else if a = !password_var then let _ = count.reset() in bal.get_balance() else let _ = count.tick() in raise wrong_pass ;; |
let open_account (initial_pass: passwd) : bank_account = let count = new_counter() in let bal = new_balance() in let (password_var: password) = ref initial_pass in let new_account = {update_pass = update_pass (password_var) (count); retrieve = retrieve (count) (bal) (password_var); deposit = deposit (count) (bal) (password_var); show_balance = show_balance (count) (bal) (password_var)} in new_account ;; let getfirst(a, _, _) = a;; let getsecond(_, b, _) = b;; let getthird(_, _, c) = c;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edge = g.edges in let rec aux (edge) (vertex: 'a) lts = match edge with | [] -> lts | x::xs -> if (getfirst x) = vertex then aux xs (vertex) (((getsecond x), (getthird x))::lts) else aux xs vertex lts in aux edge vertex [] ;; let getfirst2(a, _) = a;; let getsecond2(_, b) = b;; let rec remove a b acc = match a with | [] -> acc | x::xs -> let first = (getfirst2 x) in if (List.mem first b) then remove xs b acc else (remove xs b (x::acc)) ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux (g: 'a graph) (start) (b: 'a) (lts: 'a list) weights: ('a list * weight) = match lts with | [] -> raise Fail | x::_ -> if (x = b) then ((List.rev lts), weights) else match start with | [] -> raise Fail | hd::tl -> let h = hd in let t = tl in (try let nei = remove (neighbours g (getfirst2 h)) lts [] in aux g nei b ((getfirst2 h)::lts) ((getsecond2 h) + weights) with Fail -> aux g t b lts weights) in if b = a then (a::[], 0) else let start = (neighbours g a) in (aux g start b (a::[]) 0) ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux (g: 'a graph) (start) (b: 'a) (lts: 'a list) weights fail succeed: ('a list * weight) = match lts with | [] -> fail () | x::_ -> if (x = b) then succeed ((List.rev lts), weights) else match start with | [] -> fail () | hd::tl -> let h = hd in let t = tl in let suc2 = succeed in let fail2 () = aux g t b lts weights fail suc2 in let nei = remove (neighbours g (getfirst2 h)) lts [] in aux g nei b ((getfirst2 h) :: lts) ((getsecond2 h) + weights) fail2 suc2 in if b = a then (a::[], 0) else let start = (neighbours g a) in (aux g start b (a::[]) 0 (fun () -> raise Fail) (fun u -> u)) ;; |
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 rec aux paths maxsize max = match paths with | [] -> Some maxsize | x::xs -> if (getsecond2 x) > max then (aux xs x (getsecond2 x)) else aux xs maxsize max in aux (List.tl paths) (List.hd paths) (getsecond2 (List.hd paths)) ;; |
let new_balance() : balances = let balance = ref 0 in { get_balance = (fun () -> !balance); set_balance = (:=) balance; };; let new_counter () : my_counter = let counter = ref 0 in { tick = (fun () -> counter := !counter + 1; !counter); reset = (fun() -> counter := 0); current_counter = (fun() -> !counter) };; let update_pass password_var count (a: passwd) (b: passwd) = if a = !password_var then let _ = count.reset() in password_var := b else let _ = count.tick() in raise wrong_pass ;; let retrieve count bal password_var (a: passwd) (b: int) = if count.current_counter() = 5 then raise too_many_failures else if b < 0 && a = !password_var then raise negative_amount else if a = !password_var then if bal.get_balance() >= b then let _ = count.reset() in bal.set_balance (bal.get_balance() - b) else let _ = count.reset() in raise not_enough_balance else let _ = count.tick() in raise wrong_pass ;; let deposit count bal password_var (a: passwd) (b: int) = if count.current_counter() = 5 then raise too_many_failures else if b < 0 && a = !password_var then raise negative_amount else if a = !password_var then let _ = count.reset() in bal.set_balance (bal.get_balance() + b) else let _ = count.tick() in raise wrong_pass ;; let show_balance count bal password_var (a: passwd) = if count.current_counter() = 5 then raise too_many_failures else if a = !password_var then let _ = count.reset() in bal.get_balance() else let _ = count.tick() in raise wrong_pass ;; |
let open_account (initial_pass: passwd) : bank_account = let count = new_counter() in let bal = new_balance() in let (password_var: password) = ref initial_pass in let new_account = {update_pass = update_pass (password_var) (count); retrieve = retrieve (count) (bal) (password_var); deposit = deposit (count) (bal) (password_var); show_balance = show_balance (count) (bal) (password_var)} in new_account ;; let getfirst(a, _, _) = a;; let getsecond(_, b, _) = b;; let getthird(_, _, c) = c;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edge = g.edges in let rec aux (edge) (vertex: 'a) lts = match edge with | [] -> lts | x::xs -> if (getfirst x) = vertex then aux xs (vertex) (((getsecond x), (getthird x))::lts) else aux xs vertex lts in aux edge vertex [] ;; let getfirst2(a, _) = a;; let getsecond2(_, b) = b;; let rec remove a b acc = match a with | [] -> acc | x::xs -> let first = (getfirst2 x) in if (List.mem first b) then remove xs b acc else (remove xs b (x::acc)) ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux (g: 'a graph) (start) (b: 'a) (lts: 'a list) weights: ('a list * weight) = match lts with | [] -> raise Fail | x::_ -> if (x = b) then ((List.rev lts), weights) else match start with | [] -> raise Fail | hd::tl -> let h = hd in let t = tl in (try let nei = remove (neighbours g (getfirst2 h)) lts [] in aux g nei b ((getfirst2 h)::lts) ((getsecond2 h) + weights) with Fail -> aux g t b lts weights) in if b = a then (a::[], 0) else let start = (neighbours g a) in (aux g start b (a::[]) 0) ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux (g: 'a graph) (start) (b: 'a) (lts: 'a list) weights fail succeed: ('a list * weight) = match lts with | [] -> fail () | x::_ -> if (x = b) then succeed ((List.rev lts), weights) else match start with | [] -> fail () | hd::tl -> let h = hd in let t = tl in let suc2 = succeed in let fail2 () = aux g t b lts weights fail suc2 in let nei = remove (neighbours g (getfirst2 h)) lts [] in aux g nei b ((getfirst2 h) :: lts) ((getsecond2 h) + weights) fail2 suc2 in if b = a then (a::[], 0) else let start = (neighbours g a) in (aux g start b (a::[]) 0 (fun () -> raise Fail) (fun u -> u)) ;; |
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 rec aux paths maxsize max = match paths with | [] -> Some maxsize | x::xs -> if (getsecond2 x) > max then (aux xs x (getsecond2 x)) else aux xs maxsize max in aux (List.tl paths) (List.hd paths) (getsecond2 (List.hd paths)) ;; |
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_check: passwd) (new_password: passwd) -> if (!password = password_check) then (password := new_password; attempts := 0) else (attempts := !attempts + 1; raise wrong_pass)); retrieve = (fun (password_check: passwd) (amnt: int) -> if (!attempts > 4) then raise too_many_failures else if (!password = password_check) then (attempts := 0; if (amnt >= 0) then if (!balance > amnt) then balance := !balance-amnt else raise not_enough_balance else raise negative_amount) else (attempts := !attempts + 1; raise wrong_pass)); deposit = (fun (password_check: passwd) (amnt: int) -> if (!attempts > 4) then raise too_many_failures else if (!password = password_check) then (attempts := 0; if (amnt >= 0) then balance := !balance + amnt else raise negative_amount) else (attempts := !attempts + 1; raise wrong_pass)); show_balance = (fun (password_check: passwd) -> if (!attempts > 4) then raise too_many_failures else if (!password = password_check) then (attempts := 0; !balance) else (attempts := !attempts + 1; raise wrong_pass)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left(fun acc (s, d, w) -> if vertex = s then (d,w) :: acc else acc) [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (x,wx)::xs -> if List.mem x visited then aux_list xs visited else if (x=b) then ([x], wx) else try aux_list xs (x::visited) with Fail -> let (y,w) = aux_list (neighbours g x) (x :: visited) in (x::y, w+wx) in let (z,w) = aux_list (neighbours g a) [a] in (a::z, w);; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc (raise Fail) | (x,wx)::xs -> if List.mem x visited then fc (aux_list xs visited fc sc) else if (x=b) then sc ([x], wx) else try (aux_list xs (x::visited) fc sc ) with Fail -> let (y,w) = aux_list (neighbours g x) (x :: visited) (fun _ -> let (y,w) = aux_list (neighbours g x) (x :: visited) fc sc in (x::y, w+wx)) sc in (x::y, w+wx) in let (z,w) = aux_list (neighbours g a) [a] (fun x->x) (fun x->x) in (a::z, w);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec findPath (longestPath: ('a list * weight)) (remainingPaths: (('a list * weight) list)) (longestPathLength: int) : ('a list * weight) = match remainingPaths with | [] -> longestPath | (s,w)::xs -> if ((List.length s) > longestPathLength) then findPath (s,w) xs (List.length s) else findPath longestPath xs longestPathLength in if (findPath ([],0) (find_all_paths g a b) 0 = ([],0)) then None else Some (findPath ([],0) (find_all_paths g a b) 0);; |
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_check: passwd) (new_password: passwd) -> if (!password = password_check) then (password := new_password; attempts := 0) else (attempts := !attempts + 1; raise wrong_pass)); retrieve = (fun (password_check: passwd) (amnt: int) -> if (!attempts > 4) then raise too_many_failures else if (!password = password_check) then (attempts := 0; if (amnt >= 0) then if (!balance >= amnt) then balance := !balance-amnt else raise not_enough_balance else raise negative_amount) else (attempts := !attempts + 1; raise wrong_pass)); deposit = (fun (password_check: passwd) (amnt: int) -> if (!attempts > 4) then raise too_many_failures else if (!password = password_check) then (attempts := 0; if (amnt >= 0) then balance := !balance + amnt else raise negative_amount) else (attempts := !attempts + 1; raise wrong_pass)); show_balance = (fun (password_check: passwd) -> if (!attempts > 4) then raise too_many_failures else if (!password = password_check) then (attempts := 0; !balance) else (attempts := !attempts + 1; raise wrong_pass)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left(fun acc (s, d, w) -> if vertex = s then (d,w) :: acc else acc) [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (next, w) = node in if next = b then ([next], w) else if List.mem next visited then raise Fail else let (x, wx) = aux_list (neighbours g next) (next :: visited) in (next::x, w+wx) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (x,wx)::xs -> try (aux_node (x, wx) 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 (next, w) = node in if next = b then sc ([next], w) else if List.mem next visited then fc () else let suc2 = fun (y, wx) -> sc (next::y, w+wx) in aux_list (neighbours g next) (next :: visited) fc suc2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (x,wx)::xs -> let fail2 = fun () -> aux_list xs visited fc sc in aux_node (x, wx) visited fail2 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 rec findPath (longestPath: ('a list * weight)) (remainingPaths: (('a list * weight) list)) (longestPathWeight: int) (longestPathLength: int) : ('a list * weight) = match remainingPaths with | [] -> longestPath | (s,w)::xs -> if (w > longestPathWeight) then findPath (s,w) xs w longestPathLength else if (w = longestPathWeight) && (List.length s > longestPathLength) then findPath (s,w) xs longestPathWeight (List.length s) else findPath longestPath xs longestPathWeight longestPathLength in let x = (findPath ([],0) (find_all_paths g a b) 0 0) in if x = ([],0) then None else Some (findPath ([],0) (find_all_paths g a b) 0 0);; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let bal = ref 0 in let fail = ref 0 in { update_pass = ( fun p_old p_new -> if p_old = !pass then (fail := 0; pass := p_new) else if p_old != !pass then (fail := !fail+1; raise wrong_pass) ); deposit = ( fun p amt -> if !fail >= 5 then raise too_many_failures else if p = !pass then if amt < 0 then (fail := 0; raise negative_amount) else (fail := 0; bal := !bal + amt) else if p != !pass then (fail := !fail+1; raise wrong_pass) ); retrieve = ( fun p amt -> if !fail >= 5 then raise too_many_failures else if p = !pass then if amt < 0 then (fail := 0; raise negative_amount) else if amt > !bal then (fail := 0; raise not_enough_balance) else (fail := 0; bal := !bal - amt) else if p != !pass then (fail := !fail+1; raise wrong_pass) ); show_balance = ( fun p -> if !fail >= 5 then raise too_many_failures else if p = !pass then (fail :=0; !bal) else (fail := !fail+1; raise wrong_pass) )} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec f edges v n acc = if n < List.length edges then begin match List.nth edges n with | (v1, v2, w) when v1 = v -> f edges vertex (n+1) ((v2,w) :: acc) | (_,_,_) -> f edges vertex (n+1) acc end else acc in f g.edges vertex 0 [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if fst(node) = b then ([b],snd(node)) else if List.mem (fst(node)) visited then raise Fail else begin let result = aux_list (neighbours g (fst(node))) (fst(node)::visited) in (fst(node) :: fst(result), snd(node) + snd(result)) end 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)= if fst(node) = b then (List.rev (fst(sc (b,snd(node)))), snd(sc (b,snd(node)))) else if List.mem (fst(node)) visited then fc () else begin let sc2 = sc (fst(node), snd(node)) in aux_list (neighbours g (fst(node))) (fst(node)::visited) fc (fun (a,b) -> (a :: fst(sc2), b+snd(sc2))) end 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 (a,b) -> ([a],b));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec f paths p_max = match paths with | [] -> p_max | x :: xs -> let p = match p_max with | None -> 0 | Some s -> snd(s) in if snd(x) > p then f xs (Some x) else f xs p_max in let paths = find_all_paths g a b in f paths None;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let count = ref 0 in { update_pass = (fun curpass newpass -> (if curpass = !password then (password := (newpass); count := 0 ) else (count := !count + 1; raise wrong_pass))) ; retrieve = (fun pass amount -> (if (pass = !password && !count < 5)then (if amount >= 0 then (if (!balance - amount) >= 0 then (balance := (!balance - amount); count:= 0) else raise not_enough_balance ) else raise negative_amount) else (if !count < 5 then (count := (!count + 1); raise wrong_pass) else raise too_many_failures))) ; deposit = (fun pass amount -> if (pass = !password && !count < 5) then (if amount >= 0 then (balance := !balance + amount; count := 0) else raise negative_amount) else if (!count < 5) then (count := !count + 1; raise wrong_pass) else raise too_many_failures ) ; show_balance = (fun pass -> if (pass = !password && !count < 5) then (count:=0; !balance) else (if (!count < 5) then (count := !count + 1; raise wrong_pass) else raise too_many_failures )) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let get_first (a,_,_) = a in let get_out_vertex (_,b,c) = (b,c) in let rec loop acc l = match l with | [] -> acc | x::xs -> if (get_first x) = vertex then loop ((get_out_vertex x) :: acc) xs else loop acc xs in loop [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let get_first (y,_) = y in let get_second(_,y) = y in let ans = [] in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = ([],0) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = ([],0) in notimplemented ();; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let bank_pass=ref initial_pass in let failed=ref 0 in let balance=ref 0 in { update_pass =(fun (p1: passwd) (newpass: passwd) -> if(!bank_pass = p1 ) then (failed:=0; bank_pass := newpass) else(failed := !failed+1; raise wrong_pass)); retrieve=(fun (p1: passwd) (take: int)-> if(!failed>4)then(raise too_many_failures) else if(!bank_pass = p1 && !balance<take)then(failed:=0; raise not_enough_balance) else if(take<0) then (raise negative_amount) else if(!bank_pass = p1)then(failed:=0; balance := !balance-take) else(failed:=!failed+1; raise wrong_pass)); deposit=(fun (p1: passwd) (add: int)-> if(!failed>4)then(raise too_many_failures) else if(add<0) then (raise negative_amount) else if(!bank_pass = p1)then(failed:=0; balance := !balance+add) else(failed:=!failed+1; raise wrong_pass)); show_balance =(fun (p1 : passwd)-> if(!failed>4)then(raise too_many_failures) else if(!bank_pass = p1)then(failed:=0; !balance) else(failed:=!failed+1; raise wrong_pass )); } ;; let rec add add_to_end (a : 'a) (l : a' List) *);; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun x (y1,y2,y3)->if y1=vertex then (y2,y3)::x else x) [] 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) (v : weight) : ('a list * weight) = let (a,c) = node in if a=b then (visited @ [a]),c+v else if List.mem a visited then raise Fail else aux_list (neighbours g a) ((visited @ [a])) (c+v) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (v : weight) : ('a list * weight) = match nodes with |[]-> raise Fail |h :: t -> try aux_node h (visited) v with Fail -> aux_list t visited v 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) (v : weight) fc sc : ('a list * weight)= let (n1,w1) = node in if n1=b then let(path,v1) = sc() in (path@[n1],w1+v1) else if List.mem n1 visited then fc() else aux_list (neighbours g n1) (visited@[n1]) v fc (fun () -> let(path,v1)= sc() in (path@[n1],w1+v1)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (v : weight) fc sc : ('a list * weight) = match nodes with |[]->fc() |h::t->let (n2,_) = h in aux_node h visited v (fun () -> aux_list t (visited@[n2]) v fc sc) sc in aux_node (a,0) [] 0 (fun () -> raise Fail) (fun ()->([],0));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match find_all_paths g a b with |[]-> None |c->let rec find_max c acc t= match c with |[]-> Some t |w::d->let (_,h2) = w in if h2 > acc then find_max d h2 w else find_max d acc t in find_max c 0 ([],0);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let number = ref 5 in { update_pass = (fun old_pwd new_pwd -> (if (old_pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else number := 5; password := new_pwd; ) ); retrieve = (fun pwd num -> (if (!number <= 0) then begin raise too_many_failures; end else if (pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else begin number := 5; if (num < 0) then begin raise negative_amount; end else if (num > !balance) then begin raise not_enough_balance; end else balance := !balance - num; end ) ); deposit = (fun pwd num -> (if (!number <= 0) then begin raise too_many_failures; end else if (pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else begin number := 5; if (num < 0) then begin raise negative_amount; end else balance := !balance + num; end ) ); show_balance = (fun pwd -> (if (!number <= 0) then raise too_many_failures; begin if (pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else begin number := 5; !balance; end end ) ) };; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = if g.edges = [] then failwith "invalid input" else let rec check_neigh neigh list_neights = match neigh with | [] -> list_neights | (orig,dest,weight) :: tl -> if orig = vertex then check_neigh tl list_neights@[(dest,weight)] else check_neigh tl list_neights in check_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 (path_noode,w) = node in if path_noode = b then ([path_noode],w) else let (path,cost) = aux_list (neighbours g path_noode) visited in if (List.length path) > 0 then (path_noode::path,cost+w) else (path,cost) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = if (List.length nodes) = 0 then raise Fail else let (path_noode,w) = List.hd nodes in if List.mem path_noode visited then aux_list (List.tl nodes) visited else try let (path,cost) = aux_node (path_noode,w) (path_noode::visited) in (path,cost) with Fail -> aux_list (List.tl nodes) visited in try let (path,cost) = aux_list (neighbours g a) [a] in (a::path,cost) with Fail -> raise Fail;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (path_noode,w) = node in if path_noode = b then sc ([path_noode],w) else let su2 = (fun (path,cost) -> if (List.length path) > 0 then sc (path_noode::path,cost+w) else sc (path,cost)) in aux_list (neighbours g path_noode) visited fc su2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = if (List.length nodes) = 0 then sc fc else let (path_noode,w) = List.hd nodes in if List.mem path_noode visited then aux_list (List.tl nodes) visited fc sc else let sc2 = (fun (path,cost) -> if (List.length path) > 0 then sc (path,cost) else aux_list (List.tl nodes) visited fc sc) in aux_node (path_noode,w) (path_noode::visited) fc sc2 in let (path,cost) = aux_list (neighbours g a) [a] (([],0)) (fun res -> let (path,cost) = res in (path,cost)) in if path = [] then raise Fail else (a::path,cost);; |
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) : ('a list * weight) = let (path_noode,w) = node in if path_noode = b then ([path_noode],w) else let (path,cost) = aux_list (neighbours g path_noode) visited in if (List.length path) > 0 then (path_noode::path,cost+w) else (path,cost) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = if (List.length nodes) = 0 then ([],0) else let (path_noode,w) = List.hd nodes in if List.mem path_noode visited then aux_list (List.tl nodes) visited else let path = ref [] in let cost = ref 0 in let (path1,cost1) = aux_node (path_noode,w) (path_noode::visited) in if (List.length path1) > 0 && cost1 > !cost then ( path := path1; cost := cost1; ); let (path2,cost2) = aux_list (List.tl nodes) visited in if (List.length path2) > 0 && cost2 > !cost then ( path := path2; cost := cost2; ); (!path,!cost) in let (path,cost) = aux_list (neighbours g a) [a] in if ((List.length path) > 0) then Some(a::path,cost) else None;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let number = ref 5 in { update_pass = (fun old_pwd new_pwd -> (if (old_pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else number := 5; password := new_pwd; ) ); retrieve = (fun pwd num -> (if (!number <= 0) then begin raise too_many_failures; end else if (pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else begin number := 5; if (num < 0) then begin raise negative_amount; end else if (num > !balance) then begin raise not_enough_balance; end else balance := !balance - num; end ) ); deposit = (fun pwd num -> (if (!number <= 0) then begin raise too_many_failures; end else if (pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else begin number := 5; if (num < 0) then begin raise negative_amount; end else balance := !balance + num; end ) ); show_balance = (fun pwd -> (if (!number <= 0) then raise too_many_failures; begin if (pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else begin number := 5; !balance; end end ) ) };; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = if g.edges = [] then failwith "invalid input" else let rec check_neigh neigh list_neights = match neigh with | [] -> list_neights | (orig,dest,weight) :: tl -> if orig = vertex then check_neigh tl list_neights@[(dest,weight)] else check_neigh tl list_neights in check_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 (path_noode,w) = node in if path_noode = "" then raise Fail else if path_noode = b then ([path_noode],w) else let (path,cost) = aux_list (neighbours g path_noode) visited in if (List.length path) > 0 then (path_noode::path,cost+w) else (path,cost) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = if (List.length nodes) = 0 then raise Fail else let (path_noode,w) = List.hd nodes in if List.mem path_noode visited then aux_list (List.tl nodes) visited else try let (path,cost) = aux_node (path_noode,w) (path_noode::visited) in (path,cost) with Fail -> aux_list (List.tl nodes) visited in try let (path,cost) = aux_list (neighbours g a) [a] in (a::path,cost) with Fail -> raise Fail;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (path_noode,w) = node in if path_noode = b then sc ([path_noode],w) else let su2 = (fun (path,cost) -> if (List.length path) > 0 then sc (path_noode::path,cost+w) else sc (path,cost)) in aux_list (neighbours g path_noode) visited fc su2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = if (List.length nodes) = 0 then sc fc else let (path_noode,w) = List.hd nodes in if List.mem path_noode visited then aux_list (List.tl nodes) visited fc sc else let sc2 = (fun (path,cost) -> if (List.length path) > 0 then sc (path,cost) else aux_list (List.tl nodes) visited fc sc) in aux_node (path_noode,w) (path_noode::visited) fc sc2 in let (path,cost) = aux_list (neighbours g a) [a] (([],0)) (fun res -> let (path,cost) = res in (path,cost)) in if path = [] then raise Fail else (a::path,cost);; |
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) : ('a list * weight) = let (path_noode,w) = node in if path_noode = b then ([path_noode],w) else let (path,cost) = aux_list (neighbours g path_noode) visited in if (List.length path) > 0 then (path_noode::path,cost+w) else (path,cost) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = if (List.length nodes) = 0 then ([],0) else let (path_noode,w) = List.hd nodes in if (List.exists (fun a -> a = path_noode) visited) then aux_list (List.tl nodes) visited else let path = ref [] in let cost = ref 0 in let (path1,cost1) = aux_node (path_noode,w) (path_noode::visited) in if (List.length path1) > 0 && cost1 > !cost then ( path := path1; cost := cost1; ); let (path2,cost2) = aux_list (List.tl nodes) visited in if (List.length path2) > 0 && cost2 > !cost then ( path := path2; cost := cost2; ); (!path,!cost) in let (path,cost) = aux_list (neighbours g a) [a] in if ((List.length path) > 0) then Some(a::path,cost) else None;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let number = ref 5 in { update_pass = ( fun old_pwd new_pwd -> ( if (old_pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else number := 5; password := new_pwd; ) ); retrieve = ( fun pwd num -> ( if (!number <= 0) then begin raise too_many_failures; end else if (pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else begin number := 5; if (num < 0) then begin raise negative_amount; end else if (num > !balance) then begin raise not_enough_balance; end else balance := !balance - num; end ) ); deposit = ( fun pwd num -> ( if (!number <= 0) then begin raise too_many_failures; end else if (pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else begin number := 5; if (num < 0) then begin raise negative_amount; end else balance := !balance + num; end ) ); show_balance = ( fun pwd -> ( if (!number <= 0) then raise too_many_failures; begin if (pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else begin number := 5; !balance; end end ) ) };; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = if g.edges = [] then failwith "invalid input" else let rec check_neigh neigh list_neights = match neigh with | [] -> list_neights | (orig,dest,weight) :: tl -> if orig = vertex then check_neigh tl list_neights@[(dest,weight)] else check_neigh tl list_neights in check_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 (path_noode,w) = node in if path_noode = b then ([path_noode],w) else let (path,cost) = aux_list (neighbours g path_noode) visited in if (List.length path) > 0 then (path_noode::path,cost+w) else (path,cost) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = if (List.length nodes) = 0 then raise Fail else let (path_noode,w) = List.hd nodes in if List.mem path_noode visited then aux_list (List.tl nodes) visited else try let (path,cost) = aux_node (path_noode,w) (path_noode::visited) in (path,cost) with Fail -> aux_list (List.tl nodes) visited in try let (path,cost) = aux_list (neighbours g a) [a] in (a::path,cost) with Fail -> raise Fail;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (path_noode,w) = node in if path_noode = b then sc ([path_noode],w) else let su2 = (fun (path,cost) -> if (List.length path) > 0 then sc (path_noode::path,cost+w) else sc (path,cost)) in aux_list (neighbours g path_noode) visited fc su2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = if (List.length nodes) = 0 then sc fc else let (path_noode,w) = List.hd nodes in if List.mem path_noode visited then aux_list (List.tl nodes) visited fc sc else let sc2 = (fun (path,cost) -> if (List.length path) > 0 then sc (path,cost) else aux_list (List.tl nodes) visited fc sc) in aux_node (path_noode,w) (path_noode::visited) fc sc2 in let (path,cost) = aux_list (neighbours g a) [a] (([],0)) (fun res -> let (path,cost) = res in (path,cost)) in if path = [] then raise Fail else (a::path,cost);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec longest all_path longest_path = let (path_longest,cost_longest) = longest_path in match all_path with | [] -> Some longest_path | (path,cost) :: tl -> if cost >= cost_longest then longest tl (path,cost) else longest tl longest_path in if List.length (find_all_paths g a b) = 0 then None else longest (find_all_paths g a b) (List.nth (find_all_paths g a b) 0);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let number = ref 5 in { update_pass = ( fun old_pwd new_pwd -> ( if (old_pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else number := 5; password := new_pwd; ) ); retrieve = ( fun pwd num -> ( if (!number <= 0) then begin raise too_many_failures; end else if (pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else begin number := 5; if (num < 0) then begin raise negative_amount; end else if (num > !balance) then begin raise not_enough_balance; end else balance := !balance - num; end ) ); deposit = ( fun pwd num -> ( if (!number <= 0) then begin raise too_many_failures; end else if (pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else begin number := 5; if (num < 0) then begin raise negative_amount; end else balance := !balance + num; end ) ); show_balance = ( fun pwd -> ( if (!number <= 0) then raise too_many_failures; begin if (pwd <> !password) then begin number := !number - 1; raise wrong_pass; end else begin number := 5; !balance; end end ) ) };; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = if g.edges = [] then failwith "invalid input" else let rec check_neigh neigh list_neights = match neigh with | [] -> list_neights | (orig,dest,weight) :: tl -> if orig = vertex then check_neigh tl list_neights@[(dest,weight)] else check_neigh tl list_neights in check_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 (path_noode,w) = node in if path_noode = b then ([path_noode],w) else let (path,cost) = aux_list (neighbours g path_noode) visited in if (List.length path) > 0 then (path_noode::path,cost+w) else (path,cost) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = if (List.length nodes) = 0 then raise Fail else let (path_noode,w) = List.hd nodes in if List.mem path_noode visited then aux_list (List.tl nodes) visited else try let (path,cost) = aux_node (path_noode,w) (path_noode::visited) in (path,cost) with Fail -> aux_list (List.tl nodes) visited in try let (path,cost) = aux_list (neighbours g a) [a] in (a::path,cost) with Fail -> raise Fail;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (path_noode,w) = node in if path_noode = b then sc ([path_noode],w) else let su2 = (fun (path,cost) -> if (List.length path) > 0 then sc (path_noode::path,cost+w) else sc (path,cost)) in aux_list (neighbours g path_noode) visited fc su2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = if (List.length nodes) = 0 then sc fc else let (path_noode,w) = List.hd nodes in if List.mem path_noode visited then aux_list (List.tl nodes) visited fc sc else let sc2 = (fun (path,cost) -> if (List.length path) > 0 then sc (path,cost) else aux_list (List.tl nodes) visited fc sc) in aux_node (path_noode,w) (path_noode::visited) fc sc2 in let (path,cost) = aux_list (neighbours g a) [a] (([],0)) (fun res -> let (path,cost) = res in (path,cost)) in if path = [] then raise Fail else (a::path,cost);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec longest all_path longest_path = let (_,cost_longest) = longest_path in match all_path with | [] -> Some longest_path | (path,cost) :: tl -> if cost > cost_longest then longest tl (path,cost) else longest tl longest_path in if List.length (find_all_paths g a b) = 0 then None else longest (find_all_paths g a b) (List.nth (find_all_paths g a b) 0);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass and account_balance = ref 0 and wrong_pass_counter = ref 0 in { update_pass = (fun(old_password : passwd) (new_password : passwd) -> if old_password = !password then (password := new_password; wrong_pass_counter := 0) else (wrong_pass_counter := !wrong_pass_counter +1; raise wrong_pass)); deposit = (fun(input_password : passwd) (deposit_amount : int)-> if (!wrong_pass_counter < 5) then if(input_password = !password) then (wrong_pass_counter := 0; if (deposit_amount >= 0) then (account_balance := !account_balance + deposit_amount) else raise negative_amount) else (wrong_pass_counter := !wrong_pass_counter +1; raise wrong_pass) else raise too_many_failures); retrieve = (fun(input_password : passwd) (retrieval_amount : int)-> if (!wrong_pass_counter < 5) then if(input_password = !password) then (wrong_pass_counter := 0; if (retrieval_amount >= 0) then if (retrieval_amount <= !account_balance) then (account_balance := !account_balance - retrieval_amount) else (raise not_enough_balance) else (raise negative_amount)) else (wrong_pass_counter := !wrong_pass_counter +1; raise wrong_pass) else raise too_many_failures); show_balance = (fun(input_password : passwd) -> if (!wrong_pass_counter < 5) then if(input_password = !password) then (wrong_pass_counter := 0; !account_balance) else (wrong_pass_counter := !wrong_pass_counter +1; raise wrong_pass) else raise too_many_failures); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun (v1, v2, w) acc -> if v1 = vertex then (v2, w) :: acc else acc) g.edges [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if fst(node) = b then ([b], snd(node)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented() in notimplemented();; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented () *);; |
let open_account (initial_pass: passwd) : bank_account = let passt=ref 0 in let ipass= ref initial_pass in let b = ref 0 in { update_pass = (fun passwd1 passwd2 -> if passwd1 <> !ipass then (raise wrong_pass; if !passt > 5 then raise too_many_failures ; passt:= (!passt + 1)) else ipass:= passwd2); retrieve = ( fun passwd int -> if passwd <> !ipass then ( if !passt > 5 then raise too_many_failures ; passt:= !passt +1; raise wrong_pass;) else if int < 0 then raise negative_amount else (if (!b - int) < 0 then raise not_enough_balance else b := !b - int)); deposit = (fun passwd int -> if passwd <> !ipass then ( if !passt > 5 then raise too_many_failures ; passt:= !passt +1 ;raise wrong_pass;) else if int < 0 then raise negative_amount else b := !b + int); show_balance = (fun passwd -> if passwd <> !ipass then ( if !passt > 5 then raise too_many_failures ; passt:= !passt + 1; raise wrong_pass;) else !b); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = match g with |[];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let wrong = ref 0 in { update_pass = ( fun old_pass new_pass -> ( if old_pass = !password then (password := new_pass ; wrong := 0) else (wrong := (!wrong + 1); raise wrong_pass) )); retrieve = ( fun pass amount -> ( if !wrong > 4 then raise too_many_failures else ( if pass = !password then ( wrong := 0; if amount < 0 then raise negative_amount else if amount > !balance then raise not_enough_balance else balance := (!balance - amount) ) else ( wrong := (!wrong + 1); raise wrong_pass ) ))); deposit = ( fun pass amount -> ( if !wrong > 4 then raise too_many_failures else ( if pass = !password then ( wrong := 0; if amount < 0 then raise negative_amount else balance := (!balance + amount) ) else ( wrong := (!wrong + 1); raise wrong_pass ) ))); show_balance = ( fun pass -> ( if !wrong > 4 then raise too_many_failures else if pass = !password then ( wrong := 0; !balance ) else ( wrong := (!wrong + 1); raise wrong_pass ) )) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f a b = ( let (a1,a2,w) = a in if a1 = vertex then (List.append [(a2,w)] b) else List.append [] b) in List.fold_right f g.edges [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let initial_neighbours = neighbours g a in let equal_node (node,weight) = (node = b) in let found = List.find_opt equal_node initial_neighbours in match found with |None -> ( let rec aux_node total_weight (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (cur_node,cur_weight) = node in let n = neighbours g cur_node in let equal_node (node,weight) = (node = b) in let found = List.find_opt equal_node n in match found with |None -> (let f (node,weight) = not(List.exists ((=) node) visited) in let filtered = List.filter f n in match filtered with |[] -> raise Fail |_ -> aux_list (cur_weight + total_weight) filtered visited) |Some (a,w) -> (List.append [a] visited,total_weight+cur_weight+w) and aux_list total_weight (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |head::rest-> let (cur_node,_) = List.hd nodes in try aux_node total_weight (List.hd nodes) (List.append [cur_node] visited) with Fail -> aux_list total_weight (List.tl nodes) visited in let list = aux_list 0 (initial_neighbours) [a] in let (l,w) = list in (List.rev l, w) ) |Some (node,weight) -> ([a;b],weight);; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let initial_neighbours = neighbours g a in let equal_node (node,weight) = (node = b) in let found = List.find_opt equal_node initial_neighbours in match found with |Some (node,weight) -> ([a;b],weight) |None -> ( let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list) option = let (cur_node,_) = node in let n = neighbours g cur_node in let equal_node (node,weight) = (node = b) in let found = List.find_opt equal_node n in match found with |Some (a,w) -> sc a |None -> (let f (node,weight) = not(List.exists ((=) node) visited) in let filtered = List.filter f n in match filtered with |[] -> fc () |_ -> aux_list filtered visited fc (fun x -> ( match sc x with |None -> failwith "This case shouldn't happen" |Some (l) -> Some (List.append [cur_node] l))) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list) option = match nodes with |[] -> fc () |head::rest-> let (cur_node,_) = List.hd nodes in let attempt = aux_node (List.hd nodes) (List.append [cur_node] visited) fc sc in match attempt with |None -> aux_list (List.tl nodes) visited fc sc |Some (l) -> match sc cur_node with |None -> failwith "This case shouldn't happen" |Some (l2) -> Some(List.append l2 l) in let list = aux_list (initial_neighbours) [a] (fun () -> None) (fun x -> Some (List.append [x] []) ) in match list with |None -> raise Fail |Some (l) -> let list2 = List.append [a] l in let rec removeextra old_list new_list = if List.length old_list = 0 then new_list else if (List.length new_list) = 0 then removeextra (List.tl old_list) [List.hd old_list] else if List.mem (List.hd old_list) new_list then removeextra (List.tl old_list) new_list else removeextra (List.tl old_list) (List.append new_list [List.hd old_list]) in let list3 = (removeextra list2 []) in let rec find_weight list cur_weight = match list with |[] -> cur_weight |_::[] -> cur_weight |e1::e2::rest -> let equal_edge (n1,n2,_) = ((n1 = e1) && (n2 = e2)) in let e = List.find_opt equal_edge g.edges in match e with |None -> failwith "This case should never happen" |Some(_,_,w) -> find_weight (e2::rest) (cur_weight + w) in (list3,find_weight list3 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 l = List.length paths in if l = 0 then None else let rec compare current list = match list with |[] -> current |hd::tl -> let (cur_l,cur_w) = current in let (next_l,next_w) = hd in if next_w > cur_w then compare (next_l,next_w) (tl) else if (next_w = cur_w) && ((List.length next_l) > (List.length cur_l)) then compare (next_l,next_w) (tl) else compare (cur_l,cur_w) (tl) in Some (compare ([],0) paths);; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let counter = ref 0 in let password = ref initial_pass in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> ( if (old_pass = !password) then (password := new_pass; counter:= 0) else (counter := !counter +1; raise wrong_pass))); retrieve = (fun (entered_password: passwd) (amount_withdraw: int) -> (if !counter < 5 then (if entered_password = !password then (if (amount_withdraw >= 0 && amount_withdraw < !balance) then (balance:=!balance-amount_withdraw; counter:=0) else (if (amount_withdraw < !balance) then (raise negative_amount; counter:=!counter + 1) else (counter:=!counter+1; raise not_enough_balance) ) ) else (counter:=!counter+1; raise wrong_pass)) else (counter:=!counter+1; raise too_many_failures)) ); deposit = (fun (entered_password: passwd) (amount_deposit: int) -> (if !counter < 5 then (if (entered_password = !password) then ( if amount_deposit >= 0 then (balance := !balance + amount_deposit; counter:= 0) else (counter:=!counter+1;raise negative_amount)) else (counter := !counter + 1;raise wrong_pass)) else (counter:=!counter+1;raise too_many_failures))); show_balance = (fun (entered_password: passwd) -> (if !counter < 5 then ( if (entered_password = !password) then (counter := 0; !balance) else (counter:=!counter+1;raise wrong_pass)) else (counter:=!counter+1;raise too_many_failures))); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edges = g.edges in let rec neighbours_rec input_list output_list : ('a * weight) list = match input_list with | hd::hd2::tl -> (match hd with | (first, second, third) -> if first = vertex then (let output_list = output_list @ [(second, third)] in neighbours_rec (hd2::tl) (output_list)) else neighbours_rec (hd2::tl) output_list ) | hd::_ -> (match hd with | (first, second, third) -> if first = vertex then (let output_list = output_list @ [(second, third)] in output_list) else output_list ) in neighbours_rec edges [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let counter = ref 0 in let password = ref initial_pass in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> ( if (old_pass = !password) then (password := new_pass; counter:= 0) else (counter := !counter +1; raise wrong_pass))); retrieve = (fun (entered_password: passwd) (amount_withdraw: int) -> (if !counter < 5 then (if entered_password = !password then (if (amount_withdraw >= 0 && amount_withdraw < !balance) then (balance:=!balance-amount_withdraw; counter:=0) else (if (amount_withdraw < !balance) then (raise negative_amount; counter:=!counter + 1) else (counter:=!counter+1; raise not_enough_balance) ) ) else (counter:=!counter+1; raise wrong_pass)) else (counter:=!counter+1; raise too_many_failures)) ); deposit = (fun (entered_password: passwd) (amount_deposit: int) -> (if !counter < 5 then (if (entered_password = !password) then ( if amount_deposit >= 0 then (balance := !balance + amount_deposit; counter:= 0) else (counter:=!counter+1;raise negative_amount)) else (counter := !counter + 1;raise wrong_pass)) else (counter:=!counter+1;raise too_many_failures))); show_balance = (fun (entered_password: passwd) -> (if !counter < 5 then ( if (entered_password = !password) then (counter := 0; !balance) else (counter:=!counter+1;raise wrong_pass)) else (counter:=!counter+1;raise too_many_failures))); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edges = g.edges in let rec neighbours_rec input_list output_list : ('a * weight) list = match input_list with | hd::hd2::tl -> (match hd with | (first, second, third) -> if first = vertex then (let output_list = output_list @ [(second, third)] in neighbours_rec (hd2::tl) (output_list)) else neighbours_rec (hd2::tl) output_list ) | hd::_ -> (match hd with | (first, second, third) -> if first = vertex then (let output_list = output_list @ [(second, third)] in output_list) else output_list ) in neighbours_rec edges [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let curr_pass = ref initial_pass in let balance = ref 0 in let fails = ref 0 in let check_pass = function pass -> if !fails = 5 then raise too_many_failures else if pass = !curr_pass then (fails := 0; true) else (fails := 1 + !fails; false) in let update_pass = fun pass new_pass -> if !curr_pass = pass then (fails := 0 ;curr_pass := new_pass) else (fails := !fails + 1; raise wrong_pass) in let retrieve = fun pass amount -> if not (check_pass pass) then raise wrong_pass else if amount < 0 then raise negative_amount else if amount > !balance then raise not_enough_balance else balance := !balance - amount in let deposit = fun pass amount -> if not (check_pass pass) then raise wrong_pass else if amount < 0 then raise negative_amount else balance := !balance + amount in let show_balance = function pass -> if not (check_pass pass) then raise wrong_pass else !balance 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 filter edges vertex acc = match edges with | [] -> acc | x::xs -> let v1,v2,w = x in if v1 = vertex then filter xs vertex ((v2,w)::acc) else filter xs vertex acc in filter 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 rec contains x l = match l with | [] -> false | h::t -> h=x || contains x t in let n,w = node in if contains n visited then raise Fail else if n = b then ([b],w) else let voisins = neighbours g n in let path,cost = aux_list (voisins) (n::visited) in let paths = n::path in let total_cost = w+cost in (paths, total_cost) 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)= let n,w = node in let rec contains x l = match l with | [] -> false | h::t -> h=x || contains x t in if contains n visited then fc() else if n = b then sc([b], w) else let voisins = neighbours g n in let vis = n::visited in let acc = (function (path, cost) -> sc (n::path, cost+w)) in aux_list (voisins) (vis) fc acc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | h::t -> let f = function () -> aux_list t visited fc sc in aux_node h visited f sc in let fail = function() -> raise Fail in aux_node (a,0) [] fail (function l -> l) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec get_longest l = match l with | [] -> None | [single_el] -> Some single_el | h::xs when xs <> [] -> ( match xs with | [] -> None | m::t -> let _,w1 = h in let _,w2 = m in match w1 >= w2 with | true -> get_longest (h::t) | false -> get_longest (m::t) ) | _ -> None in get_longest(find_all_paths g a b);; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.