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 (name, weight) = node in let list = neighbours g name in if name = b then ([], 0) else aux_list list visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | node :: t -> let (name, w) = node in if List.mem name visited then aux_list t visited else ( try let (list, weight) = aux_node node (name :: visited) in (name :: list, w+weight) with Fail -> aux_list t (name :: visited) ) in aux_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) fc sc : ('a list * weight)= let (name, weight) = node in let list = neighbours g name in if name = b then sc ([], 0) else aux_list list visited fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc() | node :: t -> let (name, w) = node in if List.mem name visited then aux_list t visited fc sc else aux_node node (name :: visited) (fun r -> aux_list t (name :: visited) fc sc) (fun r -> (let (list, weight) = r in sc (name :: list, w+weight))) in aux_list [(a, 0)] [] (fun r -> raise Fail) (fun r -> r);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec aux list best_path best_weight = match list with | [] -> best_path | h :: t -> let (list, weight) = h in if weight > best_weight then aux t h weight else aux t best_path best_weight in let all_paths = find_all_paths g a b in match all_paths with | [] -> None | h :: t -> let (list, weight) = h in Some (aux t h weight);; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let current_password = ref initial_pass in let attempt = ref 0 in let new_account : bank_account = { update_pass = (fun (old_pass : passwd) (new_pass : passwd) -> if (old_pass = !current_password) then let () = attempt := 0 in current_password := new_pass else let () = attempt := !attempt + 1 in raise wrong_pass ) ; retrieve = (fun (password : passwd) (amount : int) -> if (password = !current_password && !attempt < 5) then let () = (attempt := 0) in if (!balance >= amount && amount >= 0) then balance := (!balance - amount) else if (!balance < amount && amount >= 0) then raise not_enough_balance else raise negative_amount else let () = (attempt := !attempt + 1) in if !attempt > 5 then raise too_many_failures else raise wrong_pass ); deposit = (fun (password : passwd) amount -> if (password = !current_password && !attempt < 5) then if (amount >= 0) then let () = (attempt := 0) in balance := (!balance + amount) else raise negative_amount else let () = (attempt := !attempt + 1) in if !attempt > 5 then raise too_many_failures else raise wrong_pass ); show_balance = (fun password -> if (password = !current_password && !attempt < 5) then let () = (attempt := 0) in !balance else let () = (attempt := !attempt + 1) in if !attempt > 5 then raise too_many_failures else raise wrong_pass ); } in new_account ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edge_list = g.edges in let node_finder n l = match n with | (v1, v2, wt) when v1 = vertex -> (v2, wt) :: l | _ -> l in List.fold_right node_finder edge_list [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (cur_weight : weight) : ('a list * weight)= match node with | (dest, wt) when dest == b -> (visited @ [dest], cur_weight + wt) | (c, wt) when (List.mem c visited) -> raise Fail | (n, wt) -> aux_list (neighbours g n)( visited @ [n]) (cur_weight + wt) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (cur_weight : weight) : ('a list * weight) = match nodes with | [] -> raise Fail | (n, wt) :: xs -> try aux_node (n, wt) visited cur_weight with Fail -> aux_list xs visited cur_weight in aux_list (neighbours g a) [a] 0 ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= match node with | (v2, wt) when v2 = b -> (visited @ [v2], sc wt) | (v2, wt) when (List.mem v2 visited) -> fc () | (v2, wt) -> let new_sc = (fun x -> (sc x) + wt) in aux_list (neighbours g v2) (visited @ [v2]) fc new_sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (n, wt) :: xs -> let new_fc = (fun () -> aux_list xs visited fc sc) in aux_node (n, wt) visited new_fc sc in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun n -> n) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths g a b in match all_paths with | [] -> None | _ -> let cur_path = ref (List.nth all_paths 0) in let update_max (path : 'a list * weight) : unit = match path, !cur_path with | (p1, wt1), (p2, wt2) when wt1 >= wt2 -> cur_path := path | _, _ -> () in let rec do_all f l = match l with | [] -> () | x :: xs -> f x ; do_all f xs in let () = do_all update_max all_paths in Some !cur_path ;; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let current_password = ref initial_pass in let attempt = ref 0 in let new_account : bank_account = { update_pass = (fun (old_pass : passwd) (new_pass : passwd) -> if (old_pass = !current_password) then let () = attempt := 0 in current_password := new_pass else let () = attempt := !attempt + 1 in raise wrong_pass ) ; retrieve = (fun (password : passwd) (amount : int) -> if (password = !current_password && !attempt < 5) then let () = (attempt := 0) in if (!balance >= amount && amount >= 0) then balance := (!balance - amount) else if (!balance < amount && amount >= 0) then raise not_enough_balance else raise negative_amount else let () = (attempt := !attempt + 1) in if !attempt > 5 then raise too_many_failures else raise wrong_pass ); deposit = (fun (password : passwd) amount -> if (password = !current_password && !attempt < 5) then if (amount >= 0) then let () = (attempt := 0) in balance := (!balance + amount) else raise negative_amount else let () = (attempt := !attempt + 1) in if !attempt > 5 then raise too_many_failures else raise wrong_pass ); show_balance = (fun password -> if (password = !current_password && !attempt < 5) then let () = (attempt := 0) in !balance else let () = (attempt := !attempt + 1) in if !attempt > 5 then raise too_many_failures else raise wrong_pass ); } in new_account ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edge_list = g.edges in let node_finder n l = match n with | (v1, v2, wt) when v1 = vertex -> (v2, wt) :: l | _ -> l in List.fold_right node_finder edge_list [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (cur_weight : weight) : ('a list * weight)= match node with | (v2, wt) when v2 == b -> (visited @ [v2], cur_weight + wt) | (v2, wt) when (List.mem v2 visited) -> raise Fail | (v2, wt) -> aux_list (neighbours g v2)( visited @ [v2]) (cur_weight + wt) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (cur_weight : weight) : ('a list * weight) = match nodes with | [] -> raise Fail | (n, wt) :: xs -> try aux_node (n, wt) visited cur_weight with Fail -> aux_list xs visited cur_weight in aux_list (neighbours g a) [a] 0 ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= match node with | (v2, wt) when v2 = b -> (visited @ [v2], sc wt) | (v2, wt) when (List.mem v2 visited) -> fc () | (v2, wt) -> let new_sc = (fun x -> (sc x) + wt) in aux_list (neighbours g v2) (visited @ [v2]) fc new_sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (n, wt) :: xs -> let new_fc = (fun () -> aux_list xs visited fc sc) in aux_node (n, wt) visited new_fc sc in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun n -> n) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths g a b in match all_paths with | [] -> None | _ -> let cur_path = ref (List.nth all_paths 0) in let update_max (path : 'a list * weight) : unit = match path, !cur_path with | (p1, wt1), (p2, wt2) when wt1 >= wt2 -> cur_path := path | _ , _ -> () in let rec do_all f l = match l with | [] -> () | x :: xs -> f x ; do_all f xs in let () = do_all update_max all_paths in Some !cur_path ;; |
let open_account (initial_pass: passwd) : bank_account = let banking = ref initial_pass in let balance = ref 0 in let count = ref 0 in { update_pass = (fun x y -> if (!banking = x) then ( count := 0; banking := y) else ( count := (!count + 1); raise wrong_pass )); retrieve = (fun x y -> if (!count = 5) then raise too_many_failures else if (!banking = x) then if (y < 0) then ( count := 0; raise negative_amount) else if (!balance >= y) then ( count := 0; balance := (!balance - y) ) else ( count := 0; raise not_enough_balance) else if (!count < 5) then (count := !count + 1; raise wrong_pass) else (count := (!count + 1); raise too_many_failures )); show_balance = (fun x -> if (!count = 5) then raise too_many_failures else if (!banking = x) then ( count := 0; !balance) else if (!count < 5) then (count := !count + 1; raise wrong_pass ) else (count := (!count + 1); raise too_many_failures )); deposit = (fun x y -> if (!count = 5) then raise too_many_failures else if (!banking = x) then if (y < 0) then ( count := 0; raise negative_amount) else ( count := 0; balance := (!balance + y)) else if (!count < 5) then ( count := !count + 1; raise wrong_pass) else ( count := (!count + 1); raise too_many_failures )) } ;; let getele1 = fun (x,y) -> x ;; let getele2 = fun (x,y) -> y ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun (x,y,z) acc -> (y,z) :: acc) (List.filter (fun (x,y,z)-> x=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) (count): ('a list * weight) = if ((getele1 node) = b) then ((b :: visited), count + (getele2 node)) else if (List.length (g.edges) = 0 || List.length (neighbours g (getele1 node)) = 0) then raise Fail else aux_list (neighbours g (getele1 node)) ((getele1 node) :: visited) (count + (getele2 node)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (count): ('a list * weight) = match nodes with | [] -> raise Fail | x :: xs -> if (List.mem (getele1 x) (visited)) then aux_list (xs) (visited) (count) else try aux_node (x) (visited) (count) with Fail -> aux_list (xs) (visited) (count) in (List.rev ( getele1 ( aux_node (a,0) ([]) (0) ) ), getele2 (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) (count) fc sc : ('a list * weight)= if ((getele1 node) = b) then sc ((b :: visited), count + (getele2 node)) else if (List.mem (getele1 node) (visited)) then fc () else aux_list (neighbours g (getele1 node)) ((getele1 node) :: visited) (count + (getele2 node)) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (count) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x :: xs -> (aux_node (x) (visited) (count) (fun () -> (aux_list (xs) (visited) (count) (fc) (sc))) (sc)) in (List.rev ( getele1 ( aux_node (a,0) ([]) (0) (fun () -> raise Fail) (fun a -> a) ) ), getele2 (aux_node (a,0) ([]) (0) (fun () -> raise Fail) (fun a -> a)));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match find_all_paths g a b with | [] -> None | _ -> Some (List.fold_right (fun x y -> if (getele2 x) >= (getele2 y) then x else y) (find_all_paths g a b) (([], 0)) );; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let fail_login = ref 0 in let balance = ref 0 in let update_pass request_password new_password = if (request_password = !password) then (fail_login := 0; password := new_password) else (fail_login := !fail_login + 1; raise wrong_pass ) in let retrieve request_password amount = if !fail_login = 5 then raise too_many_failures else if not (request_password = !password) then (fail_login := !fail_login + 1; raise wrong_pass) else if amount < 0 then raise negative_amount else if !balance < amount then raise not_enough_balance else (fail_login := 0; balance := !balance - amount) in let deposit request_password amount = if !fail_login = 5 then raise too_many_failures else if not (request_password = !password) then (fail_login := !fail_login + 1; raise wrong_pass) else if amount < 0 then raise negative_amount else (fail_login := 0; balance := !balance + amount ) in let show_balance request_password = if !fail_login = 5 then raise too_many_failures else if not(request_password = !password) then (fail_login := !fail_login + 1; raise wrong_pass ) else (fail_login := 0; !balance) in { update_pass; retrieve; deposit; show_balance } ;; |
let neighbours (g: 'a graph) (vertex: 'a) = let add_edge (init: ('a * 'b) list) (edge: 'a * 'a * weight) = let ((v1: 'a), (v2: 'a),(w: weight)) = edge in if vertex = v1 then List.append init [(v2,w)] else init in List.fold_left add_edge [] g.edges ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let total_weight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (v1,w) = node in if v1 = b then (total_weight := !total_weight + w; (List.append visited [v1], !total_weight)) else if List.mem v1 visited then raise Fail else try (total_weight := !total_weight + w; aux_list (neighbours g v1) (List.append visited [v1]) ) with Fail -> total_weight := !total_weight - w; raise Fail 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 (v1,w) = node in if v1 = b then sc (List.append visited [v1]) w else if List.mem v1 visited then fc () else let fc2 = fun () -> aux_list (neighbours g v1) (List.append visited [v1]) fc sc in aux_list (neighbours g v1) (List.append visited [v1]) fc2 (fun x r -> sc x (r + w)) 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 y -> (x,y));; |
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 (v1, w1) else Some(v2, w2) in List.fold_left compare None (find_all_paths g a b);; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let count_balance = ref 0 in let passwd_tries = ref 0 in let wrong_passwd = fun input_pass -> (compare (!pass) input_pass) != 0 in { update_pass = (fun old_pass -> fun new_pass -> if wrong_passwd old_pass then let _ = (passwd_tries := (!passwd_tries + 1)) in raise wrong_pass else let _ = (passwd_tries := 0) in pass := new_pass); retrieve = (fun input_pass -> fun retrieve_amount -> if (!passwd_tries >= 5) then raise too_many_failures else if wrong_passwd input_pass then let _ = (passwd_tries := (!passwd_tries + 1)) in raise wrong_pass else let _ = (passwd_tries := 0) in if retrieve_amount > (!count_balance) then raise not_enough_balance else if retrieve_amount < 0 then raise negative_amount else count_balance := (!count_balance - retrieve_amount)); deposit = (fun input_pass -> fun deposit_amount -> if (!passwd_tries >= 5) then raise too_many_failures else if wrong_passwd input_pass then let _ = (passwd_tries := (!passwd_tries + 1)) in raise wrong_pass else let _ = (passwd_tries := 0) in if deposit_amount < 0 then raise negative_amount else count_balance := (!count_balance + deposit_amount)); show_balance = (fun input_pass -> if (!passwd_tries >= 5) then raise too_many_failures else if wrong_passwd input_pass then let _ = (passwd_tries := (!passwd_tries + 1)) in raise wrong_pass else let _ = (passwd_tries := 0) in !count_balance); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = if g.edges = [] then [] else let f1 = fun edge -> let (a,_,_) = edge in (a = vertex) in let new_lst = List.filter f1 g.edges in if new_lst = [] then [] else let f2 = fun edge -> let (_,b,weight) = edge in (b,weight) in List.map f2 new_lst ;; |
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 (city,weight) = node in if city = b then ([b], weight) else if let compare visit = (compare visit city = 0) in (List.exists compare visited) then raise Fail else let (rest_path, rest_weight) = aux_list (neighbours g city) (city::visited) in (city :: rest_path, rest_weight + weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | head_node :: body -> try aux_node head_node visited with Fail -> aux_list body 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 (city,weight) = node in if city = b then sc ([b], weight) else if let compare visit = (compare visit city = 0) in (List.exists compare visited) then fc () else let sc2 (rest_path, rest_weight) = sc (city :: rest_path, weight + rest_weight) in aux_list (neighbours g city) (city :: visited) fc sc2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | head_node :: body -> let fc2 () = aux_list body visited fc sc in aux_node head_node visited fc2 sc in let fc () = raise Fail in let sc x = x in aux_node (a,0) [] fc sc ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max ( lst : ('a list * weight) list) = match lst with | [] -> None | [(path,weight)] -> Some (path,weight) | (path1,weight1) :: (path2,weight2) :: rest_paths -> if weight1 > weight2 then max ( (path1,weight1)::rest_paths ) else if weight1 = weight2 then max ( (path1,weight1)::rest_paths ) else max ( (path2,weight2)::rest_paths ) in 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 counter = ref 0 in { update_pass = (fun cur_pass new_pass -> if (cur_pass = !password) then (counter := 0; password := new_pass) else (counter := !counter + 1; raise wrong_pass)); retrieve = (fun pass amount -> (if (!counter > 4) then (raise too_many_failures) else (if (pass = !password) then (if (amount < 0) then (raise negative_amount) else (if (!balance < amount) then (raise not_enough_balance) else (counter := 0; balance := !balance - amount))) else (counter := !counter + 1; raise wrong_pass)))); deposit = (fun pass amount -> (if (!counter > 4) then (raise too_many_failures) else (if (pass = !password) then (if (amount < 0) then (raise negative_amount) else (counter := 0; balance := !balance + amount)) 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; raise wrong_pass)))); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec removeFirst l1 l2= match l1 with |[] -> l2 |x::xs -> let (n1,n2,w) = x in if (n1 = vertex) then removeFirst xs l2@[(n2,w)] else removeFirst xs l2 in removeFirst g.edges [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let path = ref [] in let totalWeight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if ((fst node) = b) then (((fst node)::(!path)),(snd node) + (!totalWeight)) else if (List.mem (fst node) visited) then (raise Fail) else let (p,w) = aux_list (neighbours g (fst node)) ((fst node)::visited) in ((fst node)::p, ((snd node)+w)) 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 (sc (visited@[(fst node)]) (snd node)) else if (List.mem (fst node) visited) then fc () else let fc2 = fun() -> (aux_list (neighbours g (fst node)) (visited@[(fst node)]) fc sc) in aux_list (neighbours g (fst node)) (visited@[(fst node)]) fc2 (fun n w -> sc n (w+(snd node))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |x::xs -> let fc2 = fun() -> (aux_list xs visited fc sc) in (aux_node x visited fc2 sc) in aux_node (a,0) [] (fun () -> raise Fail) (fun x y -> (x,y));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let compare_path path1 path2 = if ((snd path1) > (snd path2)) then 1 else if ((snd path1) < (snd path2)) then (-1) else 0 in if ((find_all_paths g a b) = []) then None else let l1 = List.sort compare_path (find_all_paths g a b) in let l2 = List.rev l1 in let longest_path = List.hd l2 in Some longest_path;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let incorrect_count = ref 0 in { update_pass = (fun old_pass new_pass -> if old_pass = !pass then (incorrect_count := 0 ; pass := new_pass) else (incr incorrect_count; raise wrong_pass)); deposit = (fun old_pass to_add -> if !incorrect_count > 4 then raise too_many_failures else if old_pass = !pass then (incorrect_count := 0 ; if to_add < 0 then raise negative_amount else balance := !balance + to_add) else (incr incorrect_count; raise wrong_pass)); retrieve = (fun old_pass to_remove -> if !incorrect_count > 4 then raise too_many_failures else if old_pass = !pass then (incorrect_count := 0 ; if to_remove <= !balance then if to_remove < 0 then raise negative_amount else balance := !balance - to_remove else raise not_enough_balance) else (incr incorrect_count; raise wrong_pass)); show_balance = (fun old_pass -> if !incorrect_count > 4 then raise too_many_failures else if old_pass = !pass then (incorrect_count := 0 ; !balance) else (incr incorrect_count; raise wrong_pass)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper l = match l with | [] -> [] | (from, toc, weight)::tl -> if from = vertex then (toc, weight) :: helper tl else helper tl in helper g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec process_node (node: 'a * weight) (visited : 'a list) (acc : int) : ('a list * weight) = if fst(node) = b then ((List.rev (fst(node)::visited)), acc + snd(node)) else if List.exists (fun y -> fst(node) = y) visited then raise Fail else (process_list (neighbours g (fst(node))) (fst(node)::visited) (acc + snd(node))) and process_list (nodes: ('a * weight) list) (visited: 'a list) (acc : int) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs-> try process_node x visited acc with Fail -> process_list xs visited acc in process_node (a, 0) [] 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec process_node (node: 'a * weight) (visited : 'a list) (acc : int) fc sc : ('a list * weight)= if fst(node) = b then ((List.rev (b::visited)), acc + snd(node)) else if List.exists (fun y -> fst(node) = y) visited then fc () else (process_list (neighbours g (fst(node))) (fst(node)::visited) (acc + snd(node)) fc (fun () -> None)) and process_list (nodes: ('a * weight) list) (visited: 'a list) (acc : int) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs-> process_node x visited acc (fun () -> process_list xs visited acc fc (fun () -> None)) (fun () -> None) in process_node (a, 0) [] 0 (fun () -> raise Fail) (fun () -> None);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max list cur_max = match list with | [] -> cur_max | x :: xs -> if(snd(x)>snd(cur_max)) then max xs x else max xs cur_max in if List.length (find_all_paths g a b) = 0 then None else Some (max (List.tl (find_all_paths g a b)) (List.hd (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 error_count = ref 0 in { update_pass = (function oldpasswd -> function newpasswd -> if oldpasswd = !password then (error_count := 0; password := newpasswd) else (error_count := !error_count + 1; raise wrong_pass)); retrieve = (function passwd -> function amount -> if !error_count < 5 then if passwd <> !password then (error_count := !error_count + 1; raise wrong_pass) else if amount < 0 then raise negative_amount else if !balance < amount then raise not_enough_balance else (balance := !balance - amount; error_count := 0) else raise too_many_failures); deposit = (function passwd -> function amount -> if !error_count < 5 then if passwd <> !password then (error_count := !error_count + 1; raise wrong_pass) else if amount < 0 then raise negative_amount else (balance := !balance + amount; error_count := 0) else raise too_many_failures); show_balance = (function passwd -> if !error_count < 5 then if passwd <> !password then (error_count := !error_count + 1; raise wrong_pass) else (error_count := 0; !balance) else raise too_many_failures) } ;; let g = {nodes = ["p1"; "p2"; "p3"]; edges = [("p1", "p2", 2); ("p2", "p3", 4); ("p1", "p3", 3)]};; let rec get_neighbours edges v acc = match edges with | [] -> acc | x :: xs -> (match x with | (x0, x1, x2) -> if x0 = v then get_neighbours xs v ((x1, x2) :: acc) else get_neighbours xs v acc | _ -> get_neighbours xs v acc) | _ -> [] ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = get_neighbours g.edges vertex [] ;; neighbours g "p1";; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) rl rw : ('a list * weight) = match (List.mem (fst(node)) visited) with | true -> raise Fail | false -> if fst(node) = b then (rl @ [(fst(node))], rw+(snd(node))) else aux_list (neighbours g (fst(node))) (fst(node) :: visited) (rl @ [(fst(node))]) (rw+(snd(node))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) rl rw : ('a list * weight) = match nodes with | [] -> raise Fail | x :: xs -> try aux_node x visited rl rw with Fail -> aux_list xs visited rl rw 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) fc sc rl rw : ('a list * weight)= match (List.mem (fst(node)) visited) with | true -> fc () | false -> if fst(node) = b then sc (rl @ [(fst(node))], rw+(snd(node))) else aux_list (neighbours g (fst(node))) (fst(node) :: visited) fc sc (rl @ [(fst(node))]) (rw+(snd(node))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc rl rw : ('a list * weight) = match nodes with | [] -> fc () | x :: xs -> let sc2 = sc in let fc2 = fun () -> aux_node x visited fc sc rl rw in aux_list xs visited fc2 sc2 rl rw in aux_node (a, 0) [] (fun () -> raise Fail) (fun x -> x) [] 0;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let l = find_all_paths g a b in let max = ref ([],0) in let f c = if (snd(c)) > (snd(!max)) then max := c else max := !max in match l with | [] -> None | _ -> let _ = (List.iter f l) in Some !max ;; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let passw = ref initial_pass in let attempts = ref 0 in {update_pass = (fun input_pass new_pass -> if input_pass = !passw then (attempts := 0 ;passw := new_pass) else (attempts := !attempts + 1; raise wrong_pass)); retrieve = (fun pass num -> if !attempts < 5 then (if pass = !passw then (attempts := 0; if num > 0 then ( let b = !balance in let z = b - num in if z > 0 then balance := z else raise not_enough_balance ) else raise negative_amount ) else (attempts := !attempts + 1; raise wrong_pass) ) else raise too_many_failures) ; deposit = (fun pass num -> if !attempts < 5 then (if pass = !passw then (attempts := 0; if num > 0 then balance := !balance + num else raise negative_amount ) else (attempts := !attempts + 1; raise wrong_pass) ) else raise too_many_failures) ; show_balance = (fun pass -> if !attempts < 5 then (if pass = !passw then (attempts := 0; !balance) else (attempts := !attempts + 1; raise wrong_pass)) else raise too_many_failures)} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper e v f= match e with | [] -> f [] | (v1,v2,w)::xs -> if v1 = v then helper xs v (fun u -> f ((v2,w)::u)) else helper xs v f in let lst = g.edges in helper lst vertex (fun u -> u);; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a) (visited : 'a list) (num : weight) : ('a list * weight) = if node = b then (List.rev visited,num) else aux_list (neighbours g node) visited num and aux_list (nodes: ('a * weight) list) (visited: 'a list) (num : weight) : ('a list * weight) = match nodes with | [] -> raise Fail | (node,w)::xs -> if (List.mem node visited) then aux_list xs visited num else try aux_node node (node::visited) (num + w) with Fail -> aux_list xs visited num in if a = b then ([b],0) else 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) (visited : 'a list) fc sc : ('a list * weight)= if node = b then (fc [], sc 0) else aux_list (neighbours g node) visited fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> raise Fail | (node,w)::xs -> if (List.mem node visited) then aux_list xs visited fc sc else try aux_node node (node::visited) (fun x -> fc (node::x)) (fun y -> sc (y + w)) with Fail -> aux_list xs visited fc sc in if a = b then ([b],0) else aux_list (neighbours g a) [a] (fun u -> (a::u)) (fun z -> z);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max (paths: ('a list * weight) list) (max_path: ('a list * weight)) (max_cost: int) : ('a list * weight) option = match paths with |[] -> Some max_path |(node,w)::xs -> if w > max_cost then max xs (node,w) w else max xs max_path max_cost in let path_list = find_all_paths g a b in match path_list with |[] -> None |_ -> max path_list (List.hd path_list) 0;; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let counter = ref 0 in let current_pass = ref initial_pass in { update_pass = (fun a b -> if (a = !current_pass) then (current_pass := b; counter := 0) else (counter := !counter + 1; raise wrong_pass)); retrieve = (fun a b -> if (!counter >= 5) then raise too_many_failures else if (a = !current_pass) then (if (b < 0) then (counter := 0; raise negative_amount) else if (b <= !balance) then (counter := 0; balance := !balance - b) else (counter := 0; raise not_enough_balance)) else (counter := !counter + 1; raise wrong_pass)); deposit = (fun a b -> if (!counter >= 5) then raise too_many_failures else if not(a = !current_pass) then (counter := !counter + 1; raise wrong_pass) else if (b < 0) then (counter := 0; raise negative_amount) else (counter := 0; balance := !balance + b)); show_balance = (fun a -> if (!counter >= 5) then raise too_many_failures else if (a = !current_pass) then (counter := 0; !balance) else (counter := !counter + 1; raise wrong_pass)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (fun (v, w, n) -> (w, n)) (List.filter (fun (v, w, n) -> v = 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) = match node with | (v, w) when v = b -> ([b], w) | (v, _) when List.mem v visited -> raise Fail | (v, w) -> let c = aux_list (neighbours g v) (v::visited) in (v::(fst c), w + (snd c)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | hd::tl -> (try aux_node hd 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)= match node with | (v, w) when v = b -> sc([b], w) | (v, _) when List.mem v visited -> fc () | (v, w) -> aux_list (neighbours g v) (v::visited) fc (fun (s, t) -> sc(v::s, w + t)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd::tl -> aux_node hd visited (fun () -> aux_list tl visited fc sc) sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun r -> r);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let l = find_all_paths g a b in match List.length l with | 0 -> None | 1 -> Some (List.hd l) | _ -> Some (List.nth (List.sort (fun c d -> compare (snd c) (snd d)) l) (List.length l - 1));; |
let open_account (initial_pass: passwd) : bank_account = let myPass = ref initial_pass in let myMoney = ref 0 in let failuresCounter = ref 0 in { update_pass = (fun givenPass newPass -> if givenPass = !myPass then (failuresCounter := 0; myPass := newPass) else (failuresCounter := !failuresCounter +1; raise wrong_pass) ); retrieve = (fun givenPass toGet-> if !failuresCounter >= 5 then (failuresCounter := !failuresCounter +1; raise too_many_failures); if givenPass = !myPass then if toGet < 0 then raise negative_amount else if toGet > !myMoney then raise not_enough_balance else (failuresCounter := 0; myMoney := !myMoney - toGet) else (failuresCounter := !failuresCounter +1; raise wrong_pass;) ); deposit = (fun givenPass toDep -> if !failuresCounter >= 5 then (failuresCounter := !failuresCounter +1; raise too_many_failures); if givenPass = !myPass then if toDep < 0 then raise negative_amount else (failuresCounter := 0; myMoney := !myMoney + toDep) else (failuresCounter := !failuresCounter +1; raise wrong_pass) ); show_balance = (fun givenPass -> if !failuresCounter >= 5 then (failuresCounter := !failuresCounter +1; raise too_many_failures); if givenPass = !myPass then (failuresCounter := 0; !myMoney) else (failuresCounter := !failuresCounter +1; raise wrong_pass) ); } ;; let fy (lst: ('a*weight) list) (tup: 'a*'a*weight) = match tup with (me,neb,wei) -> if me = vert then List.append lst [(neb,wei)] ;; let (me,neb,wei)=tup in if me = vert then List.append [(neb,wei)] lst *);; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let fy (lst:('a*weight) list) (tup:'a*'a*weight) : ('a*weight) list= let (me,neb,wei)= tup in if me = vertex then (neb,wei) :: lst else lst in List.fold_left fy [] 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 ( [(fst node)], (snd node)+0) else if (List.mem (fst node) visited) then raise Fail else let (myPath,myWei) = aux_list (neighbours g (fst node) ) ( (fst node) :: visited) in ( (fst node) :: myPath, myWei+(snd node) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | hd :: tl-> try aux_node hd 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 ( [(fst node)], (snd node)) else if (List.mem (fst node) visited) then fc() else let sc3 y= sc ((fst node)::(fst y), (snd node)+(snd y)) in aux_list (neighbours g (fst node) ) ( (fst node) :: visited) fc sc3 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd :: tl-> let suceed2 = sc in let fail2 = fun () -> aux_list tl visited fc sc in aux_node hd visited fail2 suceed2 in aux_node (a,0) [] (fun () -> raise Fail) (fun x -> (fst x),(snd x)+0);; |
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 compWeights p1 p2 = if (snd p1) = (snd p2) then 0 else if (snd p1) > (snd p2) then 1 else (-1) in let sortedPaths = List.sort compWeights allPaths in if List.length sortedPaths = 0 then None else let revList = List.rev sortedPaths in Some (List.hd revList);; |
let open_account (initial_pass: passwd) : bank_account = let passref = ref(initial_pass) in let balance = ref(0) in let failures = ref(0) in let retrieve = (fun pass withdrawl -> if(!failures>=5) then raise (too_many_failures) else( if (pass = !passref) then (failures:=0; if(withdrawl<0) then raise negative_amount else ( if ((!balance - withdrawl)<0) then raise (not_enough_balance) else balance :=!balance-withdrawl)) else (failures:=(!failures+1); raise (wrong_pass)) )) in let update_pass = (fun pass newpass -> if (pass = !passref) then (passref:=newpass; failures:=0) else (failures:=(!failures+1); raise wrong_pass)) in let show_balance = (fun pass -> if(!failures>=5) then raise (too_many_failures) else( if (pass = !passref) then (failures:=0; !balance) else (failures:=(!failures+1); raise (wrong_pass)) )) in let deposit = (fun pass input -> if(!failures>=5) then raise (too_many_failures) else( if (pass = !passref) then (failures:=0; if( input<0) then raise negative_amount else balance :=!balance+input) else (failures:=(!failures+1); raise (wrong_pass)) )) in {update_pass;retrieve;deposit;show_balance};; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec neighbourshelper edges vertex acc= match edges with |[]->acc [] |(fromnode,tonode,weight)::xs ->if(fromnode=vertex) then neighbourshelper xs vertex (fun list->acc((tonode,weight)::list)) else neighbourshelper xs vertex acc in let {nodes;edges}=g in neighbourshelper edges vertex (fun list->list);; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight)= let (place,distance) = node in if(place = b) then ([b],distance) else (let y = (aux_list (neighbours g place) (visited)) in (place::fst y, (distance+snd y))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |x::xs -> let (place,weight) = x in if(List.mem place visited) then aux_list xs visited else(try aux_node x (place::visited) with Fail -> aux_list xs (place::visited)) in let tuple = aux_list (neighbours g a) [a] in (a::fst tuple, snd tuple);; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node node visited fc sc = let (place,distance) = node in if(place = b) then sc ([b],distance) else (aux_list (neighbours g place) (visited) fc (fun tuple -> sc(place::fst tuple, distance+snd tuple))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc: ('a list * weight) = match nodes with |[] -> fc () |x::xs -> let (place,weight) = x in if(List.mem place visited) then aux_list xs visited fc sc else(let newfail= fun () -> aux_list xs (place::visited) fc sc in aux_node x (place::visited) newfail sc) in let tuple = aux_list (neighbours g a) [a] (fun ()->raise Fail) (fun list -> list) in (a::fst tuple, snd tuple);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let comparepaths = fun tuple1 tuple2 ->((snd tuple2)-(snd tuple1)) in match (List.sort comparepaths (find_all_paths g a b)) with |[] ->None |x::xs -> Some(x);; |
let open_account (initial_pass: passwd) : bank_account = let curPasswd= ref initial_pass in let curBalance= ref 0 in let numofWrongPasswd= ref 0 in { update_pass= (fun oldPasswd -> fun newPasswd -> if oldPasswd= !curPasswd then (curPasswd:= newPasswd; numofWrongPasswd:= 0) else (numofWrongPasswd:= !numofWrongPasswd+ 1; raise wrong_pass)); retrieve= (fun passwd1-> fun amount-> if !numofWrongPasswd>= 5 then raise too_many_failures else ( if passwd1 <> !curPasswd then (numofWrongPasswd:= !numofWrongPasswd+ 1; raise wrong_pass) else ( numofWrongPasswd:= 0; if amount< 0 then raise negative_amount else ( if amount> !curBalance then raise not_enough_balance else( curBalance:= !curBalance- amount ) ) ) ) ); deposit= (fun passwd1-> fun amount-> if !numofWrongPasswd>= 5 then raise too_many_failures else ( if passwd1 <> !curPasswd then (numofWrongPasswd:= !numofWrongPasswd+ 1; raise wrong_pass) else ( numofWrongPasswd:= 0; if amount< 0 then raise negative_amount else ( curBalance:= !curBalance+ amount ) ) ) ); show_balance= (fun passwd1-> if !numofWrongPasswd>= 5 then raise too_many_failures else ( if passwd1 <> !curPasswd then (numofWrongPasswd:= !numofWrongPasswd+ 1; raise wrong_pass) else ( numofWrongPasswd:= 0; !curBalance) ) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let result= (List.exists (fun x-> x= vertex) g.nodes) in if (not(result)) then (raise wrong_pass) else ( List.fold_left (fun a-> fun (v1, v2, w)-> if (v1= vertex) then ((v2, w):: a) else a) [] g.edges );; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_list (nodes: ('a * weight) list) ((visitedNodes, weights): ('a list * weight)) : ('a list * weight) = match nodes with |[]-> raise Fail |(v, w) :: xs -> if (not(List.exists (fun x-> x= v) visitedNodes)) then( if v= b then (visitedNodes @ [v], weights+ w) else( try (aux_list (neighbours g v) (visitedNodes @ [v], weights+ w)) with Fail-> try (aux_list xs (visitedNodes, weights)) with Fail-> raise Fail ) ) else( aux_list xs (visitedNodes, weights) ) in aux_list (neighbours g a ) (a::[], 0) ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_list (nodes: ('a * weight) list) ((visitedNodes, weights): ('a list * weight)) fc sc : ('a list * weight) = let check (y, _)= not(List.exists (fun x-> x= y) visitedNodes) in match List.filter check nodes with |[]-> fc () |(v, w) :: xs -> if v= b then sc (visitedNodes @ [v], weights+ w) else( let fc2= fun()-> (aux_list xs (visitedNodes, weights) fc sc) in (aux_list (neighbours g v) (visitedNodes @ [v], weights+ w) fc2 sc) ) in aux_list (neighbours g a ) (a::[], 0) (fun ()-> raise Fail) (fun x-> x);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper paths (visitedNodes, weights)= match paths with |[] -> Some (visitedNodes, weights) |(v, w):: xs-> if (w> weights) then helper xs (v, w) else helper xs (visitedNodes, weights) in match (find_all_paths g a b) with |[]-> None |p -> helper p ([], 0);; |
let open_account (initial_pass: passwd) : bank_account = let curPasswd= ref initial_pass in let curBalance= ref 0 in let numofWrongPasswd= ref 0 in { update_pass= (fun oldPasswd -> fun newPasswd -> if oldPasswd= !curPasswd then (curPasswd:= newPasswd; numofWrongPasswd:= 0) else (numofWrongPasswd:= !numofWrongPasswd+ 1; raise wrong_pass)); retrieve= (fun passwd1-> fun amount-> if !numofWrongPasswd>= 5 then raise too_many_failures else ( if passwd1 <> !curPasswd then (numofWrongPasswd:= !numofWrongPasswd+ 1; raise wrong_pass) else ( numofWrongPasswd:= 0; if amount< 0 then raise negative_amount else ( if amount> !curBalance then raise not_enough_balance else( curBalance:= !curBalance- amount ) ) ) ) ); deposit= (fun passwd1-> fun amount-> if !numofWrongPasswd>= 5 then raise too_many_failures else ( if passwd1 <> !curPasswd then (numofWrongPasswd:= !numofWrongPasswd+ 1; raise wrong_pass) else ( numofWrongPasswd:= 0; if amount< 0 then raise negative_amount else ( curBalance:= !curBalance+ amount ) ) ) ); show_balance= (fun passwd1-> if !numofWrongPasswd>= 5 then raise too_many_failures else ( if passwd1 <> !curPasswd then (numofWrongPasswd:= !numofWrongPasswd+ 1; raise wrong_pass) else ( numofWrongPasswd:= 0; !curBalance) ) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let result= (List.exists (fun x-> x= vertex) g.nodes) in if (not(result)) then (raise Fail) else ( List.fold_left (fun a-> fun (v1, v2, w)-> if (v1= vertex) then ((v2, w):: a) else a) [] g.edges );; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_list (nodes: ('a * weight) list) ((visitedNodes, weights): ('a list * weight)) : ('a list * weight) = match nodes with |[]-> raise Fail |(v, w) :: xs -> if (not(List.exists (fun x-> x= v) visitedNodes)) then( if v= b then (visitedNodes @ [v], weights+ w) else( try (aux_list (neighbours g v) (visitedNodes @ [v], weights+ w)) with Fail-> try (aux_list xs (visitedNodes, weights)) with Fail-> raise Fail ) ) else( aux_list xs (visitedNodes, weights) ) in aux_list (neighbours g a ) (a::[], 0) ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_list (nodes: ('a * weight) list) ((visitedNodes, weights): ('a list * weight)) fc sc : ('a list * weight) = let check (y, _)= not(List.exists (fun x-> x= y) visitedNodes) in match List.filter check nodes with |[]-> fc () |(v, w) :: xs -> if v= b then sc (visitedNodes @ [v], weights+ w) else( let fc2= fun()-> (aux_list xs (visitedNodes, weights) fc sc) in (aux_list (neighbours g v) (visitedNodes @ [v], weights+ w) fc2 sc) ) in aux_list (neighbours g a ) (a::[], 0) (fun ()-> raise Fail) (fun x-> x);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper paths (visitedNodes, weights)= match paths with |[] -> Some (visitedNodes, weights) |(v, w):: xs-> if (w> weights) then helper xs (v, w) else helper xs (visitedNodes, weights) in match (find_all_paths g a b) with |[]-> None |p -> helper p ([], 0);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let incorrectTries = ref 0 in let retrieve inPass money = if !incorrectTries >= 5 then raise too_many_failures else if money < 0 then raise negative_amount else if !password = inPass then if !balance > money then balance := !balance - money else (raise not_enough_balance; incorrectTries := 0) else (incorrectTries := !incorrectTries + 1; raise wrong_pass) in let deposit inPass amount = if !incorrectTries >= 5 then raise too_many_failures else if amount < 0 then raise negative_amount else if !password = inPass then (balance := !balance + amount; incorrectTries := 0) else (incorrectTries := !incorrectTries + 1;raise wrong_pass ) in let show_balance inPass = if !incorrectTries >= 5 then raise too_many_failures else if !password = inPass then (incorrectTries := 0; !balance) else (incorrectTries := !incorrectTries + 1; raise wrong_pass) in let update_pass oldPass newPass = if oldPass = !password then (password := newPass; incorrectTries := 0) else (incorrectTries := !incorrectTries + 1; raise wrong_pass) in { update_pass; show_balance; deposit; retrieve;};; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec neigh_rec edgest vert = match edgest with | [] -> [] | (v,w,weight)::xs -> if v = vert then (w,weight)::(neigh_rec xs vert) else (neigh_rec xs vert) in neigh_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) : ('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 num_fails = ref 0 in { update_pass = (fun old_pass new_pass -> if old_pass = !password then (num_fails := 0; password := new_pass) else (num_fails := !num_fails+1; raise wrong_pass)); retrieve = (fun pwd amt -> if !num_fails < 5 then if pwd = !password then (num_fails := 0; if !balance >= amt then if amt >= 0 then balance := !balance - amt else raise negative_amount else raise not_enough_balance) else (num_fails := !num_fails+1; raise wrong_pass) else raise too_many_failures); deposit = (fun pwd amt -> if !num_fails<5 then if pwd = !password then (num_fails := 0; if amt >= 0 then balance := !balance+amt else raise negative_amount) else (num_fails := !num_fails+1; raise wrong_pass) else raise too_many_failures); show_balance = (fun pwd -> if !num_fails<5 then if pwd = !password then (num_fails := 0; !balance) else (num_fails := !num_fails+1; raise wrong_pass) else raise too_many_failures) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f = fun acclist edge -> let (v1, v2, w) = edge in if v1 = vertex then ((v2, w) :: acclist) else acclist in List.fold_left f [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (accweight: weight) : ('a list * weight) = let (value, weight) = node in let neighbourlist = neighbours g value in if (value = b) then (visited @ [b], (accweight + weight)) else aux_list neighbourlist (visited @ [value]) (accweight + weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (accweight: weight) : ('a list * weight) = match nodes with | [] -> raise Fail | h :: t -> let (node, weight) = h in if (List.mem node visited) then aux_list t visited accweight else try (aux_node h (visited) accweight) with Fail -> aux_list t (visited) accweight 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) (accweight: weight) fc sc : ('a list * weight) = let (value, weight) = node in let neighbourlist = neighbours g value in if (value = b) then sc (visited @ [b], (accweight + weight)) else aux_list neighbourlist (visited @ [value]) (accweight + weight) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (accweight: weight) fc sc : ('a list * weight) = match nodes with | [] -> fc () | h :: t -> let (node, weight) = h in if (List.mem node visited) then aux_list t visited accweight fc sc else let fc2 = fun () -> sc (aux_list t (visited) accweight fc sc) in aux_node h (visited) accweight fc2 sc in aux_list (neighbours g a) [a] 0 (fun () -> raise Fail) (fun x -> x);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths_list = find_all_paths g a b in if (paths_list = []) then None else let rec find_max_path_weight l = match l with | [] -> -1 | (path, path_weight) :: [] -> path_weight | (path, path_weight) :: ls -> max path_weight (find_max_path_weight ls) in let max_weight = find_max_path_weight paths_list in let f path: bool = let (list, weight) = path in weight = max_weight in Some (List.find f paths_list) ;; |
let open_account (initial_pass: passwd) : bank_account = let passw = ref initial_pass in let a : int ref = ref 0 in let b : int ref = ref 0 in { update_pass = (fun oldp newp -> if oldp = !passw then b:= (!b - !b) else b:= (!b + 1) ; if !b = 0 then passw := newp else raise wrong_pass ); deposit = (fun passtest depos -> if !b <= 4 then if passtest = !passw then b := (!b - !b) else b:= (!b + 1) else raise too_many_failures; if !b != 0 then raise wrong_pass else if (depos < 0) then raise negative_amount else a := (!a + depos) ); retrieve = (fun passtest retri -> if !b <= 4 then if (passtest = !passw) then b := (!b - !b) else b := (!b + 1) else raise too_many_failures; if !b != 0 then raise wrong_pass else if (retri < 0) then raise negative_amount else if (retri > !a) then raise not_enough_balance else a := (!a - retri) ); show_balance = (fun passtest -> if !b <= 4 then if passtest = !passw then b := (!b - !b) else b := (!b + 1) else raise too_many_failures; if !b = 0 then !a else raise wrong_pass ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let fun1 l edge = let (node1,node2,weight) = edge in if node1 = vertex then (List.append l [(node2,weight)]) else l in let {nodes = nodelist; edges = edgelist } = g in List.fold_left fun1 [] edgelist;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let neighbours2 (g: 'a graph) (vertex: 'a) = let hmmm w edge = let (node1,node2,weight) = edge in if node1 = vertex then (List.append w [node2]) else w in let {nodes = nodelist; edges = edgelist } = g in List.fold_left hmmm [] edgelist in let rec aux visited n = match n with | [] -> raise Fail | x::t -> if List.mem x visited then aux visited t else if b=x then [x] else try aux (x::visited) t with Fail -> x:: aux (x::visited) (neighbours2 g x) in let rec fake l w = match l with |[] -> w |x::t::[] -> begin let w = w@[(x,t,0)] in w end |x::r::t -> begin let w = w@[(x,r,0)] in fake(r::t) w end in let rec addw edgelist fi = match edgelist with |[] -> fi |(x,y,w)::t -> begin let fin = fi + w in addw t fin end in let rec actu (g: 'a graph) liste w = match liste with |[] -> w |(x,y,z) as e::t -> begin let w = w@[(List.find (fun (a,b,c) -> (a = x && b = y)) g.edges)] in actu g t w end in (aux [] [a] , addw (actu g (fake (aux [] [a]) []) []) 0);; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let allpath = find_all_paths g a b in let rec allweights l = match l with |[] -> [] |(a,b)::[] -> [b] |(a,b)::f -> b :: (allweights f) in let rec most lista = match lista with |[] -> 0 |x::[] -> x |x::t -> let y = most t in if x > y then x else y in let rec findlongpath lista num = match lista with |[] -> ([],0) |(a,b)::t -> if b = num then (a,b) else findlongpath t num in if findlongpath allpath (most (allweights allpath)) = ([],0) then None else Some (findlongpath allpath (most (allweights allpath)));; |
let open_account (initial_pass: passwd) : bank_account = let password_tries = ref 0 and actual_password = ref initial_pass and balance = ref 0 in {update_pass = (fun old_pass new_pass -> if (compare !actual_password old_pass = 0) then (actual_password := new_pass; password_tries := 0) else if (compare !actual_password old_pass != 0) then (password_tries := !password_tries + 1; raise (wrong_pass)) ) ; retrieve = (fun passw_try amount -> if !password_tries >= 5 then raise (too_many_failures) else if (compare !actual_password passw_try != 0) then (password_tries := !password_tries + 1; raise (wrong_pass)) else if amount < 0 then raise (negative_amount) else if (compare !actual_password passw_try = 0) then if !balance < amount then (password_tries := 0; raise (not_enough_balance)) else (password_tries := 0; balance := !balance - amount) ) ; deposit = (fun passw_try amount -> if !password_tries >= 5 then raise (too_many_failures) else if (compare !actual_password passw_try != 0) then (password_tries := !password_tries + 1; raise (wrong_pass)) else if amount < 0 then raise (negative_amount) else if (compare !actual_password passw_try = 0) then (password_tries := 0; balance := !balance + amount) ) ; show_balance = (fun passw_try -> if !password_tries >= 5 then raise (too_many_failures) else if (compare !actual_password passw_try != 0) then (password_tries := !password_tries + 1; raise (wrong_pass)) else (password_tries := 0; !balance) ) } ;; let g1 = { nodes = ["one"; "two"; "three"; "four"] ; edges = [("one", "two", 1); ("two", "three", 1); ("three", "one", 1); ("one", "four", 1)] } in (g1, "one") , [("two", 1); ("four", 1)] ) ; (let g1 = { nodes = ["one"; "two"; "three"; "four"] ; edges = [("one", "two", 1); ("two", "three", 1); ("three", "one", 1); ("one", "four", 1)] } in (g1, "three") , [("one", 1)]) ; (let g1 = { nodes = ["one"] ; edges = [] } in (g1, "one") , []) ; (let g1 = { nodes = [] ; edges = [] } in (g1, "a") , []) ];; let is_neighbor my_node tup = let (n,_,_) = tup in n = my_node ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let outgoing_edges = List.filter (fun tup -> is_neighbor vertex tup) g.edges in List.map (fun tup -> let (_,o,w) = tup in (o, w)) outgoing_edges ;; let neighbor_in_visited (neighbor: 'a) (visited: 'a list) = (List.mem neighbor visited) let get_node a = let (n,_) = a in n ;; let get_weight a = let (_,n) = a in n;; |
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 (compare (get_node node) b = 0) then ([b], ((get_weight node))) else if (neighbor_in_visited (get_node node) visited) then raise Fail else (let (path , cost_so_far) = aux_list (neighbours g (get_node node)) ((get_node node) :: visited) in (((get_node node) :: path), cost_so_far + (get_weight node))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | x :: xs -> (try aux_node x visited with Fail -> aux_list xs visited) | [] -> raise Fail 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 (compare (get_node node) b = 0) then sc ([b], ((get_weight node))) else if (neighbor_in_visited (get_node node) visited) then fc () else aux_list (neighbours g (get_node node)) ((get_node node) :: visited) fc (fun (path, cost_so_far) -> sc ( (get_node node) :: path , cost_so_far + (get_weight node) ) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | x :: xs -> aux_node x visited (fun () -> aux_list xs visited fc sc) sc | [] -> fc () in let identity_function = (fun l -> l) in aux_node (a, 0) [] (fun () -> raise Fail) identity_function;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths_list = find_all_paths g a b in if (compare (List.length all_paths_list) 0 = 0) then None else max all_paths_list (List.hd all_paths_list);; |
let fail_count () : count_failures = let failures = ref 0 in {increase = (fun () -> failures := !failures + 1; !failures); reset = (fun () -> failures := 0); number = (fun () -> !failures) } ;; type account_info = { password : string ref; balance : int ref; failures : count_failures};; |
let open_account (initial_pass: passwd) : bank_account = let account = {password = ref initial_pass; balance = ref 0; failures = fail_count ()} in {update_pass = (fun old_pass new_pass -> if old_pass = !(account.password) then let fails = (account.failures).reset () in account.password := new_pass else let fails = (account.failures).increase () in raise wrong_pass); retrieve = (fun password amount -> if (account.failures).number () < 5 then if password = !(account.password) then let fails = (account.failures).reset () in if amount >= 0 then if amount <= !(account.balance) then account.balance := !(account.balance) - amount else raise not_enough_balance else raise negative_amount else let fails = (account.failures).increase () in raise wrong_pass else raise too_many_failures ); deposit = (fun password amount -> if (account.failures).number () < 5 then if password = !(account.password) then let fails = (account.failures).reset () in if amount >= 0 then account.balance := !(account.balance) + amount else raise negative_amount else let fails = (account.failures).increase () in raise wrong_pass else raise too_many_failures); show_balance = (fun password -> if (account.failures).number () < 5 then if password = !(account.password) then let fails = (account.failures).reset () in !(account.balance) else let fails = (account.failures).increase () in raise wrong_pass else raise too_many_failures) ; };; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun l e -> let (v1,v2,w) = e in if v1 = vertex then (v2,w)::l else l) [] g.edges;; let rec already_visited node visited = match visited with |[] -> false |h::t -> (h = node) || (already_visited node t) ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (total: weight): ('a list * weight) = let (city,w) = node in let total = total + w in if city = b then (visited, total) else aux_list (neighbours g city) visited total and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total: weight) : ('a list * weight) = match nodes with |[] -> raise Fail |h::t -> try let (city,w) = h in if (already_visited city visited) then raise Fail else aux_node h (visited @ [city]) total with Fail -> aux_list t visited total 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 total: ('a list * weight)= let (city,w) = node in let total = total + w in if city = b then sc (visited, total) else aux_list (neighbours g city) visited fc sc total and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc total: ('a list * weight) = match nodes with |[] -> fc () |h::t -> let (city,w) = h in if not (already_visited city visited) then let sc2 = sc in let fc2 = fun () -> aux_node h (visited @ [city]) fc sc total in aux_list t visited fc2 sc2 total else aux_list t visited fc sc total in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun r -> r) 0;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper p longest = match p with |[] -> Some longest |h::t -> let (cities,w) = h in let (best,price) = longest in if w > price then helper t h else helper t longest in let paths = find_all_paths g a b in if paths = [] then None else helper paths (List.nth paths 0) ;; |
let open_account (initial_pass: passwd) : bank_account = let pwd = ref initial_pass in let money = ref 0 in let err_times = ref 0 in let chk_pwd p = if !err_times >= 5 then raise too_many_failures else if !pwd = p then err_times := 0 else let () = err_times := !err_times + 1 in raise wrong_pass in let f1 pwd1 npwd = if pwd1 = !pwd then pwd := npwd else let () = err_times := !err_times + 1 in raise wrong_pass in let f2 pwd1 v = let () = chk_pwd pwd1 in if v < 0 then raise negative_amount else if !money < v then raise not_enough_balance else money := !money - v in let f3 pwd1 v = let () = chk_pwd pwd1 in if v < 0 then raise negative_amount else money := !money + v in let f4 pwd1 = let () = chk_pwd pwd1 in !money in { update_pass = f1; retrieve = f2; deposit = f3; show_balance = f4 } ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.