text
stringlengths
0
601k
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec compare_weights paths max = match paths with | [] -> Some max | p :: ps -> let (_, w1) = p in let (_, w2) = max in if w1 > w2 then compare_weights ps p else compare_weights ps max in if a = b then None else let paths = find_all_paths g a b in if paths = [] then None else compare_weights paths ([], 0) ;;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let bal = ref 0 in let mistypes = ref 0 in { update_pass = (fun (pswd:passwd) (newp:passwd) -> if (pswd = !pass) then (pass := newp; mistypes := 0) else (mistypes := !mistypes + 1; raise wrong_pass)); retrieve = (fun (pswd:passwd) (a:int) -> if (!mistypes > 4) then raise too_many_failures else (if (pswd == !pass) then (mistypes := 0; (if (a >= 0) then (if (a <= !bal) then (bal := !bal - a; mistypes := 0)else raise not_enough_balance) else raise negative_amount)) else (mistypes := !mistypes + 1; raise wrong_pass))); deposit = (fun pswd a -> if (!mistypes > 4) then raise too_many_failures else (if (pswd = !pass) then (mistypes := 0; (if (a>= 0) then (bal := !bal + a; mistypes := 0) else raise negative_amount)) else (mistypes := !mistypes + 1; raise wrong_pass))); show_balance = (fun(pswd) -> if (!mistypes > 4) then raise too_many_failures else (if (pswd = !pass) then (mistypes := 0 ; !bal) else (mistypes := !mistypes + 1; raise wrong_pass))); } ;; let rec notcontains (l: 'a list) (a: 'a) : bool = match l with |[]-> true |x::xs-> if(x=a) then false else notcontains xs a;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edges1 = (List.filter (fun (a: (string * string * weight)) -> match a with |(y,_,_) -> if (vertex=y) then true else false |_-> false) g.edges) in List.map (fun (e,b,c) -> (b,c)) edges1 ;; let rec find_neighbour nbrs b : int = match nbrs with x::xs -> match x with |(n,w) -> if (n=b) then w else find_neighbour xs b ;; let rec weightfinder g b acc : int = match b with |x::xs -> let nbrs = (neighbours g x) in match xs with |q::qs -> weightfinder g xs (acc + (find_neighbour nbrs q)) |x::[]-> acc |[] ->acc;;
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 nn = (let n = (match node with |(n,w) -> n) in (neighbours g n )) in match node with |(n,w)-> if (n=b) then (List.rev (n::visited),w) else (aux_list nn (n::visited)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |x::xs -> let (st,w) = x in if (not (notcontains visited st)) then aux_list xs (visited) else (try aux_node x (visited) with Fail -> match xs with |(q,w)::qs -> aux_node (q,w) (q::st::visited); aux_list qs (q::st::visited) |(q,w)::[] -> aux_node (q,w) (q::st::visited) |[]-> raise Fail) |x::[]-> let (st,w) = x in if ((notcontains visited st)) then aux_node x (visited) else raise Fail |[]-> raise Fail in match (aux_list (neighbours g a) [a]) with |(c,w) -> (c,(weightfinder g c 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 nn = (let n = (match node with |(n,w) -> n) in (neighbours g n )) in match node with |(n,w)-> if (n=b) then sc (List.rev (n::visited),w) else (aux_list nn (n::visited) fc sc) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |x::xs -> let (st,w) = x in if (not (notcontains visited st)) then aux_list xs (visited) fc sc else (try aux_node x (visited) fc sc with Fail -> match xs with |(q,w)::qs -> aux_node (q,w) (q::st::visited) fc sc; aux_list qs (q::st::visited) fc sc |(q,w)::[] -> aux_node (q,w) (q::st::visited) fc sc |[]-> fc()) |x::[] -> let (st,w) = x in if ((notcontains visited st)) then aux_node x (visited) fc sc else fc() |[]-> raise Fail in match aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun x -> x) with |(c,w) -> (c,(weightfinder g c 0));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let y = find_all_paths g a b in match y with |[] -> None |(l,w)::xs -> Some(l,w);;
let open_account (initial_pass: passwd) : bank_account = let amount = ref 0 in let wrongCounter = ref 0 in let pw = ref initial_pass in let update_pass password npw = if (password = !pw) then (pw := npw; wrongCounter := 0) else (wrongCounter := (!wrongCounter+1); raise wrong_pass) in let retrieve password retrAmount = if (!wrongCounter > 5) then (raise too_many_failures) else (if (password = !pw) then ( if retrAmount >= 0 then ( if (retrAmount <= !amount) then (amount := !amount - retrAmount) else (raise not_enough_balance)) else (raise (Msg("Money amount below 0")))) else (wrongCounter := (!wrongCounter+1); raise wrong_pass)) in let deposit password depAmount = if (!wrongCounter > 5) then (raise too_many_failures) else (if (password = !pw) then ( if depAmount >= 0 then (amount := !amount + depAmount) else (raise (Msg("Money amount below 0")))) else (wrongCounter := (!wrongCounter+1); raise wrong_pass)) in let show_balance password = if (!wrongCounter > 5) then (raise too_many_failures) else ( if (password = !pw) then (!amount) else (wrongCounter := (!wrongCounter+1); raise wrong_pass)) in { update_pass; deposit; show_balance; retrieve; } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (fun x -> let (x,y,z) = x in (y,z)) (List.filter (fun x -> let (v,x,y) = x in 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) (w:weight): ('a list * weight) = let (x,y) = node in if x = b then (((@) visited (x::[])), (y+w)) else (if (List.mem x visited) then (raise Fail) else (aux_list (neighbours g x) ((@) visited (x::[])) (y+w))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w:weight) : ('a list * weight) = match nodes with |h::l -> (try aux_node h visited w with Fail -> aux_list l visited w) |[] -> raise Fail in aux_node (a,0) [] 0;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) w fc sc : ('a list * weight)= let (x,y) = node in if x = b then ( sc ((@) visited (x::[])), (y+w)) else (if (List.mem x visited) then (fc ()) else (aux_list (neighbours g x) ((@) visited (x::[])) (y+w) (fc) (sc))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) w fc sc : ('a list * weight) = match nodes with |h::l -> (aux_node h visited w (fun () -> aux_list l visited w fc sc) (sc)) |[] -> fc () in aux_node (a,0) [] 0 (fun () -> raise Fail) (fun r -> r);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper (l: ('a list * weight) list) currMax currMaxW : ('a list * weight) = match l with |h::t -> (let (x,y) = h in if y > currMaxW then helper t h y else helper t currMax currMaxW ) |[] -> currMax in let result = find_all_paths g a b in if result = [] then None else (Some (helper (result) ([],0) 0));;
let open_account (initial_pass: passwd) : bank_account = let local_pass = ref initial_pass in let local_balance = ref 0 in let pass_counter = ref 0 in {update_pass = (fun attemptP newP -> if attemptP = !local_pass then (pass_counter := 0; local_pass := newP) else (pass_counter := !pass_counter + 1; raise wrong_pass)); retrieve = (fun attemptP amt -> if !pass_counter >= 5 then raise too_many_failures else begin(if attemptP = !local_pass then (pass_counter := 0; begin (if amt < 0 then raise negative_amount else if amt > !local_balance then raise not_enough_balance else let a = !local_balance - amt in local_balance := a) end) else (pass_counter := !pass_counter + 1; raise wrong_pass)) end); deposit = (fun attemptP amt -> if !pass_counter >= 5 then raise too_many_failures else begin (if attemptP = !local_pass then (pass_counter := 0; begin (if amt < 0 then raise negative_amount else let a = !local_balance + amt in local_balance := a) end) else (pass_counter := !pass_counter + 1; raise wrong_pass)) end); show_balance = (fun attemptP -> if !pass_counter >= 5 then raise too_many_failures else begin (if attemptP = !local_pass then (pass_counter := 0; !local_balance) else (pass_counter := !pass_counter + 1; raise wrong_pass)) end)} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (fun e -> let (a,c,w) = e in (c,w)) (List.filter (fun edge -> let (a,c,w) = edge in a = 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) (sum : int) : ('a list * weight) = let (v,w) = node in if v = b then (visited @ [v], sum+w) else if List.exists (fun n -> n = v) visited then raise Fail else aux_list (neighbours g v) (visited @ [v]) (sum+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (sum : int) : ('a list * weight) = match nodes with | [] -> raise Fail | (v,w)::t -> try aux_node (v,w) visited sum with Fail -> aux_list t visited sum 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) (sum: int) fc sc : ('a list * weight)= let (v,w) = node in if v = b then sc ((visited @ [v]), (sum+w)) else if List.exists (fun n -> n = v) visited then fc () else aux_list (neighbours g v) (visited @ [v]) (sum+w) fc (fun (prev, sum) -> sc (prev, sum)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (sum: int) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (v,w)::t -> aux_node (v,w) visited sum (fun () -> aux_list t visited sum fc sc) sc in aux_list (neighbours g a) [a] 0 (fun () -> raise Fail) (fun r -> r);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec longest paths max_path max_weight = match paths with | [] -> Some (max_path, max_weight) | (v,w)::t -> if w > max_weight then longest t v w else longest t max_path max_weight in let paths = find_all_paths g a b in match paths with | [] -> None | _ -> longest paths [] 0;;
let open_account (initial_pass: passwd) : bank_account = let pwReference = ref initial_pass in let numOfErrors = ref 0 in let balance = ref 0 in let passwdChecker inputPw = if !numOfErrors > 5 then raise too_many_failures else let storedPw = !pwReference in if inputPw = storedPw then numOfErrors := 0 else (incr numOfErrors; raise wrong_pass) in let update_pass oldPw newPw = passwdChecker oldPw; pwReference := newPw in let deposit inputPw amount = let depAmount = ref amount in let zeroRef = ref 0 in passwdChecker inputPw; if depAmount < zeroRef then raise negative_amount else balance := !balance + !depAmount in let retrieve inputPw amount = passwdChecker inputPw; let retAmount = ref amount in let zeroRef = ref 0 in if balance < retAmount then raise not_enough_balance else if retAmount < zeroRef then raise negative_amount else balance := !balance - !retAmount in let show_balance inputPw = passwdChecker inputPw; !balance in {update_pass; deposit; retrieve; show_balance} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edge l (b, c, w) = if b = vertex then (c, w) :: l else l in List.fold_left edge [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = if a = b then ([a], 0) else let rec aux_node node visited cumul : ('a list * weight) = match neighbours g node with | [] -> raise Fail | _ -> aux_list (neighbours g node) visited cumul and aux_list nodes visited cumul : ('a list * weight) = match nodes with | [] -> raise Fail | (aNode, w)::t -> (if aNode = b then ((visited @ [aNode]), (cumul + w)) else if List.mem aNode visited then aux_list t visited cumul else try aux_node aNode (visited @ [aNode]) (cumul + w) with Fail -> aux_list t visited cumul) in aux_node a [a] 0;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = if a = b then ([a], 0) else let rec aux_node node visited cumul fc sc : ('a list * weight)= match neighbours g node with | [] -> fc () | _ -> aux_list (neighbours g node) visited cumul fc sc and aux_list nodes visited cumul fc sc : ('a list * weight) = match nodes with | [] -> fc () | (aNode, w)::t -> (if aNode = b then sc ((visited @ [aNode]), (cumul + w)) else if List.mem aNode visited then aux_list t visited cumul fc sc else (aux_node aNode (visited @ [aNode]) (cumul + w) (fun () -> aux_list t visited cumul fc sc) sc)) in aux_node a [a] 0 (fun () -> raise Fail) (fun anotherNode -> anotherNode);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let find_max max aNode = let (_, wOne), (_, wTwo) = aNode, max in if wOne > wTwo then aNode else max in match find_all_paths g a b with | [] -> None | h::t -> Some (List.fold_left find_max h t);;
let open_account (initial_pass: passwd) : bank_account = let x : int ref = ref 0 in let p : passwd ref = ref initial_pass in let fails : int ref = ref 0 in { update_pass = (fun a b -> if (a= (!p)) then (p:=b) else (raise wrong_pass fails := !fails + 1) ); retrieve = (fun a b -> if (!fails>=6) then (raise too_many_failures) else if a<>(!p) then (fails := !fails + 1; raise wrong_pass;) else if (!x<b) then raise not_enough_balance else if (b<0) then raise negative_amount else (x:= (!x)-b)) ; deposit = (fun a b -> if (!fails>=6) then (raise too_many_failures) else if a<>(!p) then (fails := !fails + 1; raise wrong_pass;) else if (b<0) then raise negative_amount else (x:=!x+b)); show_balance= (fun a -> if (!fails>=6) then (raise too_many_failures) else if a<>(!p) then (fails := !fails + 1; raise wrong_pass;) else !x); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left ( fun acc edge -> let (v1, v2, w) = edge in 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 aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = if not (List.mem nodes visited) then begin b node; let s = neighbours g nodes in List.fold_left aux_list (nodes::visited) s end else visited in aux_node [] a 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) : ('a list * weight) list = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) list = 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 x : int ref = ref 0 in let p : passwd ref = ref initial_pass in let fails : int ref = ref 0 in { update_pass = (fun a b -> if (a= (!p)) then (p:=b) else (raise wrong_pass fails := !fails + 1) ); retrieve = (fun a b -> if (!fails>=6) then (raise too_many_failures) else if a<>(!p) then (fails := !fails + 1; raise wrong_pass;) else if (!x<b) then raise not_enough_balance else if (b<0) then raise negative_amount else (x:= (!x)-b)) ; deposit = (fun a b -> if (!fails>=6) then (raise too_many_failures) else if a<>(!p) then (fails := !fails + 1; raise wrong_pass;) else if (b<0) then raise negative_amount else (x:=!x+b)); show_balance= (fun a -> if (!fails>=6) then (raise too_many_failures) else if a<>(!p) then (fails := !fails + 1; raise wrong_pass;) else !x); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left ( fun acc edge -> let (v1, v2, w) = edge in 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 result = ref 0 in let rec succNode (neighbour: ('a * weight)list) (visited: 'a list) : ('a * weight) list = List.filter (fun (n, w) -> not (List.mem n visited)) neighbour in let rec finder (travelled_list: ('a *weight) list) (visited: 'a list) (this_node: 'a) (acc:int) : 'a list = if this_node = b then (result:=acc; [b]) else let visited = (List.append visited [this_node]) in match travelled_list with | [] -> raise Fail | (succ_node, w) :: tl -> let succ_list = succNode (neighbours g succ_node) visited in try this_node :: finder succ_list visited (succ_node) (acc+w) with Fail -> finder tl visited this_node acc in (let init = succNode (neighbours g a) [] in (let _ = finder init [] a 0 in finder init [] a 0, !result));;
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) list = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) list = 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 x : int ref = ref 0 in let p : passwd ref = ref initial_pass in let fails : int ref = ref 0 in { update_pass = (fun a b -> if (a= (!p)) then (p:=b) else (raise wrong_pass fails := !fails + 1) ); retrieve = (fun a b -> if (!fails>=6) then (raise too_many_failures) else if a<>(!p) then (fails := !fails + 1; raise wrong_pass;) else if (!x<b) then raise not_enough_balance else if (b<0) then raise negative_amount else (x:= (!x)-b)) ; deposit = (fun a b -> if (!fails>=6) then (raise too_many_failures) else if a<>(!p) then (fails := !fails + 1; raise wrong_pass;) else if (b<0) then raise negative_amount else (x:=!x+b)); show_balance= (fun a -> if (!fails>=6) then (raise too_many_failures) else if a<>(!p) then (fails := !fails + 1; raise wrong_pass;) else !x); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left ( fun acc edge -> let (v1, v2, w) = edge in 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 result = ref 0 in let rec succNode (neighbour: ('a * weight)list) (visited: 'a list) : ('a * weight) list = List.find_all (fun (n, w) -> not (List.mem n visited)) neighbour in let rec finder (travelled_list: ('a *weight) list) (visited: 'a list) (this_node: 'a) (acc:int) : 'a list = if this_node = b then (result:=acc; [b]) else let visited = (List.append visited [this_node]) in match travelled_list with | [] -> raise Fail | (succ_node, w) :: tl -> let succ_list = succNode (neighbours g succ_node) visited in try this_node :: finder succ_list visited (succ_node) (acc+w) with Fail -> finder tl visited this_node acc in (let init = succNode (neighbours g a) [] in (let _ = finder init [] a 0 in finder init [] a 0, !result));;
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) list = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) list = 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 x : int ref = ref 0 in let p : passwd ref = ref initial_pass in let fails : int ref = ref 0 in { update_pass = (fun a b -> if (a= (!p)) then (p:=b) else (raise wrong_pass fails := !fails + 1) ); retrieve = (fun a b -> if (!fails>=6) then (raise too_many_failures) else if a<>(!p) then (fails := !fails + 1; raise wrong_pass;) else if (!x<b) then raise not_enough_balance else if (b<0) then raise negative_amount else (x:= (!x)-b)) ; deposit = (fun a b -> if (!fails>=6) then (raise too_many_failures) else if a<>(!p) then (fails := !fails + 1; raise wrong_pass;) else if (b<0) then raise negative_amount else (x:=!x+b)); show_balance= (fun a -> if (!fails>=6) then (raise too_many_failures) else if a<>(!p) then (fails := !fails + 1; raise wrong_pass;) else !x); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left ( fun acc edge -> let (v1, v2, w) = edge in 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 result = ref 0 in let rec succNode (neighbour: ('a * weight)list) (visited: 'a list) : ('a * weight) list = List.find_all (fun (n, w) -> not (List.memq n visited)) neighbour in let rec finder (travelled_list: ('a *weight) list) (visited: 'a list) (this_node: 'a) (acc:int) : 'a list = if not(this_node = b) then let visited = (List.append visited [this_node]) in match travelled_list with | [] -> raise Fail | (succ_node, w) :: tl -> let succ_list = succNode (neighbours g succ_node) visited in try this_node :: finder succ_list visited (succ_node) (acc+w) with Fail -> finder tl visited this_node acc else (result:=acc; [b]) in (let init = succNode (neighbours g a) [] in (let _ = finder init [] a 0 in finder init [] a 0, !result)) ;;
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) list = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) list = 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 bal = ref 0 in let pass = ref initial_pass in let failures = ref 0 in { update_pass = ( fun oldpass newpass -> if oldpass = !pass then (pass:=newpass; failures:=0) else (failures:=(!failures+1); raise wrong_pass) ); retrieve = ( fun passwd amt -> if !failures = 5 then raise too_many_failures else if !pass <> passwd then (failures:=(!failures+1);raise wrong_pass) else ( failures:=0; match passwd with | _ when !bal >= amt && amt >= 0 -> bal := (!bal-amt) | _ when !bal >= amt -> raise negative_amount | _ -> raise not_enough_balance ) ); deposit = ( fun passwd amt -> if !failures = 5 then raise too_many_failures else if !pass <> passwd then (failures:=(!failures+1);raise wrong_pass) else ( failures:=0; match passwd with | _ when amt >= 0 -> bal := (!bal+amt) | _ -> raise negative_amount ) ); show_balance = ( fun passwd -> if !failures = 5 then raise too_many_failures else if !pass <> passwd then (failures:=(!failures+1);raise wrong_pass) else ( failures:=0; !bal ) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let add_to_list l (v1,v2,w) = if v1=vertex then (v2,w)::l else l in List.fold_left add_to_list [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let path=ref [a] in let w=ref 0 in let weights=ref [0] in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (cur,weight)=node in path:=!path@[cur]; w:=(!w+weight); weights:=weight::!weights; if cur=b then (!path,!w) else aux_list (neighbours g cur) (visited@[cur]) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = if a=b then ([a],0) else ( match nodes with | [] -> let temp=List.rev !path in let (x::ls) = temp in let (y::ls2) = !weights in path:=(List.rev ls); w:=!w-y; weights:=ls2; raise Fail | (node,w)::ls when List.mem node visited -> aux_list ls visited | (node,w)::ls -> try aux_node (node,w) (visited@[node]) with Fail -> aux_list ls visited ) in aux_list (neighbours g a) [a];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = if a=b then ([a],0) else ( let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc path cost: ('a list * weight)= let (cur,weight) = node in if List.mem cur visited then fc() else if cur=b then sc (path@[cur],cost+weight) else aux_list (neighbours g cur) (cur::visited) fc sc (path@[cur]) (cost+weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc path cost: ('a list * weight) = match nodes with | [] -> fc() | x::ls -> aux_node x visited (fun()->aux_list ls visited fc sc path cost) sc path cost in aux_list (neighbours g a) [a] (fun()->raise Fail) (fun r->r) [a] 0 );;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths=find_all_paths g a b in let max_cost=ref 0 in let rec helper list answer = match list with | [] -> answer | (path,cost)::ls when cost>(!max_cost) -> max_cost:=cost; helper ls (Some(path,cost)) | x::ls -> helper ls answer in helper paths None;;
let open_account (initial_pass: passwd) : bank_account = let currpw = ref initial_pass in let tries = ref 0 in let balance = ref 0 in { update_pass = (fun pw newpw -> match pw = !currpw with | true -> tries := 0; currpw := newpw | false -> tries := (!tries + 1); raise wrong_pass); retrieve = (fun pw amount -> if !tries >= 5 then raise too_many_failures else match pw = !currpw with | true -> tries := 0; if amount < 0 then raise negative_amount else if !balance >= amount then balance := (!balance - amount) else raise not_enough_balance | false -> tries := (!tries + 1); raise wrong_pass); deposit = (fun pw amount -> if !tries >= 5 then raise too_many_failures else match pw = !currpw with | true -> tries := 0; if amount < 0 then raise negative_amount else balance := (!balance + amount) | false -> tries := (!tries + 1); raise wrong_pass); show_balance = (fun pw -> if !tries >= 5 then raise too_many_failures else match pw = !currpw with | true -> tries := 0; !balance | false -> tries := (!tries + 1); raise wrong_pass); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let prune l (v1,v2,w) = if vertex = v1 then (v2,w) :: l else l in List.fold_left prune [] g.edges ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let tally l = let rec aux acc = function | [] -> acc | (v,w)::r -> let (v',w') = acc in aux (v::v',w+w') r in aux ([],0) l in let rec aux_node (node: 'a * weight) (visited : ('a * weight) list) : ('a list * weight) = let (v,_) = node in if v = b then tally visited else aux_list (neighbours g v) visited and aux_list (nodes: ('a * weight) list) (visited: ('a * weight) list) : ('a list * weight) = match nodes with | [] -> raise Fail | neigh1::neighs -> if List.mem neigh1 visited then raise Fail else try aux_node neigh1 (neigh1::visited) with Fail -> aux_list neighs visited in aux_list (neighbours g a) [(a,0)];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let tally l = let rec aux acc = function | [] -> acc | (v,w)::r -> let (v',w') = acc in aux (v::v',w+w') r in aux ([],0) l in let rec aux_node (node: 'a * weight) (visited : ('a * weight) list) : ('a list * weight) = let (v,_) = node in if v = b then tally visited else aux_list (neighbours g v) visited and aux_list (nodes: ('a * weight) list) (visited: ('a * weight) list) : ('a list * weight) = match nodes with | [] -> raise Fail | neigh1::neighs -> if List.mem neigh1 visited then raise Fail else try aux_node neigh1 (neigh1::visited) with Fail -> aux_list neighs visited in aux_list (neighbours g a) [(a,0)];;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();;
let open_account (initial_pass: passwd) : bank_account = let current_balance = ref 0 in let current_password = ref initial_pass in let counter = ref 0 in { update_pass = (fun old_pass -> fun new_pass -> if old_pass = !current_password then (current_password := new_pass; counter := 0) else (counter := !counter + 1; raise wrong_pass)); retrieve = (fun password -> fun amount -> if !counter >= 5 then raise too_many_failures else match amount with |_ when amount < 0 -> raise negative_amount |_ -> if !current_balance - amount < 0 then raise not_enough_balance else if (password = !current_password) then (current_balance := !current_balance - amount; counter := 0) else (counter := !counter + 1; raise wrong_pass)); deposit = (fun password -> fun amount -> if !counter >= 5 then raise too_many_failures else if amount < 0 then raise negative_amount else if password = !current_password then (current_balance := !current_balance + amount; counter := 0) else (counter := !counter + 1; raise wrong_pass)); show_balance = (fun password -> if !counter >= 5 then raise too_many_failures else if password = !current_password then (counter := 0; !current_balance) else (counter := !counter + 1; raise wrong_pass)); } let rec get_out_nodes node list acc = match list with | [] -> acc | (v1,v2,w) :: tl -> if v1 = node then get_out_nodes node tl ((v2,w) :: acc) else get_out_nodes node tl acc;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = get_out_nodes vertex g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node,w) visited = if node = b then ([b], w) else if not (List.mem node visited) then let (p,c) = aux_list (neighbours g node) (node :: visited) in (node :: p, c + w) else raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail |(x,y)::xs -> try aux_node (x,y) 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,w) (visited : 'a list) fc sc : ('a list * weight)= if node = b then sc ([b], w) else if not (List.mem node visited) then aux_list (neighbours g node) (node :: visited) fc (fun (n, weight) -> sc ((node :: n), (weight + w))) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () |(x,y)::xs -> aux_node (x,y) visited (fun () -> aux_list xs visited fc sc) 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_paths = find_all_paths g a b in match all_paths with |[] -> None |x::xs -> Some (highest_weight x xs);;
let open_account (initial_pass: passwd) : bank_account = let counter = ref 0 in let curr_pass = ref initial_pass in let amt_curr = ref 0 in let check_failure () = if !counter >= 5 then raise too_many_failures in let check_right past_pass = if !curr_pass = past_pass then (counter := 0) else (counter := !counter + 1; raise wrong_pass) in { update_pass = (fun (past_pass) (new_pass) -> if !curr_pass = past_pass then (counter :=0; curr_pass := new_pass) else (counter := !counter + 1; raise wrong_pass) ); retrieve = ( fun (past_pass) (amt_in) -> check_failure (); check_right past_pass; if amt_in < 0 then raise negative_amount else if amt_in > !amt_curr then raise not_enough_balance else (counter :=0; amt_curr := !amt_curr - amt_in) ); deposit = (fun (past_pass) (amt_in) -> check_failure (); check_right past_pass; if amt_in < 0 then raise negative_amount else (counter :=0; amt_curr := !amt_curr + amt_in) ); show_balance = (fun (past_pass) -> check_failure (); check_right past_pass; (counter :=0; !amt_curr) ); } ;;
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,w) (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, cost + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (v,w) :: vs -> try aux_node (v,w) visited with Fail -> aux_list vs 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) (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, cost + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | v :: vs -> aux_node v visited (fun () -> aux_list vs visited fc sc) sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun l -> l);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max l = match l with | [] -> None | [(p,w)] -> Some (p,w) | (p1,w1)::(p2,w2)::t -> if w1 >= w2 then max ((p1,w1)::t) else max ((p2,w2)::t) in max(find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let counter = ref 0 in let curr_pass = ref initial_pass in let amt_curr = ref 0 in let check_failure () = if !counter >= 5 then raise too_many_failures in let check_right past_pass = if !curr_pass = past_pass then (counter := 0) else (counter := !counter + 1; raise wrong_pass) in { update_pass = (fun (past_pass) (new_pass) -> if !curr_pass = past_pass then (counter :=0; curr_pass := new_pass) else (counter := !counter + 1; raise wrong_pass) ); retrieve = ( fun (past_pass) (amt_in) -> check_failure (); check_right past_pass; if amt_in < 0 then raise negative_amount else if amt_in > !amt_curr then raise not_enough_balance else (counter :=0; amt_curr := !amt_curr - amt_in) ); deposit = (fun (past_pass) (amt_in) -> check_failure (); check_right past_pass; if amt_in < 0 then raise negative_amount else (counter :=0; amt_curr := !amt_curr + amt_in) ); show_balance = (fun (past_pass) -> check_failure (); check_right past_pass; (counter :=0; !amt_curr) ); } ;;
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,w) (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, cost + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (v,w) :: vs -> try aux_node (v,w) visited with Fail -> aux_list vs 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) (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, cost + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | v :: vs -> aux_node v visited (fun () -> aux_list vs visited fc sc) sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun l -> l);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max l = match l with | [] -> None | [(p,w)] -> Some (p,w) | (p1,w1)::(p2,w2)::t -> if w1 >= w2 then max ((p1,w1)::t) else max ((p2,w2)::t) in max(find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let counter = ref 0 in let balance = ref 0 in let pasword = ref initial_pass in {update_pass = (fun oldpass newpass -> if !pasword = oldpass then (pasword := newpass; counter := 0) else (counter := !counter + 1; raise wrong_pass)) ; retrieve = (fun pass amount -> if !counter > 4 then raise too_many_failures else if !pasword = pass then (if amount < 0 then (counter := 0; raise negative_amount) else if !balance < amount then (counter := 0; raise not_enough_balance) else (balance := !balance - amount; counter := 0)) else (counter := !counter + 1; raise wrong_pass)) ; deposit = (fun pass amount -> if !counter > 4 then raise too_many_failures else if !pasword = pass then (if amount < 0 then (counter := 0; raise negative_amount) else (balance := !balance + amount; counter := 0)) else (counter := !counter + 1; raise wrong_pass)); show_balance = (fun pass -> if !counter > 4 then raise too_many_failures else if !pasword = pass then (counter := 0; !balance) else (counter := !counter + 1; raise wrong_pass)) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (function (vertex, a, weight) -> (a,weight)) (List.filter (function (a, _,_) -> a = 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) (total:int) : ('a list * weight)= match (neighbours g (fst(node))) with |[] -> raise Fail | hd::tl -> if List.mem (fst(hd)) visited then aux_list tl visited total else if b=fst(hd) then (List.rev ((fst(hd))::visited), total + snd(hd)) else try aux_list (neighbours g (fst(hd))) (fst(hd)::visited) (total + snd(hd)) with Fail -> aux_list tl visited total and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total:int) : ('a list * weight) = match nodes with |[] -> raise Fail |hd :: tl -> if List.mem (fst(hd)) visited then aux_list tl visited total else if b = fst(hd) then (List.rev((fst(hd))::visited), total + snd(hd)) else try aux_node hd (fst(hd)::visited) (total + snd(hd)) with Fail -> aux_list tl 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) (total:int) fc sc: ('a list * weight)= match (neighbours g (fst(node))) with |[] -> fc () | hd::tl -> if List.mem (fst(hd)) visited then fc () else if b=fst(hd) then sc hd visited total else aux_list (neighbours g (fst(hd))) (fst(hd)::visited) (total + snd(hd)) (fun () -> aux_list tl visited total fc sc) sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total:int) fc sc: ('a list * weight) = match nodes with |[] -> fc () |hd :: tl -> if List.mem (fst(hd)) visited then fc () else if b = fst(hd) then sc hd visited total else aux_node hd (fst(hd)::visited) (total + snd(hd)) (fun () -> aux_list tl visited total fc sc) sc in aux_list (neighbours g a) [a] 0 (fun () -> raise Fail ) (fun k visited total -> (List.rev((fst(k))::visited),total + snd(k)) );;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();;
let open_account (initial_pass: passwd) : bank_account = let counter = ref 0 in let balance = ref 0 in let pasword = ref initial_pass in {update_pass = (fun oldpass newpass -> if !pasword = oldpass then (pasword := newpass; counter := 0) else (counter := !counter + 1; raise wrong_pass)) ; retrieve = (fun pass amount -> if !counter > 4 then raise too_many_failures else if !pasword = pass then (if amount < 0 then (counter := 0; raise negative_amount) else if !balance < amount then (counter := 0; raise not_enough_balance) else (balance := !balance - amount; counter := 0)) else (counter := !counter + 1; raise wrong_pass)) ; deposit = (fun pass amount -> if !counter > 4 then raise too_many_failures else if !pasword = pass then (if amount < 0 then (counter := 0; raise negative_amount) else (balance := !balance + amount; counter := 0)) else (counter := !counter + 1; raise wrong_pass)); show_balance = (fun pass -> if !counter > 4 then raise too_many_failures else if !pasword = pass then (counter := 0; !balance) else (counter := !counter + 1; raise wrong_pass)) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (function (vertex, a, weight) -> (a,weight)) (List.filter (function (a, _,_) -> a = 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) (total:int) : ('a list * weight)= match (neighbours g (fst(node))) with |[] -> raise Fail | hd::tl -> if List.mem (fst(hd)) visited then aux_list tl visited total else if b=fst(hd) then (List.rev ((fst(hd))::visited), total + snd(hd)) else try aux_list (neighbours g (fst(hd))) (fst(hd)::visited) (total + snd(hd)) with Fail -> aux_list tl visited total and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total:int) : ('a list * weight) = match nodes with |[] -> raise Fail |hd :: tl -> if List.mem (fst(hd)) visited then aux_list tl visited total else if b = fst(hd) then (List.rev((fst(hd))::visited), total + snd(hd)) else try aux_node hd (fst(hd)::visited) (total + snd(hd)) with Fail -> aux_list tl 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) (total:int) fc sc: ('a list * weight)= match (neighbours g (fst(node))) with |[] -> fc () | hd::tl -> if List.mem (fst(hd)) visited then fc () else if b=fst(hd) then sc hd visited total else aux_list (neighbours g (fst(hd))) (fst(hd)::visited) (total + snd(hd)) (fun () -> aux_list tl visited total fc sc) sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total:int) fc sc: ('a list * weight) = match nodes with |[] -> fc () |hd :: tl -> if List.mem (fst(hd)) visited then fc () else if b = fst(hd) then sc hd visited total else aux_node hd (fst(hd)::visited) (total + snd(hd)) (fun () -> aux_list tl visited total fc sc) sc in aux_list (neighbours g a) [a] 0 (fun () -> raise Fail ) (fun k visited total -> (List.rev((fst(k))::visited),total + snd(k)) );;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented();;
let open_account (initial_pass: passwd) : bank_account = let counter = ref 0 in let balance = ref 0 in let pasword = ref initial_pass in {update_pass = (fun oldpass newpass -> if !pasword = oldpass then (pasword := newpass; counter := 0) else (counter := !counter + 1; raise wrong_pass)) ; retrieve = (fun pass amount -> if !counter > 4 then raise too_many_failures else if !pasword = pass then (if amount < 0 then (counter := 0; raise negative_amount) else if !balance < amount then (counter := 0; raise not_enough_balance) else (balance := !balance - amount; counter := 0)) else (counter := !counter + 1; raise wrong_pass)) ; deposit = (fun pass amount -> if !counter > 4 then raise too_many_failures else if !pasword = pass then (if amount < 0 then (counter := 0; raise negative_amount) else (balance := !balance + amount; counter := 0)) else (counter := !counter + 1; raise wrong_pass)); show_balance = (fun pass -> if !counter > 4 then raise too_many_failures else if !pasword = pass then (counter := 0; !balance) else (counter := !counter + 1; raise wrong_pass)) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (function (vertex, a, weight) -> (a,weight)) (List.filter (function (a, _,_) -> a = 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) (total:int) : ('a list * weight)= match (neighbours g (fst(node))) with |[] -> raise Fail | hd::tl -> if List.mem (fst(hd)) visited then aux_list tl visited total else if b=fst(hd) then (List.rev ((fst(hd))::visited), total + snd(hd)) else try aux_list (neighbours g (fst(hd))) (fst(hd)::visited) (total + snd(hd)) with Fail -> aux_list tl visited total and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total:int) : ('a list * weight) = match nodes with |[] -> raise Fail |hd :: tl -> if List.mem (fst(hd)) visited then aux_list tl visited total else if b = fst(hd) then (List.rev((fst(hd))::visited), total + snd(hd)) else try aux_node hd (fst(hd)::visited) (total + snd(hd)) with Fail -> aux_list tl 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) (total:int) fc sc: ('a list * weight)= match (neighbours g (fst(node))) with |[] -> fc () | hd::tl -> if List.mem (fst(hd)) visited then fc () else if b=fst(hd) then sc hd visited total else aux_list (neighbours g (fst(hd))) (fst(hd)::visited) (total + snd(hd)) (fun () -> aux_list tl visited total fc sc) sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total:int) fc sc: ('a list * weight) = match nodes with |[] -> fc () |hd :: tl -> if List.mem (fst(hd)) visited then fc () else if b = fst(hd) then sc hd visited total else aux_node hd (fst(hd)::visited) (total + snd(hd)) (fun () -> aux_list tl visited total fc sc) sc in aux_list (neighbours g a) [a] 0 (fun () -> raise Fail ) (fun k visited total -> (List.rev((fst(k))::visited),total + snd(k)) );;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match (find_all_paths g a b) with |[] -> None |hd::[]-> Some hd |hd :: tl ->Some (List.fold_right (fun (a,b) (c,d) -> if d >= b then (c,d) else (a,b)) (find_all_paths g a b) (hd));;
let open_account (initial_pass: passwd) : bank_account = let counter = ref 0 in let balance = ref 0 in let pasword = ref initial_pass in {update_pass = (fun oldpass newpass -> if !pasword = oldpass then (pasword := newpass; counter := 0) else (counter := !counter + 1; raise wrong_pass)) ; retrieve = (fun pass amount -> if !counter > 4 then raise too_many_failures else if !pasword = pass then (if amount < 0 then (counter := 0; raise negative_amount) else if !balance < amount then (counter := 0; raise not_enough_balance) else (balance := !balance - amount; counter := 0)) else (counter := !counter + 1; raise wrong_pass)) ; deposit = (fun pass amount -> if !counter > 4 then raise too_many_failures else if !pasword = pass then (if amount < 0 then (counter := 0; raise negative_amount) else (balance := !balance + amount; counter := 0)) else (counter := !counter + 1; raise wrong_pass)); show_balance = (fun pass -> if !counter > 4 then raise too_many_failures else if !pasword = pass then (counter := 0; !balance) else (counter := !counter + 1; raise wrong_pass)) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (function (vertex, a, weight) -> (a,weight)) (List.filter (function (a, _,_) -> a = 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) (total:int) : ('a list * weight)= match (neighbours g (fst(node))) with |[] -> raise Fail | hd::tl -> if List.mem (fst(hd)) visited then aux_list tl visited total else if b=fst(hd) then (List.rev ((fst(hd))::visited), total + snd(hd)) else try aux_list (neighbours g (fst(hd))) (fst(hd)::visited) (total + snd(hd)) with Fail -> aux_list tl visited total and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total:int) : ('a list * weight) = match nodes with |[] -> raise Fail |hd :: tl -> if List.mem (fst(hd)) visited then aux_list tl visited total else if b = fst(hd) then (List.rev((fst(hd))::visited), total + snd(hd)) else try aux_node hd (fst(hd)::visited) (total + snd(hd)) with Fail -> aux_list tl 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) (total:int) fc sc: ('a list * weight)= match (neighbours g (fst(node))) with |[] -> fc () | hd::tl -> if List.mem (fst(hd)) visited then aux_list tl visited total fc sc else if b=fst(hd) then sc hd visited total else aux_list (neighbours g (fst(hd))) (fst(hd)::visited) (total + snd(hd)) (fun () -> aux_list tl visited total fc sc) sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total:int) fc sc: ('a list * weight) = match nodes with |[] -> fc () |hd :: tl -> if List.mem (fst(hd)) visited then aux_list tl visited total fc sc else if b = fst(hd) then sc hd visited total else aux_node hd (fst(hd)::visited) (total + snd(hd)) (fun () -> aux_list tl visited total fc sc) sc in aux_list (neighbours g a) [a] 0 (fun () -> raise Fail ) (fun k visited total -> (List.rev((fst(k))::visited),total + snd(k)) );;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match (find_all_paths g a b) with |[] -> None |hd::[]-> Some hd |hd :: tl ->Some (List.fold_right (fun (a,b) (c,d) -> if d >= b then (c,d) else (a,b)) (find_all_paths g a b) (hd));;
let open_account (initial_pass: passwd) : bank_account = let cur_pass = ref initial_pass in let balance = ref 0 in let count = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> if !cur_pass = old_pass then begin count := 0; cur_pass := new_pass end else begin count := !count + 1; raise wrong_pass end ); retrieve = (fun (pass: passwd) (amount: int) -> if !count < 5 then if !cur_pass = pass then begin count := 0; if amount <= !balance then if amount >= 0 then balance := !balance - amount else raise negative_amount else raise not_enough_balance end else begin count := !count + 1; raise wrong_pass end else raise too_many_failures ); deposit =(fun (pass: passwd) (amount: int) -> if !count < 5 then if !cur_pass = pass then begin count := 0; if amount >= 0 then balance := !balance + amount else raise negative_amount end else begin count := !count + 1; raise wrong_pass end else raise too_many_failures); show_balance =(fun (pass: passwd) -> if !count < 5 then if !cur_pass = pass then begin count := 0; !balance end else begin count := !count + 1; raise wrong_pass end else raise too_many_failures); } ;; let rem_vert ((_,b,c):('a*'a*weight)):('a*weight) = (b,c);; let out_vert_comp vert (a,_,_) = vert = a;; let mem_check (a,_) = a;; let weight_add (a:weight) ((b,c):'a*weight) = (b,a+c);; let get_weight (_,a) = a;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (rem_vert) (List.filter (fun x -> out_vert_comp vertex x) (g.edges));;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (x,y) = node in if List.mem x visited then raise Fail else if x = b then (visited@[x], (y)) else aux_list (List.map (weight_add y) (neighbours g x)) (visited@[x]) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (x,y)::t -> try aux_node (x,y) (visited) with Fail -> 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 notimplemented ();;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = Some (find_path 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 let lock_account pa = if pa = !password && !counter < 5 then counter :=0 else if !counter >= 5 then raise too_many_failures else if (pa <> !password) then ( if (!counter < 5) then (counter:= !counter + 1; raise wrong_pass) else if (!counter >=5) then (counter:= !counter+1; raise too_many_failures) else counter := !counter + 1; raise wrong_pass;) in let update_pass (old_password:passwd) (new_password:passwd) = if old_password <> !password then (counter := !counter+1 ; raise wrong_pass) else (counter:=0; password := new_password;) in let retrieve (check_password:passwd) (money_retrieve: int) = lock_account check_password; if money_retrieve < 0 then raise negative_amount else if check_password = !password then counter :=0; if !balance-money_retrieve < 0 then raise not_enough_balance else balance := !balance - money_retrieve in let deposit (c:passwd) (money_deposit : int) = lock_account c; if money_deposit < 0 then raise negative_amount else (balance := money_deposit + !balance; counter :=0 ;) in let show_balance (pass : passwd) = lock_account pass; (!balance) in {update_pass; retrieve; deposit; show_balance} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec newform edge acc = match edge with |[] -> acc |x::xs -> let (a,l,m) = x in if a = vertex then newform xs ((l,m)::acc) else newform xs (acc) in newform (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) = notimplemented() and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a * weight) list = 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 stored_pass = ref initial_pass in let balance = ref 0 in let error_counter = ref 0 in let validation pass = if !error_counter = 5 then raise too_many_failures else if pass = !stored_pass then error_counter := 0 else( error_counter := !error_counter +1; raise wrong_pass ) in { update_pass= (fun old_pass new_pass -> if old_pass = !stored_pass then (error_counter := 0; stored_pass := new_pass) else (error_counter:= !error_counter +1; raise wrong_pass) ); deposit= (fun pass amount -> validation pass; if amount <0 then raise negative_amount else balance := !balance + amount ); retrieve= (fun pass amount -> validation pass; if amount <0 then raise negative_amount else if !balance < amount then raise not_enough_balance else ( balance := !balance - amount ) ); show_balance = ( fun pass -> validation pass; !balance ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun ls edge -> let (n1,n2,w) = edge in if n1 = vertex then (n2, w)::ls else ls ) [] 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 (curr_node, w) = node in if List.exists (fun n -> n = curr_node) visited then raise Fail else if curr_node = b then ( visited@[curr_node], 0) else aux_list (neighbours g curr_node) (visited @[curr_node]) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | head :: tail -> let value,weight_w = head in (try let path,w1 = aux_node head visited in (path,weight_w + w1) with Fail -> aux_list tail visited ) | [] -> raise Fail 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)= let (curr_node, w) = node in if List.exists (fun n -> n = curr_node) visited then fc () else if curr_node = b then sc ([curr_node], w) else aux_list (neighbours g curr_node) (visited @[curr_node]) fc (fun (path, cost) -> sc ( curr_node:: path, cost + w) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | head :: tail -> let fail = fun () -> aux_list tail visited fc sc in aux_node head visited fail sc in aux_node (a,0) [] (fun () -> raise Fail) (fun x -> x);;