text
stringlengths
0
601k
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edges = g.edges in List.fold_left (fun lst (v1, v2, w) -> if v1 = vertex then (v2, w) :: lst else lst) [] edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((vertex, w): 'a * weight) (visited : 'a list) : ('a list * weight) = if vertex = b then ([vertex], w) else let nears = neighbours g vertex in let news = List.filter (fun (a, _) -> not (List.mem a visited)) nears in let (lst, w') = aux_list news (vertex :: visited) in (vertex :: lst, w + w') and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with [] -> raise Fail | (v, w)::nodes -> try aux_node (v,w) (v::visited) with Fail -> aux_list nodes visited in aux_node (a,0) [a];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((v, w): 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if v = b then sc ([v], w) else let nears = neighbours g v in let news = List.filter (fun (a, _) -> not (List.mem a visited)) nears in aux_list news (v :: visited) fc (fun (lst, w') -> sc (v :: lst, w + w')) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with [] -> fc () | (v, w) :: nodes -> aux_node (v,w) visited (fun () -> aux_list nodes visited fc sc) (fun (lst, w') -> sc (lst, w')) in aux_node (a, 0) [a] (fun () -> raise Fail) (fun v -> v);;
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 (vs,w) (vs', w') -> if w > w' then (vs,w) else (vs', w')) x xs);;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let money= ref 0 in let counter = ref 0 in let result ={ update_pass = (fun pas x2-> if pas = !password then(counter := 0; password := x2) else ( counter :=!counter +1; raise wrong_pass) ) ; retrieve = (fun pas i -> if !counter >= 5 then raise too_many_failures else if pas != !password then ( counter := !counter+1; raise wrong_pass ) else if i < 0 then raise negative_amount else if !money-i<0 then raise not_enough_balance else (counter:=0 ;money:= !money-i) ) ; deposit = (fun pas i -> if !counter>= 5 then raise too_many_failures else if pas != !password then (counter:=!counter+1; raise wrong_pass) else if i<0 then raise negative_amount else (counter := 0; money := !money+i) ) ; show_balance = (fun pas -> if !counter>=5 then raise too_many_failures else if pas != !password then (counter:= !counter+1; raise wrong_pass) else ( counter := 0; !money) ) ; } in result ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let compare (v1,v2,w) v acc = if v1=v then acc @ [(v2,w)] else acc in let rec loop (e: ('a * 'a * weight)list ) (vertex: 'a) acc = match e with |[]->acc |x::xs->loop xs vertex (compare x vertex acc) in let result = loop g.edges vertex [] in result;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) acc : ('a list * weight) = match node with |(x,w) when x = b -> (visited @ [x], acc+w) |(x,w)-> if List.mem x visited then raise Fail else aux_list (neighbours g x ) (visited @ [x]) (acc+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) acc : ('a list * weight) = match nodes with |[]->raise Fail |x::xs-> try aux_node x visited acc with Fail -> aux_list xs visited acc in let nodes = neighbours g a in let result= aux_list nodes [a] 0 in result;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= match node with |(x,w) when x = b -> (visited @ [x],(sc w)) |(x,w) -> if List.mem x visited then fc () else aux_list (neighbours g x) (visited @ [x]) fc (fun x-> (sc x) + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[]->fc () |x::xs-> let sc2 = sc in let fc2 = fun ()-> aux_list xs visited fc sc in aux_node x visited fc2 sc2 in let sc = fun x-> x in let fc = fun ()-> raise Fail in aux_list (neighbours g a) [a] fc sc;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let l = find_all_paths g a b in let rec get_max (l:('a list * weight) list) result acc = match l with |[]-> (result, acc) |x::xs -> if snd(x) > acc then get_max xs (fst(x)) (snd(x)) else get_max xs result acc in let result = get_max l [] 0 in match result with |([],0)->None |(_,_)->Some result;;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let status = ref 0 in { update_pass = (fun old_p new_p -> (if old_p = !pass then (pass := new_p; status :=0) else (status := !status + 1; raise wrong_pass))); retrieve = (fun p r -> (if !status > 4 then raise too_many_failures else if p = !pass then (status := 0; (if r < 0 then raise negative_amount else if r > !balance then raise not_enough_balance else balance := !balance - r)) else (status := !status +1; raise wrong_pass))); deposit = (fun p r -> (if !status > 4 then raise too_many_failures else if p = !pass then (status := 0; if r < 0 then raise negative_amount else balance := !balance + r) else (status := !status +1; raise wrong_pass))); show_balance = (fun p ->(if !status > 4 then raise too_many_failures else if p = !pass then (status :=0; !balance) else (status := !status +1; raise wrong_pass))) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let helper l (a, b, c) = if a = vertex then (b, c) :: l else l in List.fold_left helper [] 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) (weightacc : weight): ('a list * weight) = match node with | (a, w) when (List.mem a visited) -> raise Fail | (a, w) when a = b -> (visited@[a], w + weightacc) | (a, w) -> aux_list (neighbours g a) (visited@[a]) (w + weightacc) and aux_list (nodes: ('a * weight) list) (visited: 'a list)(weightacc : weight): ('a list * weight) = match nodes with | [] -> raise Fail | x :: xs -> try aux_node x visited weightacc with Fail -> aux_list xs visited weightacc in aux_list (neighbours g a) [a] 0 ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= match node with | (n, w) when (List.mem n visited) -> fc() | (n, w) when n = b -> sc ([n], w) | (n, w) -> aux_list (neighbours g n) (n::visited) fc (fun (a, b) -> sc (n :: a, w + b)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc() | x :: xs -> let fc2 = (fun () -> aux_list xs visited fc sc) in aux_node x visited fc2 sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun (c, d) -> (c, d)) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper l node lstweight = match l with | [] -> (match lstweight with | 0 -> None | _ -> Some node) | (n, w)::xs -> if w > lstweight then helper xs (n, w) w else helper xs node lstweight in helper (find_all_paths g a b) ([], 0) 0 ;;
let open_account (initial_pass: passwd) : bank_account = let fails = ref 0 in let the_password = ref initial_pass in let balance = ref 0 in { update_pass = (function password -> if password = !the_password then fun pass -> (the_password := pass; fails := 0) else (fails := !fails +1;raise wrong_pass)); retrieve = (function password -> if !fails >= 5 then raise too_many_failures else( if password <> !the_password then (fails := !fails +1; raise wrong_pass) else (fails := 0;fun amount -> if amount <0 then raise negative_amount else if amount > !balance then raise not_enough_balance else balance := !balance - amount )) ); deposit= (function password -> if !fails >= 5 then raise too_many_failures else( if password <> !the_password then (fails := !fails +1;raise wrong_pass) else (fails := 0;fun deposit_amount -> if deposit_amount <0 then raise negative_amount else balance := !balance + deposit_amount))) ; show_balance= (function password -> if !fails >= 5 then raise too_many_failures else( if password <> !the_password then (fails := !fails +1;raise wrong_pass) else (fails := 0;!balance) ) ); } ;; let g4 = {nodes = ["green"; "black"; "blue"; "red"; "orange"; "purple";"yellow" ; "pink"; "brown"]; edges = [("black", "blue", 3); ("pink", "blue", 100); ("red", "orange", 1); ("green", "purple", 4); ("blue", "pink", 7); ("blue", "brown", 10)]};;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let v1 = vertex in let f (edge: ('a * 'a * weight)) (li: (string * weight) list): (string * weight) list= let (x,y,z) = edge in if x = v1 then (y,z) :: li else li in List.fold_right f g.edges [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (total_w: int) : ('a list * weight) = let (v,w) = node in if List.mem v visited then raise Fail else(;;
let neighbours = neighbours g v in if v=b then (List.rev (v::visited), total_w + w) else (aux_list neighbours (v::visited) (total_w + w)) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total_w: int) : ('a list * weight) = match nodes with |x ::xs -> ( try aux_node x visited total_w with fail -> aux_list xs visited total_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) (total_w: int) fc sc : ('a list * weight)= let (v,w) = node in if List.mem v visited then fc () else(;;
let neighbours = neighbours g v in if v=b then sc [v] w else( let sc2 = fun p q -> sc (v::p) (w+q) in aux_list neighbours (v::visited) (total_w + w) fc sc2 ) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total_w: int) fc sc : ('a list * weight) = match nodes with |x ::xs -> ( let (v,w) = x in let fail3 = fun () -> aux_list xs visited total_w fc sc in aux_node x visited total_w fail3 sc ) |[] -> fc() in aux_node (a,0) [] 0 (fun () -> raise Fail) (fun p q -> (p,q)) ;; exception Fail2;;
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) (total_w: int) (biglist : ('a list * weight) list ): ('a list * weight) list = let (v,w) = node in if List.mem v visited then raise Fail else(;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec find_longest (paths:('a list * weight) list) (longest_path:('a list * weight)): ('a list * weight) = match paths with |x::xs -> ( let (p,w) = x in let (p',w') = longest_path in if w > w' then find_longest xs x else find_longest xs longest_path ) |[] -> longest_path in let paths = find_all_paths g a b in match paths with |[] -> None |p :: ps -> Some (find_longest paths p);;
let open_account (initial_pass: passwd) : bank_account = let current_pass = ref initial_pass in let current_balance = ref 0 in let failure_count = ref 0 in { update_pass = (fun (old_pass) (new_pass) -> if String.equal old_pass !current_pass then (current_pass := new_pass ; failure_count := 0 ) else (failure_count := !failure_count + 1; raise wrong_pass ) ); retrieve = (fun (password) (money_wd) -> if !failure_count >= 5 then raise too_many_failures else begin if String.equal password !current_pass then (failure_count := 0; if !current_balance < money_wd then raise not_enough_balance else if money_wd < 0 then raise negative_amount else current_balance := !current_balance - money_wd ) else (failure_count := !failure_count + 1; raise wrong_pass ) end ); deposit = (fun (password) (money_deposit) -> if !failure_count >= 5 then raise too_many_failures else begin if String.equal password !current_pass then (failure_count := 0; if money_deposit < 0 then raise negative_amount else current_balance := !current_balance + money_deposit ) else (failure_count := !failure_count + 1; raise wrong_pass ) end ); show_balance = (fun (password) -> if !failure_count >= 5 then raise too_many_failures else begin if String.equal password !current_pass then (failure_count := 0 ; !current_balance ) else (failure_count := !failure_count + 1; raise wrong_pass ) end ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = notimplemented ();;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if (...) then aux_node (....) else(...) then aux_list (...) 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 cur_pass = ref initial_pass in let balance: int ref = ref 0 in let num_try: int ref = ref 0 in let check_pass = fun pass -> if !num_try >= 5 then raise too_many_failures else if pass = !cur_pass then num_try := 0 else (num_try := 1 + !num_try; raise wrong_pass) in { update_pass = (fun old_pass new_pass -> if old_pass <> !cur_pass then (num_try := 1 + !num_try; raise wrong_pass) else num_try := 0; cur_pass := new_pass); retrieve = (fun pass amount -> check_pass pass; if amount > !balance then raise not_enough_balance else if amount < 0 then raise negative_amount else balance := !balance - amount ); deposit = (fun pass amount -> check_pass pass; if amount < 0 then raise negative_amount else balance := !balance + amount); show_balance = fun pass -> (check_pass pass; !balance); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f (o, n, w) v = if o = vertex then (n, w) :: v else v in List.fold_right f 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 ([node], w) else if List.mem node visited then ([], 0) else match aux_list (neighbours g node) (node :: visited) with | ([], _) -> ([], 0) | (path, ww) -> (node :: path, w + ww) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> ([], 0) | (node :: rest) -> match aux_node node visited with | ([], _) -> aux_list rest visited | res -> res in match aux_node (a, 0) [] with | ([], _) -> raise Fail | res -> res;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((n, w): 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if n = b then sc ([n], w) else if List.mem n visited then fc () else aux_list (neighbours g n) (n :: visited) fc (fun (path, ww) -> sc (n :: path, w + ww)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (node :: rest) -> aux_node node visited (fun _ -> aux_list rest visited fc sc) sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun res -> res);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let f = fun res (path, w) -> match res with | None -> Some (path, w) | Some (path1, w1) -> Some (if w > w1 then (path, w) else (path1, w1)) in List.fold_left f None (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let accountPasswd = ref initial_pass in let accountBalance = ref 0 in let counter = ref 0 in { update_pass = (fun pass newPasswd -> if pass = !accountPasswd then (accountPasswd := newPasswd; counter := 0) else (counter := !counter + 1; raise wrong_pass) ); retrieve = (fun pass amount -> if !counter < 5 then if pass = !accountPasswd then match amount with | x when x < 0 -> raise negative_amount | y when y > !accountBalance -> counter := 0; raise not_enough_balance | _ -> accountBalance := !accountBalance - amount; counter := 0 else (counter := !counter + 1; raise wrong_pass) else raise too_many_failures ); deposit = (fun pass amount -> if !counter < 5 then if pass = !accountPasswd then match amount with | x when x < 0 -> raise negative_amount | _ -> accountBalance := !accountBalance + amount; counter := 0 else (counter := !counter + 1; raise wrong_pass) else raise too_many_failures ); show_balance = (fun pass -> if !counter < 5 then if pass = !accountPasswd then (counter := 0; !accountBalance) else (counter := !counter + 1; raise wrong_pass) else raise too_many_failures ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun x (e1,e2,e3) -> if e1 = vertex then (e2,e3)::x else x) [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let w = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = match node with | (n1, w1) when n1 = b -> (List.rev (b::visited), !w + w1) | (n2, w2) -> w := !w + w2; if List.mem n2 visited then raise Fail else aux_list (neighbours g n2) (n2::visited) 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 -> w := (!w - y); aux_list xs visited in aux_node (a, 0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let w = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= match node with | (n1, w1) when n1 = b -> sc (List.rev (b::visited), !w + w1) | (n2, w2) -> w := !w + w2; if List.mem n2 visited then fc () else aux_list (neighbours g n2) (n2::visited) fc sc 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 () -> w := !w - y; aux_list xs visited fc sc) sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun r -> r);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();;
let open_account (initial_pass: passwd) : bank_account = let accountPasswd = ref initial_pass in let accountBalance = ref 0 in let counter = ref 0 in { update_pass = (fun pass newPasswd -> if pass = !accountPasswd then (accountPasswd := newPasswd; counter := 0) else (counter := !counter + 1; raise wrong_pass) ); retrieve = (fun pass amount -> if !counter < 5 then if pass = !accountPasswd then match amount with | x when x < 0 -> raise negative_amount | y when y > !accountBalance -> counter := 0; raise not_enough_balance | _ -> accountBalance := !accountBalance - amount; counter := 0 else (counter := !counter + 1; raise wrong_pass) else raise too_many_failures ); deposit = (fun pass amount -> if !counter < 5 then if pass = !accountPasswd then match amount with | x when x < 0 -> raise negative_amount | _ -> accountBalance := !accountBalance + amount; counter := 0 else (counter := !counter + 1; raise wrong_pass) else raise too_many_failures ); show_balance = (fun pass -> if !counter < 5 then if pass = !accountPasswd then (counter := 0; !accountBalance) else (counter := !counter + 1; raise wrong_pass) else raise too_many_failures ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun x (e1,e2,e3) -> if e1 = vertex then (e2,e3)::x else x) [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let w = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = match node with | (n1, w1) when n1 = b -> (List.rev (b::visited), !w + w1) | (n2, w2) -> w := !w + w2; if List.mem n2 visited then raise Fail else aux_list (neighbours g n2) (n2::visited) 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 -> w := (!w - y); aux_list xs visited in aux_node (a, 0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let w = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= match node with | (n1, w1) when n1 = b -> sc (List.rev (b::visited), !w + w1) | (n2, w2) -> w := !w + w2; if List.mem n2 visited then fc () else aux_list (neighbours g n2) (n2::visited) fc sc 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 () -> w := !w - y; aux_list xs visited fc sc) sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun r -> r);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let longest = ref ([""], 0) in List.iter (fun (a,b) -> if b > (snd !longest) then longest := (a,b)) (find_all_paths g a b); if !longest = ([""], 0) then None else Some !longest;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let currentbalance = ref 0 in let currentfailures = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> if (!password = old_pass) then ( password := new_pass ; currentfailures := 0; ) else ( currentfailures := !currentfailures + 1; raise wrong_pass ) ); deposit = (fun (pass: passwd) (deposit: int) -> if (!currentfailures >= 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; if (deposit >= 0) then ( currentbalance := !currentbalance + deposit ) else ( raise negative_amount ) ) else (currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); retrieve = (fun (pass: passwd) (retrieval: int) -> if (!currentfailures >= 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; if ( retrieval > 0 ) then ( if ( !currentbalance > retrieval ) then ( currentbalance := !currentbalance - retrieval ) else ( raise not_enough_balance ) ) else ( raise negative_amount ) ) else ( currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); show_balance = (fun (pass: passwd) : int -> if (!currentfailures = 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; !currentbalance ) else (currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let isolate_from = fun (a,b,c) -> a in let isolate_to = fun (a,b,c) -> b in let isolate_weight = fun (a,b,c) -> c in let folding = fun inputedge emptylist -> if (isolate_from inputedge = vertex) then (isolate_to inputedge, isolate_weight inputedge) :: emptylist else emptylist in List.fold_right (folding) g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if (fst node = b) then ([fst node], snd node ) else if List.mem (fst node) visited then raise Fail else let (path,cost) = aux_list (neighbours g (fst node)) ([fst node]@visited) in ([fst node]@path, cost + snd node) and aux_list (nodes: ('a * weight) list) (visited: 'a list): ('a list * weight) = match nodes with | [] -> raise Fail | n::list -> try aux_node (n) (visited) with Fail -> aux_list (list) (visited) in aux_node (a,0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = notimplemented();;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = None;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let currentbalance = ref 0 in let currentfailures = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> if (!password = old_pass) then ( password := new_pass ; currentfailures := 0; ) else ( currentfailures := !currentfailures + 1; raise wrong_pass ) ); deposit = (fun (pass: passwd) (deposit: int) -> if (!currentfailures >= 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; if (deposit >= 0) then ( currentbalance := !currentbalance + deposit ) else ( raise negative_amount ) ) else (currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); retrieve = (fun (pass: passwd) (retrieval: int) -> if (!currentfailures >= 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; if ( retrieval > 0 ) then ( if ( !currentbalance > retrieval ) then ( currentbalance := !currentbalance - retrieval ) else ( raise not_enough_balance ) ) else ( raise negative_amount ) ) else ( currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); show_balance = (fun (pass: passwd) : int -> if (!currentfailures = 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; !currentbalance ) else (currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let isolate_from = fun (a,b,c) -> a in let isolate_to = fun (a,b,c) -> b in let isolate_weight = fun (a,b,c) -> c in let folding = fun inputedge emptylist -> if (isolate_from inputedge = vertex) then (isolate_to inputedge, isolate_weight inputedge) :: emptylist else emptylist in List.fold_right (folding) g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if (fst node = b) then ([fst node], snd node ) else if List.mem (fst node) visited then raise Fail else let (path,cost) = aux_list (neighbours g (fst node)) ([fst node]@visited) in ([fst node]@path, cost + snd node) and aux_list (nodes: ('a * weight) list) (visited: 'a list): ('a list * weight) = match nodes with | [] -> raise Fail | n::list -> try aux_node (n) (visited) with Fail -> aux_list (list) (visited) in aux_node (a,0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if (fst node = b) then sc ([fst node], snd node ) else if List.mem (fst node) visited then fc () else let suc2 = fun (path,cost) -> (let (path,cost) = aux_list (neighbours g (fst node)) ([fst node]@visited) (fc) (sc) in ([fst node]@path, cost + snd node) ) in let fail2 = fun () -> aux_list (neighbours g (fst node)) ([fst node]@visited) (fc) (sc) in aux_node (node) ([fst node]@visited) (fail2) (suc2) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | n::list -> let suc2 = sc in let fail2 = fun () -> aux_node (n) (visited) (fun () -> raise Fail) (fun (path,cost) -> (path,cost)) in aux_list (list) (visited) (fail2) (suc2) in aux_node (a,0) [] (fun () -> raise Fail) (fun (path,cost) -> (path,cost));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = None;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let currentbalance = ref 0 in let currentfailures = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> if (!password = old_pass) then ( password := new_pass ; currentfailures := 0; ) else ( currentfailures := !currentfailures + 1; raise wrong_pass ) ); deposit = (fun (pass: passwd) (deposit: int) -> if (!currentfailures >= 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; if (deposit >= 0) then ( currentbalance := !currentbalance + deposit ) else ( raise negative_amount ) ) else (currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); retrieve = (fun (pass: passwd) (retrieval: int) -> if (!currentfailures >= 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; if ( retrieval > 0 ) then ( if ( !currentbalance > retrieval ) then ( currentbalance := !currentbalance - retrieval ) else ( raise not_enough_balance ) ) else ( raise negative_amount ) ) else ( currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); show_balance = (fun (pass: passwd) : int -> if (!currentfailures = 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; !currentbalance ) else (currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let isolate_from = fun (a,b,c) -> a in let isolate_to = fun (a,b,c) -> b in let isolate_weight = fun (a,b,c) -> c in let folding = fun inputedge emptylist -> if (isolate_from inputedge = vertex) then (isolate_to inputedge, isolate_weight inputedge) :: emptylist else emptylist in List.fold_right (folding) g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if (fst node = b) then ([fst node], snd node ) else if List.mem (fst node) visited then raise Fail else let (path,cost) = aux_list (neighbours g (fst node)) ([fst node]@visited) in ([fst node]@path, cost + snd node) and aux_list (nodes: ('a * weight) list) (visited: 'a list): ('a list * weight) = match nodes with | [] -> raise Fail | n::list -> try aux_node (n) (visited) with Fail -> aux_list (list) (visited) in aux_node (a,0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if (fst node = b) then sc ([fst node], snd node ) else if List.mem (fst node) visited then fc () else let suc2 = fun (path,cost) -> (let (path,cost) = aux_list (neighbours g (fst node)) ([fst node]@visited) (fc) (sc) in ([fst node]@path, cost + snd node) ) in let fail2 = fun () -> aux_list (neighbours g (fst node)) ([fst node]@visited) (fc) (sc) in aux_node (node) ([fst node]@visited) (fail2) (suc2) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | n::list -> let suc2 = sc in let fail2 = fun () -> aux_node (n) (visited) (fun () -> raise Fail) (fun (path,cost) -> (path,cost)) in aux_list (list) (visited) (fail2) (suc2) in aux_node (a,0) [] (fun () -> raise Fail) (fun (path,cost) -> (path,cost));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = None;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let currentbalance = ref 0 in let currentfailures = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> if (!password = old_pass) then ( password := new_pass ; currentfailures := 0; ) else ( currentfailures := !currentfailures + 1; raise wrong_pass ) ); deposit = (fun (pass: passwd) (deposit: int) -> if (!currentfailures >= 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; if (deposit >= 0) then ( currentbalance := !currentbalance + deposit ) else ( raise negative_amount ) ) else (currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); retrieve = (fun (pass: passwd) (retrieval: int) -> if (!currentfailures >= 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; if ( retrieval > 0 ) then ( if ( !currentbalance > retrieval ) then ( currentbalance := !currentbalance - retrieval ) else ( raise not_enough_balance ) ) else ( raise negative_amount ) ) else ( currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); show_balance = (fun (pass: passwd) : int -> if (!currentfailures = 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; !currentbalance ) else (currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let isolate_from = fun (a,b,c) -> a in let isolate_to = fun (a,b,c) -> b in let isolate_weight = fun (a,b,c) -> c in let folding = fun inputedge emptylist -> if (isolate_from inputedge = vertex) then (isolate_to inputedge, isolate_weight inputedge) :: emptylist else emptylist in List.fold_right (folding) g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if (fst node = b) then ([fst node], snd node ) else if List.mem (fst node) visited then raise Fail else let (path,cost) = aux_list (neighbours g (fst node)) ([fst node]@visited) in ([fst node]@path, cost + snd node) and aux_list (nodes: ('a * weight) list) (visited: 'a list): ('a list * weight) = match nodes with | [] -> raise Fail | n::list -> try aux_node (n) (visited) with Fail -> aux_list (list) (visited) in aux_node (a,0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if (fst node = b) then sc ([fst node], snd node ) else if List.mem (fst node) visited then fc () else let suc2 = fun (path,cost) -> (let (path,cost) = aux_list (neighbours g (fst node)) ([fst node]@visited) (fc) (sc) in ([fst node]@path, cost + snd node) ) in let fail2 = fun () -> aux_list (neighbours g (fst node)) ([fst node]@visited) (fc) (sc) in aux_node (node) ([fst node]@visited) (fail2) (suc2) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | n::list -> let suc2 = sc in let fail2 = fun () -> aux_node (n) (visited) (fun () -> raise Fail) (fun (path,cost) -> (path,cost)) in aux_list (list) (visited) (fail2) (suc2) in aux_node (a,0) [] (fun () -> raise Fail) (fun (path,cost) -> (path,cost));;
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 password = ref initial_pass in let currentbalance = ref 0 in let currentfailures = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> if (!password = old_pass) then ( password := new_pass ; currentfailures := 0; ) else ( currentfailures := !currentfailures + 1; raise wrong_pass ) ); deposit = (fun (pass: passwd) (deposit: int) -> if (!currentfailures >= 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; if (deposit >= 0) then ( currentbalance := !currentbalance + deposit ) else ( raise negative_amount ) ) else (currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); retrieve = (fun (pass: passwd) (retrieval: int) -> if (!currentfailures >= 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; if ( retrieval > 0 ) then ( if ( !currentbalance > retrieval ) then ( currentbalance := !currentbalance - retrieval ) else ( raise not_enough_balance ) ) else ( raise negative_amount ) ) else ( currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); show_balance = (fun (pass: passwd) : int -> if (!currentfailures = 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; !currentbalance ) else (currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let isolate_from = fun (a,b,c) -> a in let isolate_to = fun (a,b,c) -> b in let isolate_weight = fun (a,b,c) -> c in let folding = fun inputedge emptylist -> if (isolate_from inputedge = vertex) then (isolate_to inputedge, isolate_weight inputedge) :: emptylist else emptylist in List.fold_right (folding) g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if (fst node = b) then ([fst node], snd node ) else if List.mem (fst node) visited then raise Fail else let (path,cost) = aux_list (neighbours g (fst node)) ([fst node]@visited) in ([fst node]@path, cost + snd node) and aux_list (nodes: ('a * weight) list) (visited: 'a list): ('a list * weight) = match nodes with | [] -> raise Fail | n::list -> try aux_node (n) (visited) with Fail -> aux_list (list) (visited) in aux_node (a,0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if (fst node = b) then sc ([fst node], snd node ) else if List.mem (fst node) visited then fc () else let suc2 = fun (path,cost) -> (let (path,cost) = aux_list (neighbours g (fst node)) ([fst node]@visited) (fc) (sc) in ([fst node]@path, cost + snd node) ) in let fail2 = fun () -> aux_list (neighbours g (fst node)) ([fst node]@visited) (fc) (sc) in aux_node (node) ([fst node]@visited) (fail2) (suc2) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | n::list -> let suc2 = sc in let fail2 = fun () -> aux_node (n) (visited) (fun () -> raise Fail) (fun (path,cost) -> (path,cost)) in aux_list (list) (visited) (fail2) (suc2) in aux_node (a,0) [] (fun () -> raise Fail) (fun (path,cost) -> (path,cost));;
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 password = ref initial_pass in let currentbalance = ref 0 in let currentfailures = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> if (!password = old_pass) then ( password := new_pass ; currentfailures := 0; ) else ( currentfailures := !currentfailures + 1; raise wrong_pass ) ); deposit = (fun (pass: passwd) (deposit: int) -> if (!currentfailures >= 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; if (deposit >= 0) then ( currentbalance := !currentbalance + deposit ) else ( raise negative_amount ) ) else (currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); retrieve = (fun (pass: passwd) (retrieval: int) -> if (!currentfailures >= 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; if ( retrieval > 0 ) then ( if ( !currentbalance > retrieval ) then ( currentbalance := !currentbalance - retrieval ) else ( raise not_enough_balance ) ) else ( raise negative_amount ) ) else ( currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); show_balance = (fun (pass: passwd) : int -> if (!currentfailures = 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; !currentbalance ) else (currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let isolate_from = fun (a,b,c) -> a in let isolate_to = fun (a,b,c) -> b in let isolate_weight = fun (a,b,c) -> c in let folding = fun inputedge emptylist -> if (isolate_from inputedge = vertex) then (isolate_to inputedge, isolate_weight inputedge) :: emptylist else emptylist in List.fold_right (folding) g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if (fst node = b) then ([fst node], snd node ) else if List.mem (fst node) visited then raise Fail else let (path,cost) = aux_list (neighbours g (fst node)) ([fst node]@visited) in ([fst node]@path, cost + snd node) and aux_list (nodes: ('a * weight) list) (visited: 'a list): ('a list * weight) = match nodes with | [] -> raise Fail | n::list -> try aux_node (n) (visited) with Fail -> aux_list (list) (visited) in aux_node (a,0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if (fst node = b) then sc ([fst node], snd node ) else if List.mem (fst node) visited then fc () else let suc2 = fun (path,cost) -> (let (path,cost) = aux_list (neighbours g (fst node)) ([fst node]@visited) (fc) (sc) in ([fst node]@path, cost + snd node) ) in let fail2 = fun () -> aux_list (neighbours g (fst node)) ([fst node]@visited) (fc) (sc) in aux_node (node) ([fst node]@visited) (fail2) (suc2) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | n::list -> let suc2 = sc in let fail2 = fun () -> aux_node (n) (visited) (fun () -> raise Fail) (fun (path,cost) -> (path,cost)) in aux_list (list) (visited) (fail2) (suc2) in aux_node (a,0) [] (fun () -> raise Fail) (fun (path,cost) -> (path,cost));;
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 password = ref initial_pass in let currentbalance = ref 0 in let currentfailures = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> if (!password = old_pass) then ( password := new_pass ; currentfailures := 0; ) else ( currentfailures := !currentfailures + 1; raise wrong_pass ) ); deposit = (fun (pass: passwd) (deposit: int) -> if (!currentfailures >= 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; if (deposit >= 0) then ( currentbalance := !currentbalance + deposit ) else ( raise negative_amount ) ) else (currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); retrieve = (fun (pass: passwd) (retrieval: int) -> if (!currentfailures >= 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; if ( retrieval > 0 ) then ( if ( !currentbalance > retrieval ) then ( currentbalance := !currentbalance - retrieval ) else ( raise not_enough_balance ) ) else ( raise negative_amount ) ) else ( currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); show_balance = (fun (pass: passwd) : int -> if (!currentfailures = 5) then ( raise too_many_failures ) else ( if (!password = pass) then ( currentfailures := 0; !currentbalance ) else (currentfailures := !currentfailures + 1; raise wrong_pass ) ) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let isolate_from = fun (a,b,c) -> a in let isolate_to = fun (a,b,c) -> b in let isolate_weight = fun (a,b,c) -> c in let folding = fun inputedge emptylist -> if (isolate_from inputedge = vertex) then (isolate_to inputedge, isolate_weight inputedge) :: emptylist else emptylist in List.fold_right (folding) g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if (fst node = b) then ([fst node], snd node ) else if List.mem (fst node) visited then raise Fail else let (path,cost) = aux_list (neighbours g (fst node)) ([fst node]@visited) in ([fst node]@path, cost + snd node) and aux_list (nodes: ('a * weight) list) (visited: 'a list): ('a list * weight) = match nodes with | [] -> raise Fail | n::list -> try aux_node (n) (visited) with Fail -> aux_list (list) (visited) in aux_node (a,0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if (fst node = b) then sc ([fst node], snd node ) else if List.mem (fst node) visited then fc () else let suc2 = fun (path,cost) -> (let (path,cost) = aux_list (neighbours g (fst node)) ([fst node]@visited) (fc) (sc) in ([fst node]@path, cost + snd node) ) in let fail2 = fun () -> aux_list (neighbours g (fst node)) ([fst node]@visited) (fc) (sc) in aux_node (node) ([fst node]@visited) (fail2) (suc2) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | n::list -> let suc2 = sc in let fail2 = fun () -> aux_node (n) (visited) (fun () -> raise Fail) (fun (path,cost) -> (path,cost)) in aux_list (list) (visited) (fail2) (suc2) in aux_node (a,0) [] (fun () -> raise Fail) (fun (path,cost) -> (path,cost));;
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 pswd = ref initial_pass in let bal = ref 0 in let wrong_count = ref 0 in let not_too_big = (fun b -> if (b > !bal) then raise not_enough_balance else true) in let check_bal = (fun b -> if (b < 0) then raise negative_amount else true) in let check_pass = (fun pass -> if (!wrong_count < 5) then (if (pass = !pswd) then (wrong_count := 0; true) else (wrong_count := !wrong_count + 1; raise wrong_pass)) else raise too_many_failures) in {update_pass = (fun oldpass newpass-> if (oldpass = !pswd) then (pswd := newpass; wrong_count := 0) else (wrong_count := !wrong_count + 1; raise wrong_pass)); retrieve = (fun pass amount -> if ((check_pass pass) && (check_bal amount) && (not_too_big amount)) then bal := !bal - amount); deposit = (fun pass amount -> if ((check_pass pass) && (check_bal amount)) then bal := !bal + amount); show_balance = (fun pass -> if (check_pass pass) then !bal else 0)} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let helper = (fun a (v1, v2, w) -> a @ [(v2, w)]) in let l1 = List.filter (fun (v1, v2, w) -> (v1 = vertex)) g.edges in List.fold_left helper [] l1 ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let wAcc = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let element = fst(node) in if List.mem element visited then raise Fail else (wAcc := (!wAcc + snd(node)); let l1 = visited @ [element] in if (element = b) then (l1, !wAcc) else (try aux_list (neighbours g element) l1 with | Fail -> wAcc := !wAcc - snd(node); raise Fail)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | hd::tl -> (try aux_node hd visited with | Fail -> aux_list tl visited) in aux_node (a, 0) [] ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let element = fst(node) in if (List.mem element visited) then fc () else let l1 = visited @ [element] in if element = b then sc ([b], snd(node)) else try aux_list (neighbours g element) l1 fc (fun (x, y) -> sc (element::x, y + snd(node))) with | Fail -> fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> let failfun = (fun () -> aux_list (List.tl nodes) visited fc sc) in aux_node (List.hd nodes) visited failfun sc in let sc = (fun y -> y) in aux_node (a, 0) [] (fun () -> aux_list (neighbours g a) [] (fun () -> raise Fail) sc) sc ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let l1 = find_all_paths g a b in let l2 = List.sort (fun x y -> snd(x) - snd(y)) l1 in let l3 = List.rev l2 in match l3 with | [] -> None | _ -> Some (List.hd l3) ;;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let counter = ref 0 in { update_pass = (fun old_pass new_pass -> if (old_pass = !pass) then (pass := new_pass) else (counter := !counter + 1; raise wrong_pass)) ; deposit = (fun p num -> if (!counter < 5) then ( if (p = !pass) then ( if (num >= 0) then (balance := !balance + num) else (raise negative_amount)) else (counter := !counter + 1; raise wrong_pass)) else (raise too_many_failures)) ; retrieve = (fun p num -> if (!counter < 5) then ( if (p = !pass) then ( match num with | num when num < 0 -> raise negative_amount | num when (!balance - num) < 0 -> raise not_enough_balance | _ -> balance := !balance - num ) else (counter := !counter + 1; raise wrong_pass)) else (raise too_many_failures)) ; show_balance = (fun p -> if (!counter < 5) then ( if (p = !pass) then (!balance) else (counter := !counter + 1; raise wrong_pass)) else (raise too_many_failures)) };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right ( fun (v1, v2, w) acc -> if (v1 = vertex) then ((v2, w) :: acc) else acc) g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a) (w: 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, cost + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (n, w) :: tail -> try aux_node n w visited with Fail -> aux_list tail 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) (w: 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, cost + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (vt, vw) :: vs -> aux_node vt vw (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 | [(node, w)] -> Some (node, w) | (node1, w1) :: (node2, w2) :: tail -> if w1 >= w2 then max((node1, w1) :: tail) else max((node2, w2) :: tail) in max (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let account = { password = ref initial_pass ; balance = ref 0 ; wrong_pw_count = ref 0 ; } in { update_pass = ( fun initial_pw final_pw -> if initial_pw = !(account.password) then ( account.password := final_pw ; account.wrong_pw_count := 0 ) else ( account.wrong_pw_count := !(account.wrong_pw_count) + 1 ; raise wrong_pass ) ) ; retrieve = ( fun pw amt -> if !(account.wrong_pw_count) >= 5 then ( account.wrong_pw_count := !(account.wrong_pw_count) + 1 ; raise too_many_failures ) else ( if pw = !(account.password) then ( account.wrong_pw_count := 0 ; if amt < 0 then raise negative_amount else ( if amt > !(account.balance) then raise not_enough_balance else ( account.balance := !(account.balance) - amt ) ) ) else ( account.wrong_pw_count := !(account.wrong_pw_count) + 1 ; raise wrong_pass ) ) ) ; deposit = ( fun pw amt -> if !(account.wrong_pw_count) >= 5 then ( account.wrong_pw_count := !(account.wrong_pw_count) + 1 ; raise too_many_failures ) else ( if pw = !(account.password) then ( account.wrong_pw_count := 0 ; if amt < 0 then ( raise negative_amount ) else ( account.balance := !(account.balance) + amt ) ) else ( account.wrong_pw_count := !(account.wrong_pw_count) + 1 ; raise wrong_pass ) ) ) ; show_balance = ( fun pw -> if !(account.wrong_pw_count) >= 5 then ( account.wrong_pw_count := !(account.wrong_pw_count) + 1 ; raise too_many_failures ) else ( if pw = !(account.password) then ( account.wrong_pw_count := 0 ; !(account.balance) ) else ( account.wrong_pw_count := !(account.wrong_pw_count) + 1 ; raise wrong_pass ) ) ) ; } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left ( fun lst edge -> let (v, b, w) = edge in if v = vertex then List.append lst [(b, w)] else List.append lst [] ) [] 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) (totalWeight: int): ('a list * weight) = if (fst node) = b then (List.rev ((fst node)::visited), (totalWeight + (snd node))) else if List.mem (fst node) visited then raise Fail else;;
let neighbours = neighbours g (fst node) in match neighbours with | [] -> raise Fail | _ -> aux_list neighbours ((fst node)::visited) (totalWeight + (snd node)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (totalWeight: int): ('a list * weight) = match nodes with | [] -> raise Fail | somenode::rest -> try aux_node somenode visited totalWeight with Fail -> aux_list rest visited totalWeight in match neighbours g a with | [] -> raise Fail | neighbours -> aux_list neighbours [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) (totalWeight: int) fc sc : ('a list * weight) = if (fst node) = b then (List.rev ((fst node)::visited), (totalWeight + (snd node))) else if List.mem (fst node) visited then fc () else let neighb = neighbours g (fst node) in match neighb with | [] -> raise Fail | _ -> aux_list neighb ((fst node)::visited) (totalWeight + (snd node)) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (totalWeight: int) fc sc : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> let (n1, w1) = x in try aux_node (n1,w1) visited totalWeight fc sc with Fail -> aux_list xs visited totalWeight fc sc in match neighbours g a with | [] -> raise Fail | neighb -> aux_list neighb [a] 0 (fun x -> raise Fail) (fun x -> x) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = List.fold_left ( fun x (node, weight) -> match x with | None -> Some(node, weight) | Some(otherNode, otherWeight) -> if (weight > otherWeight) then Some(node, weight) else Some(otherNode, otherWeight) ) None (find_all_paths g a b) ;;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let wrongpass_count = ref 0 in { update_pass = (fun initial final -> if initial = !(pass) then (pass := final; wrongpass_count := 0) else (wrongpass_count := !(wrongpass_count) + 1; raise wrong_pass) ); retrieve = (fun password amount -> if !(wrongpass_count) = 5 then (raise too_many_failures) else if password = !(pass) then (wrongpass_count := 0; if amount < 0 then raise negative_amount else if amount > !(balance) then raise not_enough_balance else balance := !(balance) - amount) else (wrongpass_count := !(wrongpass_count) + 1; raise wrong_pass) ); deposit = (fun password amount -> if !(wrongpass_count) = 5 then (raise too_many_failures) else if password = !(pass) then (wrongpass_count := 0; if amount < 0 then raise negative_amount else balance := !(balance) + amount) else (wrongpass_count := !(wrongpass_count) + 1; raise wrong_pass) ); show_balance = (fun password -> if !(wrongpass_count) = 5 then (raise too_many_failures) else if password = !(pass) then (wrongpass_count := 0; !(balance)) else (wrongpass_count := !(wrongpass_count) + 1; raise wrong_pass) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec find_node node lst = match node with |[] -> lst |(n1, n2, w)::xs -> if n1 = vertex then find_node (xs) ((n2, w)::lst) else find_node (xs) lst in find_node 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) (tweight: int): ('a list * weight) = let (n,w) = node in if n = b then (List.rev (n::visited), (tweight+w)) else if List.mem n visited then raise Fail else;;
let neighbours = neighbours g n in match neighbours with |[] -> raise Fail |_ -> aux_list neighbours (n::visited) (tweight + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (tweight: int): ('a list * weight) = match nodes with |[] -> raise Fail |n::xs -> let (n1,w1) = n in try aux_node (n1,w1) visited tweight with Fail -> aux_list xs visited tweight in match neighbours g a with |[] -> raise Fail |neighbours -> aux_list neighbours [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) (totalweight: weight) fc sc : ('a list * weight)= let (n, w) = node in if (n = b) then sc (visited@[n], totalweight + w) else if (List.mem n visited) then fc () else aux_list (neighbours g n) (visited@[n]) (totalweight + w) fc sc; and aux_list (nodes: ('a * weight) list) (visited: 'a list) (totalweight : weight) fc sc : ('a list * weight) = match nodes with | [] -> fc () | n::xs -> aux_node n visited totalweight (fun () -> aux_list xs visited totalweight fc sc) sc in aux_node (a,0) [] 0 (fun () -> raise Fail) (fun (lst,w) -> (lst,w)) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let f (x) (s,w) = match x with | None -> Some(s,w) | Some(maxp, maxw) -> if (w > maxw) then Some(s,w) else Some(maxp, maxw) in List.fold_left f None (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let c = ref 0 in let balance = ref 0 in let password = ref initial_pass in { update_pass = (fun oldpass newpass -> if ( oldpass = !password) then (c := 0; password := newpass) else ( c := !c + 1; raise wrong_pass ) ); retrieve = ( fun pw output -> if ( !c >= 5) then raise too_many_failures else ( if pw = !password then (c := 0; if output < 0 then raise negative_amount else if output > !balance then raise not_enough_balance else balance := !balance - output ) else (c := !c + 1; if !c > 5 then raise too_many_failures else raise wrong_pass) ) ); deposit = ( fun pw input -> if ( !c >= 5 ) then raise too_many_failures else (if ( pw = !password) then ( c := 0; if input < 0 then raise negative_amount else balance := !balance + input ) else (c := !c + 1; if !c > 5 then raise too_many_failures else raise wrong_pass) ) ); show_balance = ( fun pw -> if ( !c >= 5) then raise too_many_failures else if pw = !password then (c := 0; !balance ) else( c := !c + 1; if !c > 5 then raise too_many_failures else raise wrong_pass) ); } ;;