text
stringlengths 0
601k
|
---|
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 count = ref 0 in let mypwd = ref initial_pass in let balance = ref 0 in { update_pass = (fun pwd1 pwd2 -> if (pwd1 = !mypwd) then (mypwd := pwd2; count := 0; ) else (count := !count + 1; raise wrong_pass)) ; deposit = (fun pwd x -> if !count >= 5 then raise too_many_failures else (if (pwd = !mypwd) then (count := 0 ; if x < 0 then raise negative_amount else balance := !balance + x) else (count := !count + 1; raise wrong_pass) )); retrieve = (fun pwd x -> if !count >= 5 then raise too_many_failures else (if (pwd = !mypwd) then (count := 0 ; if x < 0 then raise negative_amount else ( if !balance < x then raise not_enough_balance else balance := !balance - x) ) else (count := !count + 1; raise wrong_pass) )); show_balance = (fun pwd -> if !count >= 5 then raise too_many_failures else (if (pwd = !mypwd) then (count := 0 ; !balance) else (count := !count + 1; raise wrong_pass) ) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec neighbours_rec edges vertex result = match edges with | [] -> result | (v1, v2, w) :: t -> if v1 = vertex then neighbours_rec t vertex ((v2,w)::result) else neighbours_rec t vertex result in neighbours_rec 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) (totalw: weight): ('a list * weight) = let (v, w) = node in if (List.mem v visited) then raise Fail else if (v = b) then (visited @ [b], totalw + w) else ( aux_list (neighbours g v) ( visited @ [v] ) (totalw + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w: weight): ('a list * weight) = match nodes with | [] -> raise Fail | node :: t -> try aux_node node visited w with Fail -> aux_list t 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) (totalw: weight) fc sc = let (v, w) = node in if (List.mem v visited) then fc () else if (v = b) then sc visited totalw v w else aux_list (neighbours g v) ( visited @ [v] ) (totalw + w) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w: weight) fc sc = match nodes with | [] -> fc () | node :: t -> let fc2 = fun () -> aux_list t visited w fc sc in aux_node node visited w fc2 sc in let sc = fun visited totalw v w -> (visited @ [v], totalw + w) in aux_list (neighbours g a) [a] 0 (fun () -> raise Fail) sc;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a): ('a list * weight) option = let rec find_longest list max max_lst: ('a list * weight) option = match list with | [] -> if max = 0 then None else Some (max_lst, max) | (lst, w)::xs -> if w > max then find_longest xs w lst else find_longest xs max max_lst in find_longest (find_all_paths g a b) 0 [] ;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let counter = ref 0 in let balance = ref 0 in { update_pass = (fun curpass newpass -> if curpass = !password then ( password := newpass; counter :=0; ) else ( counter := !counter + 1; raise wrong_pass) ); retrieve = (fun pass amnt -> if !counter > 4 then raise too_many_failures else( if pass = !password then ( if amnt < 0 then ( raise negative_amount ) else ( if !balance >= amnt then ( balance := !balance - amnt; counter := 0; ) else raise not_enough_balance)) else (counter := !counter + 1; raise wrong_pass) ) ); deposit = (fun pass amnt -> if !counter > 4 then raise too_many_failures else( if pass = !password then ( if amnt < 0 then ( raise negative_amount ) else( balance := !balance + amnt; counter := 0; )) else (counter := !counter + 1; raise wrong_pass) ) ); show_balance = (fun pass -> if !counter > 4 then raise too_many_failures else ( if pass = !password then ( counter := 0; !balance ) else (counter := !counter + 1; print_string !password; raise wrong_pass) ) ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let startswith x = let (fst, _, _) = x in fst = vertex in List.map (fun x -> let (_, snd, thrd) = x in (snd, thrd)) (List.filter startswith 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 (visited@[fst node], snd node) else if List.exists (fun n -> (fst node) = n) visited then raise Fail else aux_list (List.map (fun (a1, w1) -> (a1, w1 + (snd node))) (neighbours g (fst node))) (visited@[fst node]) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |h::tl -> try aux_node h 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)= if (fst node) = b then sc ([b], (snd node)) else if List.exists (fun a -> (fst node) = a) visited then fc () else aux_list (neighbours g (fst node)) ((fst node)::visited) fc (fun (x1, y1) -> sc ((fst node)::x1, y1 + (snd node))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |h::tl -> aux_node h visited (fun _ -> aux_list tl visited fc sc) sc in aux_node (a, 0) [] (fun _ -> raise Fail) (fun (x,y) -> (x,y));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match find_all_paths g a b with |[] -> None |h::tl -> Some (List.fold_left (fun a b -> if (snd b) > (snd a) then b else a) h tl);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let counter = ref 0 in let balance = ref 0 in { update_pass = (fun curpass newpass -> if curpass = !password then ( password := newpass; counter :=0; ) else ( counter := !counter + 1; raise wrong_pass) ); retrieve = (fun pass amnt -> if !counter > 4 then raise too_many_failures else( if pass = !password then ( if amnt < 0 then ( raise negative_amount ) else ( if !balance >= amnt then ( balance := !balance - amnt; counter := 0; ) else raise not_enough_balance)) else (counter := !counter + 1; raise wrong_pass) ) ); deposit = (fun pass amnt -> if !counter > 4 then raise too_many_failures else( if pass = !password then ( if amnt < 0 then ( raise negative_amount ) else( balance := !balance + amnt; counter := 0; )) else (counter := !counter + 1; raise wrong_pass) ) ); show_balance = (fun pass -> if !counter > 4 then raise too_many_failures else ( if pass = !password then ( counter := 0; !balance ) else (counter := !counter + 1; print_string !password; raise wrong_pass) ) ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let startswith x = let (fst, _, _) = x in fst = vertex in List.map (fun x -> let (_, snd, thrd) = x in (snd, thrd)) (List.filter startswith 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 (List.append visited [fst node], snd node) else if List.exists (fun n -> (fst node) = n) visited then raise Fail else aux_list (List.map (fun (a1, w1) -> (a1, w1 + (snd node))) (neighbours g (fst node))) (List.append visited [fst node]) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |h::tl -> try aux_node h 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)= if (fst node) = b then sc ([b], (snd node)) else if List.exists (fun a -> (fst node) = a) visited then fc () else aux_list (neighbours g (fst node)) ((fst node)::visited) fc (fun (x1, y1) -> sc ((fst node)::x1, y1 + (snd node))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |h::tl -> aux_node h visited (fun _ -> aux_list tl visited fc sc) sc in aux_node (a, 0) [] (fun _ -> raise Fail) (fun (x,y) -> (x,y));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match find_all_paths g a b with |[] -> None |h::tl -> Some (List.fold_left (fun a b -> if (snd b) > (snd a) then b else a) h tl);; |
let open_account (initial_pass: passwd) : bank_account = let (curr_pass : passwd ref ) = ref "" in let (attempts: int ref) = ref 0 in let (balance: int ref) = ref 0 in let check_attempts () = if !attempts >= 5 then raise too_many_failures in let check_neg amt = if (amt < 0) then raise negative_amount in let count_password pass = if not (pass = !curr_pass) then attempts := !attempts + 1 else attempts := 0 in let check_password pass = if not (pass = !curr_pass) then raise wrong_pass in let update_pass p_old p_new = count_password p_old; check_password p_old; attempts := 0; curr_pass := p_new; in let retrieve p amt = check_attempts (); count_password p; check_password p; check_neg amt; if (!balance < amt) then raise not_enough_balance else balance := !balance - amt in let deposit p amt = check_attempts (); count_password p; check_password p; check_neg amt; balance := !balance + amt in let show_balance p : int = check_attempts (); count_password p; check_password p; !balance in update_pass "" initial_pass; { update_pass = update_pass; retrieve = retrieve; deposit = deposit; show_balance = show_balance } ;; let graph0 = {nodes= ["a"]; edges=[]} in let graph1 = {nodes= ["a";"b"]; edges=[("a","b",1)]} in let graph2 = {nodes= ["a";"b"; "c"]; edges=[("b","c",2)]} in [ ((graph0,"a"),[]); ((graph1,"a"),[("b",1)]); ((graph1,"b"),[]); ((graph2,"a"),[]); ((graph2,"b"),[("c",2)]) ] ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec find_out_neighbours e' acc = match e' with | (v, n, w)::tl when v=vertex -> find_out_neighbours tl ((n,w)::acc) | _::tl -> find_out_neighbours tl acc | [] -> acc in find_out_neighbours g.edges [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (path: 'a list) (cumul : weight) : ('a list * weight) = let (n,_) = node in if (n=b) then ((path @ [n]), cumul) else aux_list (neighbours g n) visited (path @ [n]) cumul and aux_list (nodes: ('a * weight) list) (visited: 'a list) (path: 'a list) (cumul : weight) : ('a list * weight) = match nodes with | [] -> raise Fail | (n, w)::tl -> if List.mem n visited then aux_list tl visited path cumul else try aux_node (n,w) (n::visited) path (cumul+w) with Fail -> aux_list tl visited path cumul in if a=b then ([a],0) else aux_list (neighbours g a) [a] [a] 0 ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (n,w) = node in if (n=b) then sc (n,w) else let sc2 = (fun (n',w') -> let (p, cw)= sc (n,w) in ((p @ [n']),cw+w')) in aux_list (neighbours g n) visited fc sc2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (n, w) :: tl -> if List.mem n visited then fc () else let sc2 = sc in let fc2 = fun () -> aux_list tl visited fc sc in aux_node (n,w) (n::visited) fc2 sc2 in if a=b then ([a], 0) else aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun (n,w) -> (([a]@[n]), w)) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec search_longest (paths : ('a list * weight) list) (max_path : 'a list) (max_w : weight) : 'a list * weight = match paths with | [] -> (max_path, max_w) | (path, w) :: tl -> if w >= max_w then search_longest tl path w else search_longest tl max_path max_w in let all_paths = find_all_paths g a b in if all_paths=[] then None else Some (search_longest all_paths [] 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 pass_1 pass_2 -> if pass_1 = !password then let _= attempts := 0 in password := pass_2 else let _= attempts := !attempts + 1 in raise wrong_pass); retrieve = (fun pass amount -> if attempts >= ref 5 then raise too_many_failures else if pass = !password then (let _= attempts := 0 in if amount > !balance then raise not_enough_balance else if amount < 0 then raise negative_amount else balance := !balance - amount) else let _= attempts := !attempts + 1 in raise wrong_pass ); deposit = (fun pass amount -> if attempts >= ref 5 then raise too_many_failures else if pass = !password && amount < 0 then raise negative_amount else if pass = !password && amount >= 0 then let _= attempts := 0 in balance := !balance + amount else let _= attempts := !attempts +1 in raise wrong_pass); show_balance = (fun pass -> if attempts >= ref 5 then raise too_many_failures else if pass = !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 find_nodes n l = match n with |[] -> l |(v1,v2,w)::xs -> if v1 = vertex then find_nodes (xs) ((v2,w)::l) else find_nodes (xs) l in find_nodes 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) (total_weight: int): ('a list * weight) = let (v1,w1) = node in if v1 = b then ((List.rev (v1::visited)), (total_weight+w1)) else if List.mem v1 visited then raise Fail else let list_neighbours = neighbours g v1 in match list_neighbours with |[] -> raise Fail |_ -> aux_list list_neighbours (v1::visited) (total_weight+w1) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total_weight: int): ('a list * weight) = match nodes with |[] -> raise Fail |x::xs -> let (v2,w2) = x in try aux_node (v2,w2) visited total_weight with Fail -> aux_list xs visited total_weight in match neighbours g a with |[] -> raise Fail |neighbours_list -> aux_list neighbours_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: int) fc sc : ('a list * weight)= let (v1,w1) = node in if v1 = b then sc ((List.rev (v1::visited)), (total_weight+w1)) else if List.mem v1 visited then fc () else let list_neighbours = neighbours g v1 in aux_list list_neighbours (v1::visited) (total_weight+w1) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total_weight: int) fc sc : ('a list * weight) = match nodes with |[] -> fc () |x::xs -> let (v2,w2) = x in let suc2 = sc in let fail2 = fun () -> aux_list xs visited total_weight fc sc in aux_node (v2,w2) visited total_weight fail2 suc2 in aux_node (a,0) [] 0 (fun () -> raise Fail) (fun l -> l);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let compare l1 l2 = match l1 with | None -> let (v2, w2) = l2 in Some(v2,w2) | Some(v1,w1) -> let (v2, w2) = l2 in if w2 > w1 then Some (v2, w2) else Some(v1, w1) in List.fold_left compare None (find_all_paths g a b);; |
let open_account (initial_pass: passwd) : bank_account = let init = ref initial_pass in let counter = ref 0 in let amount_in = ref 0 in let updateco passwd = counter := !counter +1; raise wrong_pass in { update_pass = (fun oldp newp-> if !init = oldp then (init := newp ; counter := 0 ) else if (!counter <=5 && !init != oldp) then updateco oldp else if (!counter > 5 && !init != oldp) then updateco oldp ) ; deposit = (fun oldp (amount:int) -> if (!counter >= 5) then raise too_many_failures else ( if !init = oldp then if amount >= 0 then (amount_in := !amount_in + amount; counter := 0 ) else (raise negative_amount; counter := 0 ) else updateco oldp)); retrieve = (fun oldp(amount:int) -> if (!counter >=5) then raise too_many_failures else ( if !init = oldp then (if amount < 0 then (raise negative_amount; counter := 0 ) else (counter := 0; (if !amount_in >= amount then amount_in := !amount_in - amount else raise not_enough_balance))) else updateco oldp)); show_balance = (fun oldp -> if (!counter >=5) then raise too_many_failures else( if !init = oldp then (counter := 0; !amount_in) else updateco oldp))} ;; |
let neighbours g vertex = List.fold_left (fun a (x, y, z) -> if x=vertex then (y,z)::a else a) [] g.edges;; let rec get_nth mylist index = match mylist with | [] -> raise (Failure "empty list") | first::rest -> if index = 0 then first else get_nth rest (index-1) ;; let pair_mode l = if List.length l < 2 then failwith "List too small." else let (_ :: no_front) = l in let (_ :: no_back) = List.rev l in let no_back = List.rev no_back in List.combine no_back no_front;; let get_tuple anode= let (a,b,c) = anode in (a, b);; let rec find x lst = match lst with | [] -> 10000 | h :: t -> if x = h then 0 else 1 + find x t;; let rec matchback list3 list2 acc= let tupleoflist3 = (List.map get_tuple list3) in match list2 with |[] -> acc |x::tl -> matchback list3 tl ((find x tupleoflist3)::acc) ;; let rec matchba2 index list3 acc = let index2 = List.filter (fun x -> x <10000) index in match index2 with |[]->acc |x::tl -> matchba2 tl list3 ((get_nth list3 x)::acc);; let get_weights anode= let (a,b,c) = anode in c;; let sumup finallist = let rec sumup2 finallist acc = match finallist with |[]->acc |x::tl -> sumup2 tl (acc+ (get_weights x)) in sumup2 finallist 0;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let get_nodes anode= let (s,h) = anode in s in let rec aux_node node visited = let thelist = List.map get_nodes (neighbours g node) in if node = b then [b] else if List.mem node visited then raise Fail else node::(aux_list thelist (node::visited)) and aux_list nodes visited = match nodes with | [] -> raise Fail | h::t -> try aux_node h visited with |Fail -> aux_list t visited in let pathlist = aux_node a [] in let list2 = pair_mode pathlist in let index = matchback g.edges list2 [] in let final = matchba2 index g.edges [] in let weights = sumup final in (pathlist, weights);; |
let find_path' g a b = let rec get_nth mylist index = match mylist with | [] -> raise (Failure "empty list") | first::rest -> if index = 0 then first else get_nth rest (index-1) in let pair_mode l = if List.length l < 2 then failwith "List too small." else let (_ :: no_front) = l in let (_ :: no_back) = List.rev l in let no_back = List.rev no_back in List.combine no_back no_front in let get_tuple anode= let (a,b,c) = anode in (a, b) in let rec find x lst = match lst with | [] -> raise (Failure "Not Found") | h :: t -> if x = h then 0 else 1 + find x t in let rec matchback list3 list2 acc= let tupleoflist3 = (List.map get_tuple list3) in match list2 with |[] -> acc |x::tl -> matchback list3 tl ((find x tupleoflist3)::acc) in let rec matchba2 index list3 acc = match index with |[]->acc |x::tl -> matchba2 tl list3 ((get_nth list3 x)::acc) in let get_weights anode= let (a,b,c) = anode in c in let sumup finallist = let rec sumup2 finallist acc = match finallist with |[]->acc |x::tl -> sumup2 tl (acc+ (get_weights x)) in sumup2 finallist 0 in let rec aux_node node visited func1 func2 = let get_nodes anode= let (s,h) = anode in s in let thelist = List.map get_nodes (neighbours g node) in let thecheck = (List.mem node visited) || (thelist = []) in if thecheck then (func1 ()) else aux_list thelist (node::visited) func1 (fun k -> func2 (node::k)) and aux_list nodes visited func1 func2 = match nodes with | [] -> func1 () | [head] -> if head = b then func2 [b] else aux_node head visited func1 func2 | head::tl ->if head = b then func2 [b] else aux_node head visited (fun () -> aux_list tl visited func1 func2) func2 in let pathlist = aux_node a [] (fun () -> raise Fail) (fun r->r) in let list2 = pair_mode pathlist in let index = matchback g.edges list2 [] in let final = matchba2 index g.edges [] in let weights = sumup final in (pathlist, weights);; let pair_modeversion2 l = if List.length l < 2 then failwith "List too small." else let (_ :: no_front) = l in let (_ :: no_back) = List.rev l in let no_back = List.rev no_back in List.combine no_back no_front;; let get_tupleversion2 anode= match anode with | (a,b,c) -> (a,b);; let get_weightsversion2 anode= match anode with | (a,b,c) -> c;; let get_first anode= match anode with | (a,b,c) -> a;; let sumupversion2 finallist = let rec sumup2version2 finallist acc = match finallist with |[]->acc |x::tl -> sumup2version2 tl (acc+ (get_weights x)) in sumup2version2 finallist 0;; let get_tupleversion2 anode= match anode with | (a,b,c) -> (a,b);; let getlistversion2 g = let alledges = g.edges in let rec getnewlistversion2 alledges acc = match alledges with | []-> acc | x::tl -> getnewlistversion2 tl ((get_tupleversion2 x)::acc) in getnewlistversion2 alledges [] let rec list_pathversion2 g a b = let neighbors g a condition = let edge l (b, c) = if b = a && condition c then c :: l else if c = a && condition b then b :: l else l in List.fold_left edge [] (getlistversion2 g) in match b with | [] -> raise Fail | x :: _ -> if x = a then [b] else let thelist = neighbors g x (fun c -> not (List.mem c b)) in List.concat (List.map (fun c -> list_pathversion2 g a (c :: b)) thelist) let rec findversion2 x lst = match lst with | [] -> 10000 | h :: t -> if x = h then 0 else 1 + findversion2 x t;; let rec get_nthversion2 mylist index = match mylist with | [] -> raise Fail | first::rest -> if index = 0 then first else get_nthversion2 rest (index-1) ;; let rec matchbackversion2 list3 list2 acc= let tupleoflist3 = (List.map get_tupleversion2 list3) in match list2 with |[] -> acc |x::tl -> matchbackversion2 list3 tl ((findversion2 x tupleoflist3)::acc) ;; let rec matchba2version2 index list3 acc = if List.for_all (fun x -> x <10000) index then match index with |[]->acc |x::tl ->matchba2version2 tl list3 ((get_nthversion2 list3 x)::acc) else [];; let rec prelist index listofnodes acc= match index with |[]->acc |x::tl -> prelist tl listofnodes (get_nthversion2 listofnodes x::acc);; let sumup finallist = let rec sumup2 finallist acc = match finallist with |[]->acc |x::tl -> sumup2 tl (acc+ (get_weights x)) in sumup2 finallist 0;; let nodelistfunc finallist = let rec func2 finallist acc = match finallist with |[]->acc |x::tl -> func2 tl ((get_first x) :: acc) in func2 finallist [];; let store_sum g a b = let find_all_paths3 g a b = let find_all_paths2 g a b = let pathlist = list_pathversion2 g a [b] in let rec foreachpath pathlist acc= match pathlist with | []-> acc | x::tl -> let list2 = pair_modeversion2 x in let index = matchbackversion2 g.edges list2 [] in let final = matchba2version2 index g.edges [] in foreachpath tl (final::acc) in foreachpath pathlist [] in List.filter (fun x -> x != []) (find_all_paths2 g a b) in let last = find_all_paths3 g a b in let rec get x bcc= match x with |[] -> bcc |q:: qr-> let wei = sumup q in get qr (wei::bcc) in get last [] ;; let haha g a b = let find_all_paths3 g a b = let find_all_paths2 g a b = let pathlist = list_pathversion2 g a [b] in let rec foreachpath pathlist acc= match pathlist with | []-> acc | x::tl -> let list2 = pair_modeversion2 x in let index = matchbackversion2 g.edges list2 [] in let final = matchba2version2 index g.edges [] in foreachpath tl (final::acc) in foreachpath pathlist [] in List.filter (fun x -> x != []) (find_all_paths2 g a b) in let last = find_all_paths3 g a b in let rec get x bcc= match x with |[] -> bcc |q:: qr-> let nodelist = List.rev (nodelistfunc q) in get qr (nodelist::bcc) in get last [] ;; let find_a g a b = let nlist = haha g a b in let rec append nlist acc= match nlist with |[] -> acc |q:: qr-> let nodelist = q@ [b] in append qr (nodelist::acc) in append nlist [] ;; let rec zip paired_lists = match paired_lists with | [], [] -> [] | h1::t1, h2::t2 -> (h1, h2)::(zip (t1, t2)) | _, _ -> failwith "oops, the lists seems to have different lengths" ;; let uniq_cons x xs = if List.mem x xs then xs else x :: xs let remove_from_right xs = List.fold_right uniq_cons xs [];; |
let find_longest_path g a b = let list1= remove_from_right (List.rev(store_sum g a b)) in if list1 = [] then None else let sorted = List.sort compare list1 in let greatestele = List.nth sorted ((List.length sorted) -1) in let index = findversion2 greatestele list1 in let all =find_all_paths g a b in Some (List.nth all index) ;; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let actual_pass = ref initial_pass in let num_err = ref 0 in { update_pass = (fun input1 -> (fun input2 -> if input1 = !actual_pass then (actual_pass :=input2; num_err := 0;) else (incr num_err ; raise wrong_pass; ) )); retrieve= (fun input1 -> (fun input2 -> if !num_err >= 5 then raise too_many_failures else ( if input1 = !actual_pass then ( if input2 > 0 then (if !balance - input2 >= 0 then ( balance := !balance - input2; num_err := 0; ) else raise not_enough_balance;) else raise negative_amount; ) else (incr num_err; raise wrong_pass;) ))); deposit = (fun input1 -> (fun input2 -> if !num_err >= 5 then raise too_many_failures else ( if input1 = !actual_pass then ( if input2 >= 0 then ( balance := !balance + input2 ; num_err := 0; ) else (raise negative_amount) ) else (incr num_err; raise wrong_pass; ) ))); show_balance= (fun input1 -> if !num_err >= 5 then raise too_many_failures else ( if input1 = !actual_pass then (num_err := 0; !balance;) else (incr num_err; raise wrong_pass; ) );) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let list = g.edges in List.fold_right (fun triple acc -> let (a,b,c) = triple in if a=vertex then (b,c)::acc else acc) list [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (curr_w : int) : ('a list * weight) = let (x,y) = node in if List.mem x visited then (raise Fail) else ( if x == b then ((visited @ [x], curr_w + y ) ) else ( try aux_list (neighbours g x) (visited @ [x]) (curr_w + y ) with Fail -> raise Fail ) ;) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (curr_w : int) : ('a list * weight) = match nodes with | hd :: tl -> (try aux_node hd visited curr_w with Fail -> aux_list tl visited curr_w) | [] -> raise Fail 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) (curr_w : int) success failure : ('a list * weight) = let (x,y) = node in if List.mem x visited then failure () else ( if x == b then success(node, visited, curr_w) else ( aux_list (neighbours g x) (visited @ [x]) (curr_w + y ) success failure ;)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (curr_w : int) success failure : ('a list * weight) = match nodes with | hd :: tl -> (let failure2 = fun() -> aux_list tl visited curr_w success failure in let succes2 = success in aux_node hd visited curr_w succes2 failure2) | [] -> failure () in aux_node (a,0) [] 0 (fun (node,visited,curr_w) -> let (x,y) = node in (visited @ [x], curr_w + y ) ) (fun () -> raise Fail );; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = try Some (find_path' g a b) with Fail -> None;; |
let open_account (initial_pass: passwd) : bank_account = let userPwd = ref initial_pass in let balance = ref 0 in let counter = ref 0 in let correct_password (attempt : passwd) : bool = if (attempt <> !userPwd) then raise wrong_pass else true in let positive_amount (amount : int) = if (amount < 0) then raise negative_amount else true in let enough_balance (amount : int) = if ((!balance - amount) < 0) then raise not_enough_balance else true in { update_pass = (fun (old : passwd) (update : passwd) -> try if (correct_password old) then userPwd := update; counter := 0 with wrong_pass -> counter := !counter + 1; raise wrong_pass); retrieve = (fun (pwd : passwd) (withdrawal : int) -> try if (!counter >= 5) then raise too_many_failures else if (correct_password pwd && positive_amount withdrawal && enough_balance withdrawal) then balance := !balance - withdrawal; counter := 0 with |wrong_pass -> counter := !counter + 1; raise wrong_pass |negative_amount -> counter := 0; raise negative_amount |not_enough_balance -> counter := 0; raise not_enough_balance); deposit = (fun (pwd : passwd) (dep : int) -> try if (!counter >= 5) then raise too_many_failures else if (correct_password pwd && positive_amount dep) then balance := !balance + dep; counter := 0 with |wrong_pass -> counter := !counter + 1; raise wrong_pass |negative_amount -> counter := 0; raise negative_amount); show_balance = (fun (pwd : passwd) -> if (!counter >= 5) then raise too_many_failures else try if (correct_password pwd) then counter := 0; !balance with wrong_pass -> counter := !counter + 1; raise wrong_pass); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let graph_edges = g.edges in let rec make_neighbour_list (edges: ('a * 'a * weight) list) ((vertex: 'a)) : ('a * int) list = match edges with |[] -> [] |x :: xs -> match x with |(w, y, z) when w = vertex -> (y, z) :: make_neighbour_list xs vertex |(_,_,_) -> make_neighbour_list xs vertex in make_neighbour_list graph_edges vertex let rec findEdgeWeight (a : 'a) (b: 'a) (g: ('a * 'a * weight) list) : int = match g with |[] -> 0 |x :: xs -> let (start, end', y) = x in let (_, _, weight) = x in if (start, end', y) = (a, b, y) then weight else findEdgeWeight a b xs let rec findTotalWeight (path : 'a list) (g : 'a graph) : int = match path with |[] -> 0 |x :: [] -> 0 |x :: (y :: _ as z) -> findEdgeWeight x y g.edges + findTotalWeight z g;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let endPoint = b in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list) = let (city, _) = node in if (List.mem city visited) then raise Fail else match node with |(x, _) when x = endPoint -> [x] |(w, _) -> w :: aux_list (neighbours g w) (w :: visited) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list) = match nodes with |[] -> raise Fail |x :: xs -> try aux_node x visited with Fail -> aux_list xs visited in let path = aux_node (a, 0) [] in (path, findTotalWeight path g);; let neighbourstl (g: 'a graph) (vertex: 'a) : ('a * weight) list = let graph_edges = g.edges in let rec make_neighbour_list (edges: ('a * 'a * weight) list) (vertex: 'a) acc : ('a * int) list = match edges with |[] -> acc |x :: xs -> match x with |(w, y, z) when w = vertex -> make_neighbour_list xs vertex ((y,z) :: acc) |(_,_,_) -> make_neighbour_list xs vertex acc in make_neighbour_list graph_edges vertex [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let fail_case () = raise Fail in let success_case x acc = x :: acc in let endPoint = b in let rec aux_node (node: 'a * weight) (remaining: ('a * weight) list) (visited : 'a list) (acc : 'a list) fc sc : ('a list)= let (city, _) = node in if (List.mem city visited) then fc () else match node with |(x, _) when x = endPoint -> sc x acc |(w, _) -> let fail_aux_list = fun () -> aux_list remaining visited acc fc sc in aux_list (neighbourstl g w) (w :: visited) (sc w acc) fail_aux_list success_case and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc: 'a list) fc sc : ('a list) = match nodes with |[] -> fc () |x :: xs -> let fail_aux_node = fun () -> aux_list xs visited acc fc sc in aux_node x xs visited acc fail_aux_node success_case in let path = List.rev (aux_list (neighbours g a) [a] [a] fail_case success_case) in (path, findTotalWeight path g);; let rec removeRepeated (list: ('a list) list) : ('a list) list = match list with |[] -> [] |x :: xs -> if List.mem x xs then removeRepeated xs else x :: removeRepeated xs;; let rec finalAnswer (list: ('a list) list) (g: 'a graph) acc : ('a list * weight) list = match list with |[] -> acc |x :: xs -> finalAnswer xs g ((x, findTotalWeight x g) :: acc);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec longest (list: ('a list * weight) list) (max : ('a list * weight)) : ('a list * weight) option = match list with |[] -> None |x :: [] -> let (_, weight) = x in let (_, maxWeight) = max in if weight > maxWeight then Some x else Some max |x :: xs -> let (_, weight) = x in let (_, maxWeight) = max in if weight > maxWeight then longest xs x else longest xs max in let paths = find_all_paths g a b in longest paths ([], 0) ;; |
let open_account (initial_pass: passwd) : bank_account = let passW = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in { update_pass = (fun oldPass newPass -> if (oldPass = !passW) then (passW := newPass; attempts := 0) else (attempts := !attempts + 1; raise wrong_pass)); retrieve = (fun pass amt -> if (!attempts >= 5) then (raise too_many_failures) else if not (pass = !passW) then (attempts := (!attempts + 1); raise wrong_pass) else (if (amt > !balance) then (raise not_enough_balance) else if (amt < 0) then (raise negative_amount) else (balance := (!balance - amt)); attempts := 0)); deposit = (fun pass amt -> if (!attempts >= 5) then (raise too_many_failures) else if not (pass = !passW) then (attempts := (!attempts + 1); raise wrong_pass) else if (amt < 0) then (raise negative_amount) else (balance := (!balance + amt)); attempts := 0); show_balance = (fun pass -> if (!attempts >= 5) then (raise too_many_failures) else if not (pass = !passW) then (attempts := (!attempts + 1); raise wrong_pass) else (attempts := 0; !balance)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let mkList = (fun acc edge -> let (v, o, w) = edge in if (v = vertex) then ((o, w) :: acc) else acc) in List.fold_left mkList [] (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) = let (name, w2) = node in if (name = b) then (List.append visited [name], w + w2) else aux_list (neighbours g name) (List.append visited [name]) (w+w2) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w : weight) : ('a list * weight) = match nodes with | [] -> raise Fail | (name, w2)::tl -> if (List.mem name visited) then aux_list tl visited w else try aux_node (name, w2) visited w with Fail -> aux_list tl 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) fc sc : ('a list * weight)= let (name, w) = node in if (name = b) then sc ([name], w) else if not (List.mem name visited) then aux_list (neighbours g name) (List.append visited [name]) fc (fun (path, pathWeight) -> sc (List.append path [name], w + pathWeight)) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | (name, w)::rest -> let sc2 = sc in let fc2 = (fun () -> aux_list rest visited fc sc) in aux_node (name, w) visited fc2 sc2 | [] -> fc () in let (paths, weight) = aux_node (a, 0) [] (fun () -> raise Fail) (fun identity -> identity) in (List.rev paths, weight) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec findMax inList bestCombo maxW = match inList with | [] -> bestCombo | (valList, w)::rest -> if (w >= maxW) then (findMax rest (valList, w) w) else findMax rest bestCombo maxW in let ab = findMax (find_all_paths g a b) ([],0) 0 in if (ab = ([], 0)) then None else Some ab;; |
let open_account (initial_pass: passwd) : bank_account = let pas = ref initial_pass in let bal = ref 0 in let incorrect = ref 0 in {update_pass = (fun pwo pwn -> if pwo = !pas then (pas := pwn; incorrect := 0 ) else (incorrect := !incorrect + 1; raise wrong_pass)); retrieve= (fun pw i -> if !incorrect > 4 then raise too_many_failures else ( if i < 0 then raise negative_amount else (if pw = !pas then ( if i <= !bal then (incorrect := 0; bal := !bal - i; ) else ( incorrect := 0; raise not_enough_balance )) else (incorrect := !incorrect + 1; raise wrong_pass)))); deposit= (fun p i -> if !incorrect > 4 then (raise too_many_failures) else (if p <> !pas then (if !incorrect > 4 then (incorrect := !incorrect + 1; raise too_many_failures) else (incorrect := !incorrect + 1; raise wrong_pass)) else (if i < 0 then ( incorrect :=0; raise negative_amount ) else (bal := !bal + i; incorrect := 0)))); show_balance =(fun p -> if !incorrect > 4 then raise too_many_failures else (if p <> !pas then (if !incorrect > 4 then (incorrect := !incorrect + 1; raise too_many_failures) else (incorrect := !incorrect + 1; raise wrong_pass)) else (incorrect :=0; !bal)))} ;; |
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) exception NoneToGo;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((x,w) as node: 'a * weight) (visited : 'a list) : ('a list * weight) = if x = b then (visited, w) else aux_list (neighbours g x) (visited) w and aux_list (nodes: ('a * weight) list) (visited: 'a list) acc : ('a list * weight) = let rec helper (nodes) acc = match nodes with | [] -> raise NoneToGo | (x,w) :: xs -> try aux_node (x,w+acc) (x::visited) with NoneToGo -> helper xs acc in helper (List.filter (fun (u,w) -> not (List.mem u visited)) nodes) acc in try (let y= (aux_list (neighbours g a) [a] 0) in ((fun (u,t) -> (List.rev u, t)) y)) with NoneToGo -> raise Fail;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((x,w) as node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if x = b then (sc (x::[], w)) else (if (List.mem x visited) then fc () else aux_list (neighbours g x) (x::visited) fc (fun (t, v) -> sc (x::t, w+v)) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> (fc ()) | (x,w) :: xs -> aux_node (x,w) visited (fun () -> aux_list xs visited fc sc ) sc in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun (u,w) -> (a::u, w)) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec get_largest (ps: ('a list * weight) list ) ((vis, w) as cur_max): 'a list * weight = match ps with | [] -> cur_max | (visc, wc) :: xs -> if wc > w then get_largest xs (visc, wc) else get_largest xs cur_max in let paths = find_all_paths g a b in match paths with | [] -> None | x :: xs -> Some (get_largest paths ([], 0));; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let counter = ref 0 in { update_pass = (function oldPass -> function newPass -> if oldPass = !password then let _ = (counter := 0) in password := newPass else let _ = (counter := !counter + 1) in raise wrong_pass); retrieve = (function pass -> function amount -> if !counter = 5 then raise too_many_failures else (if pass = !password then let _ = (counter := 0) in if amount >= 0 then if amount <= !balance then balance := !balance - amount else raise not_enough_balance else raise negative_amount else let _ = (counter := !counter + 1) in raise wrong_pass ) ); deposit = (function pass -> function amount -> if !counter = 5 then raise too_many_failures else (if pass = !password then let _ = (counter := 0) in if amount >= 0 then balance := !balance + amount else raise negative_amount else let _ = (counter := !counter + 1) in raise wrong_pass ) ); show_balance = (function pass -> if !counter = 5 then raise too_many_failures else (if pass = !password then let _ = (counter := 0) in !balance else let _ = (counter := !counter + 1) in raise wrong_pass ) ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper edges vertex acc = match edges with | [] -> acc | h :: t -> match h with | (v,nei,weight) when v = vertex -> helper t v (acc @ [(nei,weight)]) | (_,_,_) -> helper t vertex acc in helper g.edges vertex [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (start,w) visited = if start = b then ([b],w) else if not (List.mem start visited) then let (path,acc) = aux_list (neighbours g start) (start :: visited) in (start :: path,acc + w) else raise Fail and aux_list (nei_list : ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nei_list 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 (start, w) visited fail success = if start = b then success ([b], w) else if not (List.mem start visited) then aux_list (neighbours g start) (start :: visited) fail (fun (path,acc) -> success (start :: path,acc + w)) else fail () and aux_list nodes visited fail success = match nodes with | [] -> fail () | h :: t -> aux_node h visited (fun () -> aux_list t visited fail success) success 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 = find_max (find_all_paths g a b) ;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in let check_pass pass = if pass = !password && !attempts < 5 then attempts := 0 else if pass = !password && !attempts >= 5 then raise too_many_failures else if (pass <> !password) then ( if (!attempts < 5) then (attempts := !attempts + 1; raise wrong_pass) else if (!attempts >= 5) then raise too_many_failures) in let update_pass (old_password:passwd) (new_password:passwd) = if old_password <> !password then (attempts := !attempts + 1; raise wrong_pass) else (attempts := 0; password := new_password) in let retrieve (pass:passwd) (money_retrieve: int) = check_pass pass; if money_retrieve < 0 then raise negative_amount else if !balance - money_retrieve < 0 then raise not_enough_balance else balance := !balance - money_retrieve in let deposit (pass:passwd) (money_deposit:int) = check_pass pass; if money_deposit < 0 then raise negative_amount else balance := !balance + money_deposit in let show_balance (pass:passwd) = check_pass pass; (!balance) in {update_pass; retrieve; deposit; show_balance};; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = notimplemented ();; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in let check_pass pass = if pass = !password && !attempts < 5 then attempts := 0 else if !attempts >= 5 then raise too_many_failures else if (!attempts < 5) then (attempts := !attempts + 1; raise wrong_pass) in let update_pass (old_password:passwd) (new_password:passwd) = if old_password <> !password then (attempts := !attempts + 1; raise wrong_pass) else (attempts := 0; password := new_password) in let retrieve (pass:passwd) (money_retrieve: int) = check_pass pass; if money_retrieve < 0 then raise negative_amount else if !balance - money_retrieve < 0 then raise not_enough_balance else balance := !balance - money_retrieve in let deposit (pass:passwd) (money_deposit:int) = check_pass pass; if money_deposit < 0 then raise negative_amount else balance := !balance + money_deposit in let show_balance (pass:passwd) = check_pass pass; (!balance) in {update_pass; retrieve; deposit; show_balance};; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = notimplemented ();; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in let update_pass (old_password:passwd) (new_password:passwd) = if old_password <> !password then raise wrong_pass else password := new_password in let retrieve (pass:passwd) (money_retrieve: int) = if pass = !password then if money_retrieve < 0 then raise negative_amount else if !balance - money_retrieve < 0 then raise not_enough_balance else balance := !balance - money_retrieve in let deposit (pass:passwd) (money_deposit:int) = if pass = !password then if money_deposit < 0 then raise negative_amount else balance := !balance + money_deposit in let show_balance (pass:passwd) = !balance in {update_pass; retrieve; deposit; show_balance};; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = notimplemented ();; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let password = ref initial_pass in let fail = ref 0 in { update_pass = ( fun (old_pass : passwd) -> fun (new_pass : passwd) -> if old_pass = !password then ( fail := 0; password := new_pass) else ( fail := !fail + 1; raise wrong_pass ) ) ; retrieve = (fun (pass : passwd) -> fun (amount: int) -> if !fail >= 5 then raise too_many_failures else ( if pass = !password then ( fail := 0; if amount < 0 then raise negative_amount else if amount > !balance then raise not_enough_balance else balance := !balance - amount ) else ( fail := !fail + 1; raise wrong_pass ) ) ) ; deposit = (fun (pass : passwd) -> fun (amount : int) -> if !fail >= 5 then raise too_many_failures else ( if pass = !password then ( fail := 0; if amount < 0 then raise negative_amount else balance := !balance + amount ) else ( fail := !fail + 1; raise wrong_pass ) ) ) ; show_balance = (fun (pass : passwd) -> if !fail >= 5 then raise too_many_failures else ( if pass = !password then ( fail := 0; !balance ) else ( fail := !fail + 1; raise wrong_pass ) ) ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f (l : ('a * weight) list) ((v1, v2, w): ('a * 'a * weight)) = if v1 = vertex then List.append l [(v2, w)] else l in List.fold_left f [] g.edges ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((v2, w): 'a * weight) (visited : ('a * weight) list) : ('a list * weight) = if v2 = b then ( let cum_w = List.fold_left (fun x -> fun (y, z) -> x + z) 0 visited in let l = List.fold_left (fun x -> fun (y, z) -> List.append x [y]) [] visited in (l, cum_w) ) else ( let ngb = neighbours g v2 in aux_list ngb visited ) and aux_list (nodes: ('a * weight) list) (visited: ('a * weight) list) : ('a list * weight) = match nodes with | [] -> raise Fail | (v2, w)::xs -> if List.mem_assoc v2 visited then aux_list xs visited else ( try aux_node (v2, w) (List.append visited [(v2, w)]) with Fail -> aux_list xs visited ) in aux_list (neighbours g a) [(a, 0)] ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((v2, w): 'a * weight) (visited : ('a * weight) list) fc sc : ('a list * weight)= if v2 = b then sc visited else ( let ngb = neighbours g v2 in aux_list ngb visited fc sc ) and aux_list (nodes: ('a * weight) list) (visited: ('a * weight) list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (v2, w)::xs -> if List.mem_assoc v2 visited then aux_list xs visited fc sc else ( let fc2 = fun () -> aux_list xs visited fc sc in aux_node (v2, w) (List.append visited [(v2, w)]) fc2 sc ) in let sc = fun visited -> let cum_w = List.fold_left (fun x -> fun (y, z) -> x + z) 0 visited in let l = List.fold_left (fun x -> fun (y, z) -> List.append x [y]) [] visited in (l, cum_w) in aux_list (neighbours g a) [(a, 0)] (fun () -> raise Fail) sc ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in if paths = [] then None else ( let sort_f ((path1, w1): 'a list * weight) ((path2, w2): 'a list * weight) = if w1 = w2 then 0 else if w1 < w2 then 1 else -1 in let sort_paths = List.sort sort_f paths in Some (List.hd sort_paths) ) ;; |
let open_account (initial_pass: passwd) : bank_account = let bal = ref 0 in let pass = ref initial_pass in let numFailPass = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass:passwd) -> if old_pass = !pass then ( numFailPass := 0; pass := new_pass; ) else ( numFailPass := !numFailPass + 1; raise wrong_pass ) ); retrieve = (fun (correct_pass: passwd) (withdraw: int) -> if !numFailPass < 5 then ( if withdraw < 0 then ( raise negative_amount; ) else if correct_pass = !pass then ( numFailPass := 0; if withdraw < !bal then bal := !bal - withdraw else if withdraw > !bal then raise not_enough_balance ) else ( numFailPass := !numFailPass + 1; raise wrong_pass) ) else raise too_many_failures ); deposit = (fun (correct_pass: passwd) (depo: int) -> if !numFailPass < 5 then ( if depo < 0 then ( raise negative_amount; ) else if correct_pass = !pass then ( numFailPass := 0; bal := !bal + depo; ) else ( numFailPass := !numFailPass + 1; raise wrong_pass ) ) else raise too_many_failures ); show_balance = (fun (correct_pass: passwd) -> if !numFailPass < 5 then ( if correct_pass = !pass then ( numFailPass := 0; !bal ) else ( numFailPass := !numFailPass + 1; raise wrong_pass ) ) else raise too_many_failures ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun collectNeighbors path -> let v1,v2,w = path in if v1 = vertex then ( collectNeighbors@[(v2,w)] ) else collectNeighbors ) [] g.edges let notVisitedYetFunction (vertex: 'a) (g: 'a graph) (visitedNodes: 'a list) = List.filter ( fun edge -> let node, _ = edge in not (List.mem node visitedNodes)) (neighbours g vertex);; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec dfs (vertex: 'a) (path : ('a list * weight)) (visitedNodes: 'a list) : ('a list * weight) = let notVisitedYet = notVisitedYetFunction vertex g visitedNodes in match notVisitedYet with | (nod, totalWeight)::tl -> let node, weight = path in if nod = b then (node@[nod], totalWeight + weight) else ( try dfs nod (node@[nod], totalWeight+weight) (visitedNodes@[nod]) with Fail -> dfs vertex path (visitedNodes@[nod]) ) | [] -> raise Fail in dfs a ([a], 0) [a];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec dfs (vertex: 'a) (path: ('a list * weight)) (visitedNodes: 'a list) sc fc = let notVisitedYet = notVisitedYetFunction vertex g visitedNodes in match notVisitedYet with | (nod, totalWeight)::tl -> let node, weight = path in if nod = b then sc (node@[b], totalWeight + weight) else ( dfs nod (node@[nod], totalWeight + weight) (visitedNodes@[nod]) sc ( fun () -> dfs vertex path (visitedNodes@[nod]) sc fc) ) | [] -> fc () in dfs a ([a], 0) [a] (fun x -> x) (fun () -> raise Fail);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let allPaths = find_all_paths g a b in let rec max f = match f with | [(path1,path2)] -> Some (path1,path2) | (path1, weight1) :: (path2,weight2) :: tl -> if weight1 > weight2 then max ((path1,weight1)::tl) else max ((path2,weight2)::tl) | [] -> None in max allPaths;; |
let open_account (initial_pass: passwd) : bank_account = let old_pass = ref initial_pass in let balance = ref 0 in let counter = ref 0 in {update_pass = (fun pass (new_pass : passwd) : unit -> if (pass = !old_pass) then (old_pass := new_pass) else (counter := !counter + 1; raise wrong_pass)); retrieve = (fun pass amount : unit -> if !counter >= 5 then raise too_many_failures else if pass <> !old_pass then (counter := !counter + 1; raise wrong_pass) else if amount > !balance then raise not_enough_balance else if amount < 0 then (counter := 0; raise negative_amount) else balance := (counter := 0; !balance - amount) ); deposit = (fun pass amount : unit -> if !counter >= 5 then raise too_many_failures else if pass <> !old_pass then (counter := !counter + 1; raise wrong_pass) else if amount < 0 then (counter := 0; raise negative_amount) else balance := (counter := 0; !balance + amount) ); show_balance = fun (pass : passwd) : int -> if !counter >= 5 then raise too_many_failures else if pass = !old_pass then let _ = counter in counter := 0; !balance else (counter := !counter + 1; raise wrong_pass);} ;; let first (tup: ('a * 'a * weight)) : 'a = let (a,b,c) = tup in a let snd_thd (tup: ('a * 'a * weight)) : ('a * weight) = let (a,b,c) = tup in (b, c);; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = match g.edges with | [] -> [] | _ -> List.rev (List.fold_left (fun (acc : ('a * weight) list) (our_edge : ('a * 'a * weight)) : ('a * weight) list -> if(first our_edge = vertex) then (List.cons (snd_thd our_edge) 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.mem (fst(node)) (visited)) then raise Fail else aux_list (neighbours g (fst(node))) (fst(node) :: visited) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = let acc = ([], 0) in match nodes with | [] -> raise Fail | h::t -> if (fst(h) = b) then ((fst(acc) @ [fst(h)]), snd(acc) + snd(h)) else try (fst(acc) @ [fst(h)] @ (fst(aux_node (h) (visited))), snd(acc) + snd(h) + (snd(aux_node (h) (visited)))) with Fail -> try aux_list t visited with Fail -> raise Fail in ([a] @ fst(aux_list (neighbours g a) [a]), snd(aux_list (neighbours g a) [a]));; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = try Some (find_path g a b) with Fail -> None;; |
let open_account (initial_pass: passwd) : bank_account = let wrong = ref 0 in let pass = ref initial_pass in let money = ref 0 in { update_pass = (fun input1 input2 -> if input1 = !pass then (wrong := 0; pass := input2) else raise wrong_pass); retrieve = (fun input1 input2 -> if input1 <> !pass then (wrong := !wrong + 1; if !wrong <=5 then raise wrong_pass else if !wrong > 5 then raise too_many_failures else ()) else if input1 = !pass then ( if !wrong >=5 then raise too_many_failures else (wrong := 0; if input2 < 0 then raise negative_amount else if input2 > !money then raise not_enough_balance else money := !money - input2)) else ()); deposit = (fun input1 input2 -> if input1 <> !pass then (wrong := !wrong + 1; if !wrong <=5 then raise wrong_pass else if !wrong > 5 then raise too_many_failures else ()) else if input1 = !pass then ( if !wrong >=5 then raise too_many_failures else ( wrong := 0; if input2 < 0 then raise negative_amount else money := !money + input2)) else ()); show_balance = (fun input1 -> if input1 <> !pass then (wrong := !wrong + 1; if !wrong <=5 then raise wrong_pass else raise too_many_failures) else ( if !wrong >=5 then raise too_many_failures else (wrong := 0; !money))) };; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper1 edges vertex t = match edges with | [] -> t ; | (a,b,c)::xs -> if vertex = a then let newt = (b,c)::t in helper1 xs vertex newt else helper1 xs vertex t in helper1 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) = if let o = fst(node) in List.mem o visited then raise Fail else let newvisited = fst(node) :: visited in match node with |(a,0) -> if a = b then ([a],0) else let nodess = neighbours g a in let y = aux_list nodess newvisited in (a::fst(y),snd(y)) ; |(n,w) when n = b -> ([],0); |(c,d)-> let nodess = neighbours g c in aux_list nodess newvisited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[]-> raise Fail; |(c,d)::xs-> try (let x =aux_node (c,d) visited in (c::fst(x),d+snd(x))) 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 newvisited = fst(node) :: visited in match node with |(a,0) -> if a = b then sc ([a],0) else (let nodess = neighbours g a in let y = aux_list nodess newvisited fc sc in sc (a::fst(y),snd(y))) ; |(n,w) when n = b -> sc ([b],w); |(c,f)-> let nodess = neighbours g c in aux_list nodess newvisited fc sc ; and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[]-> fc (); |(c,d)::xs-> if List.mem c visited then aux_list xs visited fc sc else (let newvisited = c :: visited in let fail2 = fun()-> aux_list xs newvisited fc sc in aux_node (c,d) visited fail2 sc) in aux_node (a,0) [] (fun()->raise Fail) (fun y -> y) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let c = find_all_paths g a b in if c = [] then None else let rec helper3 c (n,m) = match c with | [] -> Some (n,m) | (x,w)::xs -> if w>m then helper3 xs (x,w) else helper3 xs (n,m) in helper3 c ([],0);; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in { update_pass = (fun old_pass new_pass -> if old_pass = !pass then let _ = attempts := 0 in pass := new_pass else let _ = attempts := !attempts + 1 in raise wrong_pass); retrieve = (fun password amount -> if !attempts >= 5 then raise too_many_failures else if password = !pass then let _ = attempts := 0 in if amount < 0 then raise negative_amount else if !balance - amount >= 0 then balance := !balance - amount else raise not_enough_balance else let _ = attempts := !attempts + 1 in raise wrong_pass); deposit = (fun password amount -> if !attempts >= 5 then raise too_many_failures else if password <> !pass then let _ = attempts := !attempts + 1 in raise wrong_pass else if amount < 0 then raise negative_amount else let _ = attempts := 0 in balance := !balance + amount); show_balance = (fun password -> if !attempts >= 5 then raise too_many_failures else if password = !pass 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 edges = g.edges in let f = fun result edge -> match edge with | (v1, v2, weight) -> if v1 = vertex then result @ [(v2, weight)] else result | _ -> result in List.fold_left f [] 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 password = ref initial_pass in let failed_attempts = ref 0 in let money = ref 0 in { update_pass = (fun old_pass new_pass -> if old_pass = !password then (failed_attempts := 0; password := new_pass;) else (failed_attempts := !failed_attempts + 1; raise wrong_pass) ); retrieve = (fun pass amount -> if !failed_attempts = 5 then raise too_many_failures else if pass = !password then (if amount < 0 then raise negative_amount else if amount > !money then raise not_enough_balance else failed_attempts := 0; money := !money - amount;) else (failed_attempts := !failed_attempts + 1; raise wrong_pass) ); deposit = (fun pass amount -> if !failed_attempts = 5 then raise too_many_failures else if pass = !password then (if amount < 0 then raise negative_amount else failed_attempts := 0; money := !money + amount;) else (failed_attempts := !failed_attempts + 1; raise wrong_pass) ); show_balance = (fun pass -> if !failed_attempts = 5 then raise too_many_failures else if pass = !password then (failed_attempts := 0; !money) else (failed_attempts := !failed_attempts + 1; raise wrong_pass) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left ( fun ls (a, b, w) -> if a = vertex then (b, w) :: ls else ls ) [] g.edges;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.