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) = 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 att = ref 0 in { update_pass = (fun oldp newp -> if oldp <> !password then (att := !att+1; raise wrong_pass) else att :=0; password := newp); retrieve = (fun pass amount -> if pass <> !password then (att := !att+1; if !att > 5 then raise too_many_failures else raise wrong_pass) else if !att < 5 then (if amount < 0 then raise negative_amount else if amount > !balance then raise not_enough_balance else att :=0; balance := !balance - amount) else raise too_many_failures); deposit = (fun pass amount-> if pass <> !password then (att := !att+1; if !att > 5 then raise too_many_failures else raise wrong_pass) else if !att < 5 then (if amount < 0 then raise negative_amount else att :=0; balance := !balance + amount) else raise too_many_failures); show_balance = (fun pass-> if pass <> !password then (att := !att+1; if !att > 5 then raise too_many_failures else raise wrong_pass) else if !att < 5 then (att :=0; !balance) else raise too_many_failures); };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun (v1,v2,w) acc -> if v1 = vertex then (v2,w)::acc else acc) g.edges [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (n,w) = node in if n = b then ([n],w) else if List.mem n visited then raise Fail else let (sub_list, sub_w) = aux_list (neighbours g n) (n::visited) in (n::sub_list, w+sub_w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | node::rest -> try aux_node node visited with Fail -> aux_list rest visited in aux_node (a, 0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (n, w) = node in let success1 = sc in if n = b then success1 ([n],w) else if List.mem n visited then fc () else let success2 = fun (sub_list, sub_w) -> success1 (n::sub_list, w+sub_w) in aux_list (neighbours g n) (n::visited) fc success2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | node::rest -> let fail2 = fun() -> aux_list rest visited fc sc in aux_node node visited fail2 sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun x -> x) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all = find_all_paths g a b in match all with | [] -> None | [p] -> Some (p) | first::rest -> Some (let f = fun (path,w) (first_path, max) -> if w > max then (path, w) else (first_path, max) in List.fold_right f all first) ;;
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let passwd = ref initial_pass in let wrong_times = ref 0 in let update_pass old_passwd new_passwd = if old_passwd = !passwd then (wrong_times := 0; passwd := new_passwd) else (incr wrong_times; raise wrong_pass) in let deposit input_passwd num = if !wrong_times >= 5 then raise too_many_failures else if input_passwd <> !passwd then (incr wrong_times; raise wrong_pass) else if num < 0 then raise negative_amount else (wrong_times := 0; balance := !balance + num) in let retrieve input_passwd num = if !wrong_times >= 5 then raise too_many_failures ; if input_passwd <> !passwd then (incr wrong_times; raise wrong_pass) ; if num < 0 then raise negative_amount ; if !balance - num < 0 then raise not_enough_balance ; (wrong_times := 0; balance := !balance - num) in let show_balance input_passwd = if !wrong_times >= 5 then raise too_many_failures else if input_passwd <> !passwd then (incr wrong_times; raise wrong_pass) else (wrong_times := 0; !balance) in {update_pass; deposit; retrieve; show_balance} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc (in', out', weight) -> if in' = vertex then (out', weight)::acc else acc) [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let node, weigth = node in if List.mem node visited then [], 0 else if node = b then [node], weigth else;;
let neighbours = neighbours g node |> List.filter (fun (x, _) -> not @@ List.mem x visited) in let l, weight' = aux_list neighbours (node :: visited) in if List.length l = 0 then [], 0 else node :: l, weight' + weigth and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = let k = List.fold_left (fun acc h -> match acc with | Some v -> Some v | None -> let l, weight' = aux_node h visited in if List.length l = 0 then None else Some (l, weight')) (None) nodes in match k with | Some v -> v | None -> [], 0 in match aux_list (neighbours g a) [a] with | [], _ -> raise Fail | path, weight -> a :: path, weight;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) cont = let node, weigth = node in if List.mem node visited then cont ([], 0) else if node = b then cont ([node], weigth) else;;
let neighbours = neighbours g node |> List.filter (fun (x, _) -> not @@ List.mem x visited) in aux_list neighbours (node :: visited) (fun (l, weigth') -> if List.length l = 0 then cont ([], 0) else cont (node :: l, weigth' + weigth)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) cont = let rec f l = match l with | [] -> cont ([], 0) | h::tl -> aux_node h visited (fun (l, weight') -> if List.length l = 0 then f tl else cont (l, weight')) in f nodes in let sc () = raise Fail in aux_list (neighbours g a) [a] (fun result -> match result with | [], _ -> sc () | path, weight -> a :: path, weight);;
let find_all_paths (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) list = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) list = let node, weigth = node in if List.mem node visited then [[], 0] else if node = b then [[node], weigth] else;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let path = find_all_paths g a b in List.fold_left (fun acc (l, weight) -> match acc with | Some (l', weight') -> if weight >= weight' then Some (l, weight) else acc | None -> Some (l, weight) ) None path;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let fail_cnt = ref 0 in let update_pass (old_pass:passwd) (new_pass:passwd) = if (old_pass = !password) then let _ = (fail_cnt := 0 ) in password := new_pass else (let _ =(fail_cnt := fail_cnt.contents + 1) in raise wrong_pass) in let deposit (pass:passwd) (amount:int) = if (fail_cnt.contents > 4) then raise too_many_failures else if (pass = !password) then let _ = (fail_cnt := 0 ) in if (amount < 0) then raise negative_amount else balance := !balance + amount else (let _ = (fail_cnt := fail_cnt.contents + 1) in raise wrong_pass) in let retrieve (pass:passwd) (amount:int) = if (fail_cnt.contents > 4) then raise too_many_failures else if (pass = !password) then let _ = (fail_cnt := 0 ) in if (amount < 0 ) then raise negative_amount else if (amount <= !balance) then balance := !balance - amount else raise not_enough_balance else (let _ = (fail_cnt := fail_cnt.contents + 1) in raise wrong_pass) in let show_balance (pass:passwd) = if (fail_cnt.contents > 4) then raise too_many_failures else if (pass = !password) then let _ = (fail_cnt := 0 ) in !balance else (let _ = (fail_cnt := fail_cnt.contents + 1) in raise wrong_pass) in {update_pass = update_pass; retrieve = retrieve; deposit = deposit ; show_balance = show_balance} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper edges v acc = match edges with | [] -> acc |(v1,v2,w) :: tl when v1 = v -> helper tl v ((v2,w) :: acc) | _ :: tl -> helper tl v acc in helper 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) = match node, visited with | (v,w) , visited when v = b -> ([],0) | (v,w) , visited when List.exists ((=) v) visited -> raise Fail | (v,w) , visited when v = a -> ( try let (ls,totalWeight) = aux_list (neighbours g v) (v :: visited) in (v :: ls, totalWeight) with Fail -> raise Fail ) | (v,w) , visited -> aux_list (neighbours g v) (v :: visited) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (v,weight) :: tl -> try let (ls,totalWeight) = aux_node (v, weight) visited in ( v::ls , totalWeight+weight) with Fail -> match nodes with | [] -> raise 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, visited with | (v,w) , visited when v = b -> sc ([],0) | (v,w) , visited when List.exists ((=) v) visited -> fc () | (v,w) , visited when v = a -> let sc2 = (fun (ls,totalweight) -> sc (v::ls,totalweight)) in aux_list (neighbours g v) (v :: visited) fc sc2 | (v,w) , visited -> aux_list (neighbours g v) (v :: visited) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (v,weight) :: tl -> let sc2 = (fun (ls,totalweight) -> sc (v::ls,totalweight+weight)) in let fc2 = (fun () -> aux_list tl visited fc sc) in aux_node (v,weight) visited fc2 sc2 in aux_node (a,0) [] (fun () -> raise Fail) (fun (ls,w) -> (ls,w));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match find_all_paths g a b with | [] -> None | listOfPaths -> Some(List.fold_right (fun (ls1,w1) -> fun (ls2,w2)-> if w1>w2 then (ls1,w1) else (ls2,w2)) listOfPaths ([],0));;
let new_counter () = let counter = ref 0 in { tick = (fun () -> counter := !counter + 1; !counter); reset = (fun () -> counter := 0); };;
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let password = ref initial_pass in let fail_counter = ref 0 in { update_pass = ( fun p1 p2 -> if compare p1 !password != 0 then ( fail_counter := !fail_counter + 1; raise wrong_pass ) else ( fail_counter := 0; password := p2 ) ); retrieve = ( fun p1 amount -> if !fail_counter > 4 then raise too_many_failures else if compare p1 !password != 0 then ( fail_counter := !fail_counter + 1; raise wrong_pass ) else if amount < 0 then raise negative_amount else if amount > !balance then raise not_enough_balance else ( fail_counter := 0; balance := !balance - amount ) ); deposit = ( fun p1 amount -> if !fail_counter > 4 then raise too_many_failures else if compare p1 !password != 0 then ( fail_counter := !fail_counter + 1; raise wrong_pass ) else if amount < 0 then raise negative_amount else ( fail_counter := 0; balance := !balance + amount ) ); show_balance = ( fun p1 -> if !fail_counter > 4 then raise too_many_failures else if compare p1 !password != 0 then ( fail_counter := !fail_counter + 1; raise wrong_pass ) else ( fail_counter := 0; !balance ) ) };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec rec_neighbours (edges: ('a * 'a * weight) list) (rec_vertex: 'a) out_list : ('a * weight) list = match edges with | [] -> out_list | (a,b,c)::rest -> if a = rec_vertex then rec_neighbours rest rec_vertex ((b,c)::out_list) else rec_neighbours rest rec_vertex out_list in rec_neighbours g.edges vertex [];; let element_exists (tup: 'a * weight) (l: 'a list): bool = match tup with | (a,b) when List.exists (fun el -> el = a) l -> true | _ -> false;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (acc: int): ('a list * weight) = match node with | (x,y) when x = b -> (List.rev (x::visited), acc + y) | (x,y) -> aux_list (neighbours g x) (x::visited) (acc+y) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc: int) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs when (element_exists x visited) -> aux_list xs visited acc | x::xs -> try aux_node x visited acc with Fail -> aux_list xs visited acc 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 fc sc : ('a list * weight) = match node with | (x,y) when x = b -> ((visited [x]), sc y) | (x,y) -> aux_list (neighbours g x) (fun r -> visited (x::r)) fc (fun r -> sc (r + y)) and aux_list (nodes: ('a * weight) list) visited fc sc : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs when (element_exists x (visited [])) -> aux_list xs visited fc sc | x::xs -> try aux_node x visited fc sc with Fail -> aux_list xs visited fc sc in aux_list (neighbours g a) (fun v -> a::v) (fun p -> p) (fun q -> q);;
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 balance = ref 0 in let acc_password = ref initial_pass in let num_of_failures = ref 0 in let check_pass (pwd : passwd) : bool = (pwd = !acc_password) in let login (pwd : passwd) (can_be_locked_out : bool) : unit = if (can_be_locked_out && !num_of_failures >= 5) then raise too_many_failures else ( if (not (check_pass pwd)) then ( num_of_failures := !num_of_failures + 1 ; raise wrong_pass ) else ( num_of_failures := 0; ) ) in let bank_acc = { update_pass = (fun old_pass new_pass -> login old_pass false ; acc_password := new_pass; num_of_failures := 0; ); retrieve = (fun password amount -> login password true ; if (amount < 0) then raise negative_amount; if (!balance - amount >= 0) then balance := !balance - amount else raise not_enough_balance ); deposit= (fun password amount -> login password true ; if (amount >= 0) then balance := !balance + amount else raise negative_amount ); show_balance = (fun password -> login password true; !balance ) } in bank_acc ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun curr_edge l -> match curr_edge with | (v1, v2, w) when v1 = vertex -> List.append l ((v2,w)::[]) | _ -> l ) g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (w_tot : int) : ('a list * weight) = match node with | (v, weight) when v = b -> (List.rev (v :: visited), w_tot + weight) | (v, weight) -> let next_nodes = (neighbours g v) in if next_nodes = [] then raise Fail else aux_list next_nodes (v :: visited) (w_tot + weight); and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w_tot : int): ('a list * weight) = match nodes with | [] -> raise Fail | hd::tl -> (try let (h, _) = hd in if (List.mem (h) visited) then raise Fail else aux_node hd visited w_tot with Fail -> aux_list tl visited w_tot) in aux_node (a, 0) [] 0 ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_list (node: 'a * weight) (visited : 'a list) (fc : unit -> 'b) (sc : 'a list -> weight -> 'a list * weight) = let (v, _) = node in let next_nodes = List.filter (fun (n : 'a * weight) -> let (value, _) = n in not (List.mem value visited)) (neighbours g v) in match next_nodes with | [] -> fc () | (hd_val, hd_weight)::_ -> (if hd_val = b then sc (hd_val::[]) (hd_weight) else let suc2 = fun (n : 'a list) (x : weight) -> sc (hd_val :: n) (hd_weight + x) in let fail2 = fun () -> (aux_list (node) (hd_val::visited) fc sc) in aux_list (hd_val, hd_weight) (hd_val::visited) fail2 suc2) in let sc = fun (n : 'a list) (x : weight) -> (a::n, x) in let fc = fun () -> raise Fail in aux_list (a, 0) [a] fc sc ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec aux_rec (options : ('a list * weight) list) (biggest_weight : int) (biggest_path : ('a list * weight)) : ('a list * weight) = match options with | [] -> biggest_path | hd::tl -> (let (_, curr_weight) = hd in if (curr_weight > biggest_weight) then aux_rec tl curr_weight hd else aux_rec tl biggest_weight biggest_path) in let options = find_all_paths g a b in let bp = aux_rec options 0 ([],0) in if bp = ([],0) then None else Some bp ;;
let open_account (initial_pass: passwd) : bank_account = let pwd = ref initial_pass in let balance = ref 0 in let fld_attempts = ref 0 in let update_pass (x:passwd) (y:passwd) = if x=pwd.contents then (fld_attempts:= 0;pwd := y) else (fld_attempts:=fld_attempts.contents + 1;raise wrong_pass) in let deposit (x:passwd) (y:int) = if fld_attempts.contents >=5 then raise too_many_failures else (if x=pwd.contents then (fld_attempts:= 0;(if y<0 then raise negative_amount else balance:=balance.contents + y)) else (fld_attempts:=fld_attempts.contents + 1;raise wrong_pass)) in let retrieve (x:passwd) (y:int) = if fld_attempts.contents >=5 then raise too_many_failures else (if x=pwd.contents then (fld_attempts:= 0;(if y<0 then raise negative_amount else (if y>balance.contents then raise not_enough_balance else balance:=balance.contents - y))) else (fld_attempts:=fld_attempts.contents + 1;raise wrong_pass)) in let show_balance (x:passwd) = if fld_attempts.contents >=5 then raise too_many_failures else (if x=pwd.contents then (fld_attempts:= 0;balance.contents) else (fld_attempts:=fld_attempts.contents + 1;raise wrong_pass)) in {update_pass = update_pass; retrieve = retrieve; deposit = deposit; show_balance = show_balance} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec edig (edg: ('a * 'a * weight) list) (v: 'a) = match edg with | [] -> [] | (vertex,u,k)::tl when vertex=v -> (u,k)::edig tl v | (vertex,u,k)::tl -> edig tl v in edig 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) = match node,visited with | (x,u),visited when x=b -> ([b],u) | (x,u),visited when x=a && not (List.exists (fun elm -> elm=a) visited) -> (try let (l,w) =aux_list (neighbours g x) (x::visited) in (a::l,w+u) with Fail -> raise Fail) | (x,u),visited when List.exists (fun elm -> elm=x) visited -> raise Fail | (x,u),visited -> (try let (l,w) =aux_list (neighbours g x) (x::visited) in (x::l,w+u) with Fail -> raise Fail) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes,visited with | [],visited -> raise Fail | (x,u)::tl,visited -> (try aux_node (x,u) visited with Fail -> match nodes with | [] -> raise Fail | (x,u)::tl -> 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,visited with | (x,u),visited when x=b -> sc ([b],u) | (x,u),visited when x=a && not (List.exists (fun elm -> elm=a) visited) -> (let sc_tl=(fun (l,w)->sc (a::l,w+u)) in aux_list (neighbours g x) (x::visited) fc sc_tl) | (x,u),visited when List.exists (fun elm -> elm=x) visited -> fc () | (x,u),visited -> (let sc_tl=(fun (l,w)->sc (x::l,w+u)) in aux_list (neighbours g x) (x::visited) fc sc_tl) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes,visited with | [],visited -> fc () | (x,u)::tl,visited -> (let fc_tl=(fun ()->aux_list tl visited fc sc) in aux_node (x,u) visited fc_tl 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 = match (find_all_paths g a b) with | [] -> None | x::xs -> Some(List.fold_left (fun (c,d)-> fun (h,i)-> if d>i then (c,d) else (h,i)) x xs);;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass and account_balance = ref 0 and wrong_attempts = ref 0 in { update_pass = (fun (old_pw: passwd) (new_pw: passwd) -> if old_pw = !password then (password := new_pw; wrong_attempts := 0) else (wrong_attempts := !wrong_attempts + 1; raise wrong_pass) ); deposit = (fun (pass_check: passwd) (amount: int) -> if !wrong_attempts < 5 then if pass_check = !password then if amount >= 0 then (account_balance := !account_balance + amount; wrong_attempts := 0) else raise negative_amount else (wrong_attempts := !wrong_attempts + 1; raise wrong_pass) else raise too_many_failures); retrieve = (fun (pass_check: passwd) (amount: int) -> if !wrong_attempts < 5 then if pass_check = !password then if amount >= 0 then if amount <= !account_balance then (account_balance := !account_balance - amount; wrong_attempts := 0) else raise not_enough_balance else raise negative_amount else (wrong_attempts := !wrong_attempts + 1; raise wrong_pass) else raise too_many_failures); show_balance = (fun (pass_check: passwd) -> if !wrong_attempts < 5 then if pass_check = !password then (wrong_attempts := 0; !account_balance) else (wrong_attempts := !wrong_attempts + 1; raise wrong_pass) else raise too_many_failures); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc (v1, v2, w) -> if v1 = vertex then (v2, w) :: acc else acc) []g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((node, w): 'a * weight) (visited: 'a list) : ('a list * weight) = if node = b then ([b] , w) else if List.mem node visited then raise Fail else let (path, cost) = aux_list (neighbours g node) (node :: visited) in (node :: path, w + cost) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (node, w) :: tl -> try aux_node (node, w) 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, w): 'a * weight) (visited: 'a list) fc sc : ('a list * weight) = if node = b then sc ([b], w) else if List.mem node visited then fc () else aux_list (neighbours g node) (node :: visited) fc (fun (path, cost) -> sc(node :: path, w + cost)) 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 list -> list);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max l = match l with | [] -> None | [(path,w)] -> Some(path,w) | (path1,w1) :: (path2,w2) :: rest -> if w1 >= w2 then max ((path1,w1) :: rest) else max ((path2,w2) :: rest) in max (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass and account_balance = ref 0 and wrong_attempts = ref 0 in { update_pass = (fun (old_pw: passwd) (new_pw: passwd) -> if old_pw = !password then (password := new_pw; wrong_attempts := 0) else (wrong_attempts := !wrong_attempts + 1; raise wrong_pass) ); deposit = (fun (pass_check: passwd) (amount: int) -> if !wrong_attempts < 5 then if pass_check = !password then if amount >= 0 then (account_balance := !account_balance + amount; wrong_attempts := 0) else raise negative_amount else (wrong_attempts := !wrong_attempts + 1; raise wrong_pass) else raise too_many_failures); retrieve = (fun (pass_check: passwd) (amount: int) -> if !wrong_attempts < 5 then if pass_check = !password then if amount >= 0 then if amount <= !account_balance then (account_balance := !account_balance - amount; wrong_attempts := 0) else raise not_enough_balance else raise negative_amount else (wrong_attempts := !wrong_attempts + 1; raise wrong_pass) else raise too_many_failures); show_balance = (fun (pass_check: passwd) -> if !wrong_attempts < 5 then if pass_check = !password then (wrong_attempts := 0; !account_balance) else (wrong_attempts := !wrong_attempts + 1; raise wrong_pass) else raise too_many_failures); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc (v1, v2, w) -> if v1 = vertex then (v2, w) :: acc else acc) []g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((node, w): 'a * weight) (visited: 'a list) : ('a list * weight) = if node = b then ([b] , w) else if List.mem node visited then raise Fail else let (path, cost) = aux_list (neighbours g node) (node :: visited) in (node :: path, w + cost) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (node, w) :: tl -> try aux_node (node, w) 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, w): 'a * weight) (visited: 'a list) fc sc : ('a list * weight) = if node = b then sc ([b], w) else if List.mem node visited then fc () else aux_list (neighbours g node) (node :: visited) fc (fun (path, cost) -> sc(node :: path, w + cost)) 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 list -> list);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max l = match l with | [] -> None | [(path,cost)] -> Some(path,cost) | (path1,cost1) :: (path2,cost2) :: rest -> if cost1 >= cost2 then max ((path1,cost1) :: rest) else max ((path2, cost2) :: rest) in max (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let curr_balance = ref 0 in let fail_attempts = ref 0 in { update_pass = (fun old_pw new_pw -> if !fail_attempts > 4 then begin if !password = old_pw then begin password := new_pw; fail_attempts := 0; end else raise wrong_pass end else if !password = old_pw then begin password := new_pw; fail_attempts := 0; end else begin fail_attempts := !fail_attempts + 1; raise wrong_pass; end); retrieve = (fun pw amount -> if !fail_attempts > 4 then raise too_many_failures else if !password = pw then begin fail_attempts := 0; if amount < 0 then raise negative_amount else if amount > !curr_balance then raise not_enough_balance else curr_balance := !curr_balance - amount end else begin fail_attempts := !fail_attempts + 1; raise wrong_pass; end); deposit = (fun pw amount -> if !fail_attempts > 4 then raise too_many_failures else if !password = pw then begin fail_attempts := 0; if amount > 0 then curr_balance := !curr_balance + amount else raise negative_amount end else begin fail_attempts := !fail_attempts + 1; raise wrong_pass; end); show_balance = (fun pw -> if !fail_attempts > 4 then raise too_many_failures else if !password = pw then begin fail_attempts := 0; !curr_balance end else begin fail_attempts := !fail_attempts + 1; raise wrong_pass; end) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edge lst (s, t, w) = if s = vertex then (t,w) :: lst else lst in List.fold_left edge [] 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) (cost: int) : ('a list * weight) = let (n,w) = node in if n = b then (visited, w) else let neighbour_set = neighbours g n in if List.exists (fun x -> x = n) visited then aux_list [] visited cost else aux_list neighbour_set (visited @ [n]) (cost+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (cost: int) : ('a list * weight) = match nodes with [] -> raise Fail | x :: xs -> ( let (n,w) = x in if n = b then (visited@[n], w + cost) else try aux_node x visited cost with Fail -> aux_list xs visited cost) 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) (cost: int) fc sc : ('a list * weight)= let (n,w) = node in if n = b then sc (visited, w) else let neighbour_set = neighbours g n in if List.exists (fun x -> x = n) visited then aux_list [] visited cost fc sc else aux_list neighbour_set (visited @ [n]) (cost+w) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (cost: int) fc sc : ('a list * weight) = match nodes with [] -> fc () | x :: xs -> ( let (n,w) = x in if n = b then sc (visited@[n], w + cost) else let suc2 = sc in let fail2 = (fun() -> aux_list xs visited cost fc sc) in aux_node x visited cost fail2 suc2) in aux_node (a,0) [] 0 (fun() -> raise Fail) (fun x -> x);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let max = ref 0 in let result = ref ([],0) in let rec find_max paths = match paths with [] -> None | (l,w) :: [] -> if w > !max then Some (l,w) else Some !result | (l,w) :: xs -> if w > !max then begin max := w; result := (l,w); find_max xs end else find_max xs in let all_paths = find_all_paths g a b in find_max all_paths;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let curr_bal = ref 0 in let counter = ref 0 in { update_pass = (fun initial_pass new_pass -> if (initial_pass = !password) then (password := new_pass; counter := 0) else (counter := (!counter + 1); raise (wrong_pass))); deposit = (fun initial_pass amount -> if (!counter >= 5) then raise (too_many_failures) else if (initial_pass = !password) then (if amount < 0 then raise (negative_amount) else curr_bal := (!curr_bal + amount); if !counter > 0 then counter := 0) else (counter := (!counter + 1); raise (wrong_pass))); retrieve = (fun initial_pass money -> if (!counter >= 5) then raise (too_many_failures) else if (initial_pass = !password) then (if !counter > 0 then counter := 0; if money < 0 then raise (negative_amount) else if (!curr_bal >= money) then curr_bal := (!curr_bal - money) else raise (not_enough_balance)) else (counter := (!counter + 1); raise (wrong_pass))); show_balance = (fun initial_pass -> if (!counter >= 5) then raise (too_many_failures) else if (initial_pass = !password) then ( counter := 0; !curr_bal ) else (counter := (!counter + 1); raise (wrong_pass))); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun l (v1, v2, w) -> if (v1 = vertex) then (v2, w)::l else l) [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let weigh = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (x,y) = node in if (x=b) then (List.rev (x::visited), (!weigh + y)) else ((weigh := !weigh + y); aux_list (neighbours g x) (x::visited)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise (Fail) | h::t -> let (p,q) = h in if (List.mem p visited) then raise (Fail) else try aux_node h visited with Fail -> weigh := 0; aux_list t visited in aux_list (neighbours g a) [a];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let 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 aux_list (neighbours g a) [a] (raise Fail) ([],0);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths (g: 'a graph) (a: 'a) (b: 'a) in let [] = all_paths in None;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let curr_bal = ref 0 in let counter = ref 0 in { update_pass = (fun initial_pass new_pass -> if (initial_pass = !password) then (password := new_pass; counter := 0) else (counter := (!counter + 1); raise (wrong_pass))); deposit = (fun initial_pass amount -> if (!counter >= 5) then raise (too_many_failures) else if (initial_pass = !password) then (if amount < 0 then raise (negative_amount) else curr_bal := (!curr_bal + amount); if !counter > 0 then counter := 0) else (counter := (!counter + 1); raise (wrong_pass))); retrieve = (fun initial_pass money -> if (!counter >= 5) then raise (too_many_failures) else if (initial_pass = !password) then (if !counter > 0 then counter := 0; if money < 0 then raise (negative_amount) else if (!curr_bal >= money) then curr_bal := (!curr_bal - money) else raise (not_enough_balance)) else (counter := (!counter + 1); raise (wrong_pass))); show_balance = (fun initial_pass -> if (!counter >= 5) then raise (too_many_failures) else if (initial_pass = !password) then ( counter := 0; !curr_bal ) else (counter := (!counter + 1); raise (wrong_pass))); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun l (v1, v2, w) -> if (v1 = vertex) then (v2, w)::l else l) [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let weigh = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (x,y) = node in if (x=b) then (List.rev (x::visited), (!weigh + y)) else ((weigh := !weigh + y); aux_list (neighbours g x) (x::visited)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise (Fail) | h::t -> let (p,q) = h in if (List.mem p visited) then raise (Fail) else try aux_node h visited with Fail -> weigh := 0; aux_list t visited in aux_list (neighbours g a) [a];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let weigh = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (x,y) = node in if (x=b) then (List.rev (x::visited), (!weigh + y)) else ((weigh := !weigh + y); aux_list (neighbours g x) (x::visited) (raise Fail) (fun x -> x)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc | h::t -> let (p,q) = h in if (List.mem p visited) then fc else try aux_node h visited (raise Fail) (fun x -> x) with Fail -> weigh := 0; aux_list t visited (raise Fail) (fun x -> x) in aux_list (neighbours g a) [a] (raise Fail) (fun x -> x);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths (g: 'a graph) (a: 'a) (b: 'a) in let [] = all_paths in None;;
let open_account (pass : string) : bank_account = let password = ref pass in let balance = ref 0 in let wrong_pass_counter = ref 0 in { update_pass = (fun pass pass1 -> if pass = !password then begin password := pass1; wrong_pass_counter := 0 end else begin incr wrong_pass_counter; raise wrong_pass end ); retrieve = (fun pass amount -> if !wrong_pass_counter = 5 then raise too_many_failures; if pass = !password then begin if amount < 0 then raise negative_amount else if amount <= balance.contents then begin balance := !balance - amount; wrong_pass_counter := 0 end else raise not_enough_balance end else begin incr wrong_pass_counter; raise wrong_pass end); deposit = (fun pass amount -> if !wrong_pass_counter = 5 then raise too_many_failures; if pass = !password then begin if amount < 0 then raise negative_amount else begin balance := !balance + amount; wrong_pass_counter := 0 end end else begin incr wrong_pass_counter; raise wrong_pass end); show_balance = (fun pass -> if !wrong_pass_counter = 5 then raise too_many_failures; if pass = !password then begin wrong_pass_counter := 0; !balance end else begin incr wrong_pass_counter; raise wrong_pass end); };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc (v1, v2, w) -> if v1 = vertex then (v2, w) :: acc else acc) [] (List.rev g.edges);;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let nghb = neighbours g (fst node) in let nghb_upd = List.fold_left (fun acc (v, w) -> if List.mem v visited then acc else acc @ [(v, w + (snd node))]) [] nghb in aux_list nghb_upd (fst node :: visited) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (v, w) :: tl -> if v = b then (List.rev (v :: visited), w) else begin try aux_node (v, w) visited with | Fail -> aux_list tl visited end 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 : 'a list) (sc : weight): ('a list * weight) = let nghb = neighbours g (fst node) in let nghb_upd = List.filter (fun (v,w) -> not (List.mem v visited)) nghb in aux_list nghb_upd (fst node :: visited) (fst node :: visited) (snd node + sc) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (fc : 'a list) (sc : weight): ('a list * weight) = match nodes with | [] -> raise Fail | (v, w) :: tl -> if v = b then (List.rev (v ::fc), w + sc) else begin try aux_node (v, w) visited fc sc with | Fail -> aux_list tl visited fc sc end in aux_node (a, 0) [] [] 0;;
let find_longest_path (g : 'a graph) (a : 'a) (b : 'a) : ('a list * weight) option = match find_all_paths g a b with | [] -> None | lps -> List.fold_left (fun acc (lp, w) -> begin match acc with | None -> Some (lp, w) | Some (_, w_acc) -> if w > w_acc then Some (lp,w) else acc end) None lps ;;
let open_account (pass : string) : bank_account = let password = ref pass in let balance = ref 0 in let wrong_pass_counter = ref 0 in { update_pass = (fun pass pass1 -> if pass = !password then begin password := pass1; wrong_pass_counter := 0 end else begin incr wrong_pass_counter; raise wrong_pass end ); retrieve = (fun pass amount -> if !wrong_pass_counter = 5 then raise too_many_failures; if pass = !password then begin if amount < 0 then raise negative_amount else if amount <= balance.contents then begin balance := !balance - amount; wrong_pass_counter := 0 end else raise not_enough_balance end else begin incr wrong_pass_counter; raise wrong_pass end); deposit = (fun pass amount -> if !wrong_pass_counter = 5 then raise too_many_failures; if pass = !password then begin if amount < 0 then raise negative_amount else begin balance := !balance + amount; wrong_pass_counter := 0 end end else begin incr wrong_pass_counter; raise wrong_pass end); show_balance = (fun pass -> if !wrong_pass_counter = 5 then raise too_many_failures; if pass = !password then begin wrong_pass_counter := 0; !balance end else begin incr wrong_pass_counter; raise wrong_pass end); };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc (v1, v2, w) -> if v1 = vertex then (v2, w) :: acc else acc) [] (List.rev g.edges);;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let nghb = neighbours g (fst node) in let nghb_upd = List.fold_left (fun acc (v, w) -> if List.mem v visited then acc else acc @ [(v, w + (snd node))]) [] nghb in aux_list nghb_upd (fst node :: visited) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (v, w) :: tl -> if v = b then (List.rev (v :: visited), w) else begin try aux_node (v, w) visited with | Fail -> aux_list tl visited end 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 : 'a list) (sc : weight): ('a list * weight) = let nghb = neighbours g (fst node) in let nghb_upd = List.filter (fun (v,w) -> not (List.mem v visited)) nghb in aux_list nghb_upd (fst node :: visited) (fst node :: visited) (snd node + sc) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (fc : 'a list) (sc : weight): ('a list * weight) = match nodes with | [] -> raise Fail | (v, w) :: tl -> if v = b then (List.rev (v ::fc), w + sc) else begin try aux_node (v, w) visited fc sc with | Fail -> aux_list tl visited fc sc end in aux_node (a, 0) [] [] 0;;
let find_longest_path (g : 'a graph) (a : 'a) (b : 'a) : ('a list * weight) option = match find_all_paths g a b with | [] -> None | lps -> List.fold_left (fun acc (lp, w) -> begin match acc with | None -> Some (lp, w) | Some (_, w_acc) -> if w > w_acc then Some (lp,w) else acc end) None lps ;;
let open_account (initial_pass: passwd) : bank_account = let pass=ref initial_pass and balance=ref 0 and failCount= ref 0 in { update_pass=(fun (p:passwd)(n:passwd) -> if(p=(!pass)) then ( failCount:=0; pass:=n ) else ( failCount:=(!failCount)+1; raise wrong_pass) ); retrieve=(fun (p:passwd)(amount:int)-> if((!failCount)>=5) then raise too_many_failures else if (p!=(!pass)) then ( failCount:= (!failCount)+1; raise wrong_pass ) else ( if(amount<0) then ( failCount:= 0; raise negative_amount ) else if((!balance)>=amount) then ( failCount:= 0; balance:=(!balance)-amount ) else ( failCount:= 0; raise not_enough_balance ))); deposit =(fun (p:passwd)(amount:int)-> if((!failCount)>=5) then raise too_many_failures else if ((!pass)!=p) then ( failCount:= (!failCount)+1; raise wrong_pass ) else ( if(amount<0) then( failCount:= 0; raise negative_amount ) else ( failCount:= 0; balance:=(!balance)+amount ) )); show_balance =(fun (p:passwd)-> if((!failCount)>=5) then raise too_many_failures else if (p!=(!pass)) then ( failCount:= (!failCount)+1; raise wrong_pass ) else ( failCount:= 0; !balance )) } ;; let containNode (target: 'a)(edge:'a * 'a * weight)= let (a,_,_) = edge in a = target ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec addNode (filter_l:('a * 'a * weight) list)(result:('a * weight) list):('a * weight) list= match filter_l with |[]-> result |x::xs-> let (_,e,n) = x in addNode (xs) ((e,n)::result) in addNode (List.filter (containNode vertex) (g.edges)) [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec dfs (n: ('a * weight) list) (path:'a list * weight)(visited : 'a list): ('a list * weight) = if(List.length n=0) then raise Fail else if(List.exists (fun(a:'a)-> a=(fst(List.hd n))) visited) then dfs (List.tl n) path visited else try match List.hd n with |(x,y) when x=b->(fst path @ [x],(snd path)+y) |(x,y)-> dfs (neighbours g x) (fst path @ [x],(snd path)+y) ([x] @ visited) with Fail-> dfs (List.tl n) path visited in dfs (neighbours g a) ([a],0) [a] ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec dfs (n: ('a * weight) list) (path:'a list * weight)(visited : 'a list): ('a list * weight) = if(List.length n=0) then raise Fail else if(List.exists (fun(a:'a)-> a=(fst(List.hd n))) visited) then dfs (List.tl n) path visited else try match List.hd n with |(x,y) when x=b->(fst path @ [x],(snd path)+y) |(x,y)-> dfs (neighbours g x) (fst path @ [x],(snd path)+y) ([x] @ visited) with Fail-> dfs (List.tl n) path visited in dfs (neighbours g a) ([a],0) [a] ;; let get_last (l: 'a list):'a= let rev_l=List.rev l in List.hd rev_l ;; let remove_last (l: 'a list):'a list= let rev_l=List.rev l in let tl_l=List.tl rev_l in List.rev tl_l ;; let lastNode (path:'a list) (l:'a) :('a * 'a) = let prev=get_last(path) in (prev,l) ;; let containEdge (target: 'a*'a)(edge:'a * 'a * weight):bool= let (a,b,_) = edge in not ((a = (fst target)) && (b = (snd target))) ;; let rec removeNode (g:'a graph)(path:'a list * weight):'a graph= let target=lastNode (remove_last(fst(path))) (get_last (fst(path))) in {nodes =g.nodes;edges =(List.filter (containEdge target) g.edges)} ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let l=find_all_paths g a b in if(List.length l=0)then None else let rec long_path (l:('a list * weight) list) (current:('a list * weight)):('a list * weight)option= match l with |[]->Some(current) |x::xs->if(snd(x)>snd(current)) then long_path xs x else long_path xs current in long_path l ([a],min_int) ;;
let open_account (initial_pass: passwd) : bank_account = let old_password = ref initial_pass in let balance = ref 0 in let wrongpasscount = ref 0. in let update (initpass : passwd) (newpass : passwd) = if initpass <> !old_password then let _ = (wrongpasscount := !wrongpasscount +. 1.) in raise wrong_pass else let _ = (wrongpasscount := 0.) in old_password := newpass; in let ret (password : passwd) (number : int) = if !wrongpasscount >= 5. then raise too_many_failures else if password <> !old_password then let _ = (wrongpasscount := !wrongpasscount +. 1.) in raise wrong_pass else let _ = (wrongpasscount := 0.) in if number < 0 then raise negative_amount else if number > !balance then raise not_enough_balance else balance := !balance - number; in let dep (password : passwd) (number : int) = if !wrongpasscount >= 5. then raise too_many_failures else if password <> !old_password then let _ = (wrongpasscount := !wrongpasscount +. 1.) in raise wrong_pass else let _ = (wrongpasscount := 0.) in if number < 0 then raise negative_amount else balance := !balance + number; in let show (password : passwd) : int = if !wrongpasscount >= 5. then raise too_many_failures else if password <> !old_password then let _ = (wrongpasscount := !wrongpasscount +. 1.) in raise wrong_pass else let _ = (wrongpasscount := 0.) in !balance; in { update_pass = update; retrieve = ret; deposit = dep; show_balance = show; } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec getNodes (theList : ('a * 'a * weight) list) (thing :'a) : ('a * weight) list = match theList with | [] -> [] | c :: d -> let (f, g, w) = c in if f = thing then [(g, w)] @ getNodes d thing else getNodes d thing in getNodes g.edges vertex ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let ret = ref [a] in let rec popList nod visit = if List.mem nod visit then if !ret = [] then raise Fail else if List.hd (List.rev !ret) = nod then let _ = ret := List.rev (List.tl (List.rev !ret)) in if !ret = [] then raise Fail else popList (List.hd (List.rev !ret)) visit else !ret else !ret in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (c, d) = node in let l = neighbours g c in let rec checkNeighbors p = match p with |[] -> if !ret = [] then raise Fail else let number = List.hd (List.rev !ret) in let _ = ret := List.rev (List.tl (List.rev !ret)) in if !ret = [] then raise Fail else aux_list (neighbours g (List.hd (List.rev !ret))) (visited @ [number]) |q :: r -> let (h, z) = q in if List.mem b !ret then (!ret, d) else if h = b then let _ = ret := !ret @ [h] in (!ret, z+d) else if (List.mem h visited) then let _ = ret := popList h (visited) in checkNeighbors r else if List.mem h !ret || h = a then checkNeighbors r else if neighbours g c = [] then let _ = ret := popList c (visited @ [c]) in if !ret = [] then raise Fail else aux_list (neighbours g (List.hd (List.rev !ret))) (visited @ [c]) else let (y, i) = aux_list (neighbours g c) (visited) in if (List.mem c !ret) then (!ret, d + i) else (!ret, i + 0) in checkNeighbors l and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = if List.mem b !ret then let (two, three) = (List.hd (List.rev nodes)) in aux_node (List.hd (List.rev !ret), three) visited else match nodes with |[] -> if !ret = [] then raise Fail else let num = (List.hd (List.rev !ret)) in let _ = ret := popList num (visited @ [num])in if !ret = [] then raise Fail else aux_node (List.hd (List.rev !ret), 0) (visited @ [num]) |j :: v -> let (w, f) = j in if List.mem w visited then aux_list (v) visited else if (List.mem w !ret) || w = a then aux_list (v) visited else let _ = ret := !ret @ [w] in aux_node j visited in aux_node (a, 0) [] ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let ret = ref [a] in let rec popList nod visit = if List.mem nod visit then if !ret = [] then raise Fail else if List.hd (List.rev !ret) = nod then let _ = ret := List.rev (List.tl (List.rev !ret)) in if !ret = [] then raise Fail else popList (List.hd (List.rev !ret)) visit else !ret else !ret in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (c, d) = node in let l = neighbours g c in let rec checkNeighbors p = match p with |[] -> if !ret = [] then raise Fail else let number = List.hd (List.rev !ret) in let _ = ret := List.rev (List.tl (List.rev !ret)) in if !ret = [] then raise Fail else aux_list (neighbours g (List.hd (List.rev !ret))) (visited @ [number]) |q :: r -> let (h, z) = q in if List.mem b !ret then (!ret, d) else if h = b then let _ = ret := !ret @ [h] in (!ret, z+d) else if (List.mem h visited) then let _ = ret := popList h (visited) in checkNeighbors r else if List.mem h !ret || h = a then checkNeighbors r else if neighbours g c = [] then let _ = ret := popList c (visited @ [c]) in if !ret = [] then raise Fail else aux_list (neighbours g (List.hd (List.rev !ret))) (visited @ [c]) else let (y, i) = aux_list (neighbours g c) (visited) in if (List.mem c !ret) then (!ret, d + i) else (!ret, i + 0) in checkNeighbors l and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = if List.mem b !ret then let (two, three) = (List.hd (List.rev nodes)) in aux_node (List.hd (List.rev !ret), three) visited else match nodes with |[] -> if !ret = [] then raise Fail else let num = (List.hd (List.rev !ret)) in let _ = ret := popList num (visited @ [num])in if !ret = [] then raise Fail else aux_node (List.hd (List.rev !ret), 0) (visited @ [num]) |j :: v -> let (w, f) = j in if List.mem w visited then aux_list (v) visited else if (List.mem w !ret) || w = a then aux_list (v) visited else let _ = ret := !ret @ [w] in aux_node (w, f) visited in aux_node (a, 0) [] ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec getLongest num longestPath lis = match lis with |[] -> if longestPath = ([], 0) then None else Some longestPath |a :: b -> let (l, p) = a in if p > num then getLongest p (l, p) b else getLongest num longestPath b in getLongest 0 ([], 0) (find_all_paths g a b) ;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let pass_attempts = ref 0 in let balance = ref 0 in let validate_pass pass = if !pass_attempts >= 5 then raise too_many_failures else if !password = pass then pass_attempts := 0 else (pass_attempts := !pass_attempts + 1; raise wrong_pass) in { update_pass = (fun old_pass new_pass -> if old_pass = !password then (password := new_pass; pass_attempts := 0) else (pass_attempts := !pass_attempts + 1; raise wrong_pass)); retrieve = (fun pass amount -> (validate_pass pass; if amount < 0 then raise negative_amount else if !balance >= amount then balance := !balance - amount else raise not_enough_balance)); deposit = (fun pass amount -> (validate_pass pass; if amount < 0 then raise negative_amount else balance := !balance + amount)); show_balance = (fun pass -> (validate_pass pass; !balance)) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun l (from, to_, w) -> if from = vertex then (to_, w) :: l else l ) [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec impl (nodes: ('a * weight) list) (path: 'a list) (w: weight) : ('a list * weight) = if (List.hd path) = b then (List.rev path, w) else match nodes with | [] -> raise Fail | ((e, ew)::rs) -> if List.mem e path then impl rs path w else try impl (neighbours g e) (e :: path) (w + ew) with Fail -> impl rs path w in impl (neighbours g a) [a] 0;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec impl (nodes: ('a * weight) list) (path: 'a list) (w: weight) (failure: unit -> ('a list * weight)) : ('a list * weight) = if (List.hd path) = b then (List.rev path, w) else match nodes with | [] -> failure () | ((e, ew)::rs) -> if List.mem e path then impl rs path w failure else impl (neighbours g e) (e :: path) (w + ew) (fun () -> impl rs path w failure) in impl (neighbours g a) [a] 0 (fun () -> raise Fail);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = List.fold_left ( fun path_a (pb, wb) -> match path_a with | None -> Some (pb, wb) | Some (pa, wa) -> if wa > wb then Some (pa, wa) else Some (pb, wb) ) None (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 fails = ref 0 in let validate (pass : passwd): bool = ( if (pass = !password) then (fails := 0; true) else (fails := !fails + 1; false) ) in { update_pass = (fun pass -> fun npass -> if (validate pass) then password := npass else raise wrong_pass ); retrieve = (fun pass -> fun amount -> if (!fails >= 5) then raise too_many_failures else if (validate pass) then if(amount < 0) then raise negative_amount else if (!balance < amount) then raise not_enough_balance else balance := !balance - amount else raise wrong_pass ); deposit = (fun pass -> fun amount -> if (!fails >= 5) then raise too_many_failures else if (validate pass) then if(amount < 0) then raise negative_amount else balance := !balance + amount else raise wrong_pass ); show_balance = (fun pass -> if (!fails >= 5) then raise too_many_failures else if (validate pass) then !balance else raise wrong_pass ) } ;; let g : string graph = { nodes = ["0"; "1"; "2"; "3"; "4"]; edges = [("1", "2", 3); ("1", "4", 3); ("2", "3", 5); ("2", "4", 6); ("3", "1", 4); ("3", "4", -7); ("4", "3", 7)] } in [ ((g, "0"), []); ((g, "1"),[("2", 3); ("4", 3)]); ((g, "2"),[("3", 5); ("4", 6)]); ((g, "3"),[("1", 4); ("4",-7)]); ((g, "4"),[("3", 7)]) ];;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let from_vertex = fun (v :'a) -> fun ((v1, _, _):('a * 'a * weight)) -> v = v1 in let out = fun ((_, v2, w) : ('a * 'a * weight)) -> (v2, w) in let valid_edges = List.filter (from_vertex vertex) g.edges in List.map out valid_edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (acc: weight) : ('a list * weight) = let (v, w) = node in if (List.mem v visited) then raise Fail else if (v = b) then (visited @ [v], acc+w) else aux_list (neighbours g v) (visited @ [v]) (acc+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc: weight) : ('a list * weight) = match nodes with | [] -> raise Fail | n :: ns -> try aux_node n visited acc with Fail -> aux_list ns visited acc 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) acc fc sc : ('a list * weight)= let (v, w) = node in if (v = b) then sc node (visited, acc) else if (List.mem v visited) then fc () else aux_list (neighbours g v) (visited @ [v]) (acc+w) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) acc fc sc : ('a list * weight) = match nodes with | [] -> fc () | n :: ns -> if (List.mem (fst n) visited) then aux_list ns visited acc fc sc else let fc2 = fun () -> (aux_list ns visited acc fc sc) in aux_node n visited acc fc2 sc in let fc = fun () -> raise Fail in let sc = fun ((v, w): 'a * weight) -> fun ((vis, weight) : 'a list * weight) -> (vis @ [v], weight + w) in aux_node (a,0) [] 0 fc 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 match paths with | [] -> None | _ -> let rec get_max (list : ('a list * weight) list) max = match list with | [] -> max | x :: xs -> if ((snd x) > (snd max)) then get_max xs (x) else get_max xs max in Some (get_max paths (List.hd paths));;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let attempts = ref 0 in let storage = ref 0 in { update_pass = (fun first second -> if !password <> first then( attempts := !attempts + 1; raise wrong_pass) else( password := second; attempts := 0)); retrieve = (fun pass amount -> if !attempts = 5 then raise too_many_failures else if !password <> pass then ( attempts := !attempts + 1; raise wrong_pass) else if amount < 0 || !storage < amount then( if amount < 0 then( attempts := 0; raise negative_amount) else( attempts := 0; raise not_enough_balance) ) else( storage := !storage - amount; attempts := 0)); deposit = (fun pass amount-> if !attempts = 5 then raise too_many_failures else if !password <> pass then ( attempts := !attempts + 1; raise wrong_pass) else if amount < 0 then( attempts := 0; raise negative_amount) else( storage := !storage + amount; attempts := 0)); show_balance = (fun pass -> if !attempts = 5 then raise too_many_failures else if !password <> pass then ( attempts := !attempts + 1; raise wrong_pass) else( attempts := 0; !storage)) } ;; let getFirst (a, _, _) = a let getSecond (_, b, _) = b let getThird (_, _, c) = c let rec lastElement lst = match lst with | [] -> raise Fail | [x] -> x | _::xs -> lastElement xs;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec aux (lst1 : ('a * 'a * weight) list) (lst2 : ('a * weight) list) = match lst1 with | [] -> lst2 | x::xs -> if getFirst x = vertex then aux xs ((getSecond x, getThird x)::lst2) else aux xs lst2 in aux 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) (path: 'a list): ('a list * weight) = if (fst node) = b then (path@[b], w + (snd node)) else (if (List.filter (fun a -> a = (fst node)) visited) = [] then aux_list g.edges (visited@[(fst node)]) (w+(snd node)) (path@[(fst node)]) else raise Fail) and aux_list (nodes: ('a * 'a * weight) list) (visited: 'a list) (w: weight) (path: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> if getFirst x = (lastElement path) then (try aux_node (getSecond x, getThird x) visited w path with Fail -> aux_list xs visited w path) else aux_list xs visited w path in aux_list g.edges [a] 0 [a];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node fail (node: 'a * weight) (visited : 'a list) (w: weight) (path: 'a list): ('a list * weight) = if (fst node) = b then (path@[b], w + (snd node)) else (if (List.filter (fun a -> a = (fst node)) visited) = [] then aux_list fail g.edges (visited@[(fst node)]) (w+(snd node)) (path@[(fst node)]) else fail ()) and aux_list success (nodes: ('a * 'a * weight) list) (visited: 'a list) (w: weight) (path: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> if getFirst x = (lastElement path) then aux_node (fun () -> aux_list success xs visited w path) (getSecond x, getThird x) visited w path else aux_list success xs visited w path in aux_list (fun () -> raise Fail) g.edges [a] 0 [a] ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec search paths max state = if (state = "first" && paths = []) then None else match paths with | [] -> Some max | x::xs -> if ((snd x) > (snd max)) then (let max = x in search xs max "no") else search xs max "no" in let paths = find_all_paths g a b in search paths ([], 0) "first" ;;
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 p1 p2 -> if p1 = !password then (counter := 0; password := p2) else (counter := !counter +1;raise wrong_pass)); retrieve = (fun p amount -> if !counter > 4 then raise too_many_failures else if p = !password then (if amount < 0 then (counter := 0;raise negative_amount) else if amount > !balance then (counter := 0;raise not_enough_balance) else (counter := 0;balance := !balance - amount)) else (counter := !counter +1;raise wrong_pass)); deposit = (fun p amount -> if !counter > 4 then raise too_many_failures else if p = !password then (if amount < 0 then (counter := 0;raise negative_amount) else (counter := 0;balance := !balance + amount)) else (counter := !counter +1;raise wrong_pass)); show_balance = (fun p -> if !counter > 4 then raise too_many_failures else if p = !password then (counter :=0;!balance) else (counter := !counter + 1;raise wrong_pass)); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc x -> let (a,b,c) = x in if a = vertex then (b,c)::acc else acc) [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec total_weight (result : ('a * weight) list): int = match result with | [] -> 0 | x::xs -> snd x + total_weight xs in let rec nodes_to_list (result : ('a * weight) list): 'a list = match result with | [] -> [] |(a,b)::xs -> a::nodes_to_list xs in let rec process_node (node: 'a * weight) (visited : ('a * weight) list): ('a list * weight)= if List.mem node visited then raise Fail else if fst node = b then (List.rev (nodes_to_list (node::visited)), total_weight (node::visited)) else let n = neighbours g (fst node) in if List.for_all (fun x -> not(List.mem x visited)) n then process_neighbours n (node::visited) else raise Fail and process_neighbours (nodes: ('a * weight) list) (visited: ('a * weight) list): ('a list * weight) = match nodes with |[] -> raise Fail |x::xs -> try process_node x visited with Fail -> process_neighbours xs visited in process_node (a,0) [];;