text
stringlengths 0
601k
|
---|
let open_account (initial_pass: passwd) : bank_account = let curr_pass = ref initial_pass in let balance = ref 0 in let fails = ref 0 in let check_pass = function pass -> if !fails = 5 then raise too_many_failures else if pass = !curr_pass then (fails := 0; true) else (fails := 1 + !fails; false) in let update_pass = fun pass new_pass -> if !curr_pass = pass then (fails := 0 ;curr_pass := new_pass) else (fails := !fails + 1; raise wrong_pass) in let retrieve = fun pass amount -> if not (check_pass pass) then raise wrong_pass else if amount < 0 then raise negative_amount else if amount > !balance then raise not_enough_balance else balance := !balance - amount in let deposit = fun pass amount -> if not (check_pass pass) then raise wrong_pass else if amount < 0 then raise negative_amount else balance := !balance + amount in let show_balance = function pass -> if not (check_pass pass) then raise wrong_pass else !balance in {update_pass=update_pass; retrieve=retrieve; deposit=deposit; show_balance=show_balance} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec filter edges vertex acc = match edges with | [] -> acc | x::xs -> let v1,v2,w = x in if v1 = vertex then filter xs vertex ((v2,w)::acc) else filter xs vertex acc in filter g.edges vertex [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let rec contains x l = match l with | [] -> false | h::t -> h=x || contains x t in let n,w = node in if contains n visited then raise Fail else if n = b then ([b],w) else let voisins = neighbours g n in let path,cost = aux_list (voisins) (n::visited) in let paths = n::path in let total_cost = w+cost in (paths, total_cost) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | h :: t -> try aux_node h visited with Fail -> aux_list t visited in aux_node (a,0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let n,w = node in let rec contains x l = match l with | [] -> false | h::t -> h=x || contains x t in if contains n visited then fc() else if n = b then sc([b], w) else let voisins = neighbours g n in let vis = n::visited in let acc = (function (path, cost) -> sc (n::path, cost+w)) in aux_list (voisins) (vis) fc acc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc() | h::t -> let f = function () -> aux_list t visited fc sc in aux_node h visited f sc in let fail = function() -> raise Fail in aux_node (a, 0) [] fail (function l -> l) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec find_longest l = match l with | [] -> None | [single_el] -> Some single_el | h::xs when xs <> [] -> let m::t = xs in let (p1,w1) = h in let (p2,w2) = m in match w1 >= w2 with | true -> find_longest ((p1,w1)::t) | false -> find_longest ((p2,w2)::t) in find_longest(find_all_paths g a b);; |
let open_account (initial_pass: passwd) : bank_account = let curr_pass = ref initial_pass in let curr_balance = ref 0 in let fail_counter = ref 0 in let correct_pass = (function x -> if x = !curr_pass then (fail_counter := 0; true) else (fail_counter := !fail_counter + 1; false)) in let raise_errors = (function x -> if !fail_counter >= 5 then raise too_many_failures else if not (correct_pass x) then raise wrong_pass) in let raise_errors2 = (fun x y -> raise_errors x; if y < 0 then raise negative_amount) in { update_pass = (fun x y -> if correct_pass x then curr_pass := y else raise wrong_pass); retrieve = (fun x y -> if raise_errors2 x y = () && y > !curr_balance then raise not_enough_balance else curr_balance := (!curr_balance - y)); deposit = (fun x y -> raise_errors2 x y; curr_balance := (!curr_balance + y)); show_balance = (fun x -> raise_errors x; !curr_balance) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = if not (List.exists ((=) vertex) g.nodes) then [] else List.fold_left (fun lst (_,v2,w) -> lst @ [(v2, w)]) [] (List.filter (function (v1, _, _) -> v1 = vertex) g.edges);; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = if not (List.exists ((=) a) g.nodes) || not (List.exists ((=) b) g.nodes) then raise Fail else if a = b then ([a], 0) else let weight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (v, w) = node in weight := !weight + w; if List.exists ((=) v) visited then raise Fail else aux_list (neighbours g v) (visited @ [v]) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |x::xs -> let (v,w) = x in if v = b then ((visited @ [b]), !weight + w) else try aux_node x visited with Fail -> weight := !weight - w; if xs = [] then raise Fail else aux_list xs visited in aux_node (a, 0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = if not (List.exists ((=) a) g.nodes) || not (List.exists ((=) b) g.nodes) then raise Fail else if a = b then ([a], 0) else let weight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (v, w) = node in weight := !weight + w; if List.exists ((=) v) visited then fc () else aux_list (neighbours g v) (visited @ [v]) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |x::xs -> let (v,w) = x in if v = b then sc (visited @ [v], !weight + w) else aux_node x visited (fun () -> weight := !weight - w; if xs = [] then fc () else 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 max_weight = ref 0 in let get_paths = find_all_paths g a b in match get_paths with |[] -> None |_ -> let _ = (List.exists (fun (_, cost) -> if cost > !max_weight then (max_weight := cost; false) else false) get_paths) in let res = (List.find (fun (_, w) -> w = !max_weight) get_paths) in Some (res) ;; |
let open_account (initial_pass: passwd) : bank_account = let balance, pass, fails = ref 0, ref initial_pass, ref 0 in { update_pass = (fun oldpass newpass -> if oldpass <> !pass then (fails := !fails + 1; raise wrong_pass) else (pass := newpass; fails := 0)) ; retrieve = (fun pwd amt -> if !fails = 5 then raise too_many_failures else if pwd <> !pass then (fails := !fails + 1; raise wrong_pass) else (if amt > !balance then raise not_enough_balance else if amt < 0 then raise negative_amount else balance := !balance - amt ; fails := 0)); deposit = (fun pwd amt -> if !fails = 5 then raise too_many_failures else if pwd <> !pass then (fails := !fails + 1; raise wrong_pass) else (if amt < 0 then raise negative_amount else balance := !balance + amt ; fails := 0)); show_balance = (fun pwd -> if !fails = 5 then raise too_many_failures else if pwd <> !pass then (fails := !fails + 1; raise wrong_pass) else fails := 0; !balance); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let h x acc = match x with |(v1, v2, w) -> if vertex = v1 then (v2, w)::acc else acc in List.fold_right h g.edges [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (acc: weight) (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (v,w) = node in if (List.mem v visited) then raise Fail else if (b = v) then (visited@[v], acc+w) else aux_list (acc+w) (neighbours g v) (visited@[v]) ; and aux_list (acc: weight) (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try aux_node acc x visited with Fail -> aux_list acc xs visited in aux_node 0 (a,0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (acc: weight) (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (v,w) = node in if ( List.mem v visited ) then fc () else if ( b = v ) then sc (visited@[v], acc+w) else aux_list (acc+w) (neighbours g v) (visited@[v]) fc sc; and aux_list (acc: weight) (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> aux_node acc x visited (fun () -> aux_list acc xs visited fc sc) sc in aux_node 0 (a,0) [] (fun () -> raise Fail) (fun (l,w) -> (l,w));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let f (optimal) (l,w)= match optimal with | None -> Some(l,w) | Some(maxPath, maxWeight) -> if (w > maxWeight) then Some(l,w) else Some(maxPath, maxWeight) 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 passwd = ref initial_pass in let balance = ref 0 in {update_pass = (fun pwd1 pwd2 -> if pwd1 = !passwd then let _ = (c := 0) in (passwd := pwd2) else let _ = (c := (!c + 1)) in raise wrong_pass); deposit=(fun p amount -> if p = !passwd && (!c < 5) then let _ = (c := 0) in if amount >= 0 then balance := (!balance + amount) else if amount < 0 then raise negative_amount else raise negative_amount else let _ = (c := (!c + 1)) in if !c >= 6 then raise too_many_failures else raise wrong_pass); retrieve = (fun p amount -> if p = !passwd && (!c<5) then let _ = (c := 0) in if amount < 0 then raise negative_amount else if amount >= 0 && (!balance < amount) then raise not_enough_balance else balance := (!balance - amount) else let _ = (c := (!c + 1)) in if !c < 6 then raise wrong_pass else raise too_many_failures); show_balance = (fun p -> if p = !passwd && (!c<5) then let _ = (c:= 0) in !balance else let _ = (c := (!c + 1)) in if !c < 6 then raise wrong_pass else raise too_many_failures) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec temp list solution= match list with |x::xs-> (let (vertex_1,vertex_2,w) = x in if not(vertex_1 = vertex) then temp xs solution else temp xs ((vertex_2,w) :: solution)) |_ -> solution in temp 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 : ('a list * weight) = let _ = node in if fst(node)=b then (List.rev(fst(node)::visited),(w+(snd(node)))) else if not (List.mem (fst(node)) visited) then (aux_list (neighbours g (fst(node))) (fst(node)::visited) (w+snd(node))) else raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) w: ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try aux_node x visited w with Fail -> aux_list xs visited w in aux_list (neighbours g a) ([a]) 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) w fc sc : ('a list * weight)= let _ = node in if fst(node)=b then (List.rev(fst(node)::visited),(w+(snd(node)))) else if not (List.mem (fst(node)) visited) then (aux_list (neighbours g (fst(node))) (fst(node)::visited) (w+snd(node)) fc sc) else fc() and aux_list (nodes: ('a * weight) list) (visited: 'a list) w fc sc : ('a list * weight) = match nodes with | [] -> fc() | x::xs -> let sc1=sc in let fc1 = fun() -> aux_node x visited w fc sc in aux_list xs visited w fc1 sc1 in aux_list (neighbours g a) ([a]) 0 (fun() -> raise Fail) (fun x -> x);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = if List.length(find_all_paths g a b) = 0 then None else let rec search p solution k y= match p with | [] -> Some(List.nth (find_all_paths g a b) y) | x::xs -> if snd(x) < solution then search xs solution (k+1) y else if snd(x) = solution then search xs solution (k+1) y else search xs (snd(x)) (k+1) (k) in search (find_all_paths g a b) 0 0 0;; |
let open_account (initial_pass: passwd) : bank_account = let attempts = ref 0 in let password = ref initial_pass in let balance = ref 0 in { update_pass = (fun x-> (fun y-> if !password <> x then let p = (attempts := !attempts+1; raise(wrong_pass)) in p else let w = (attempts := 0; password := y) in w )); retrieve = ( fun x-> (fun y-> if !attempts >= 5 then raise(too_many_failures) else if !password <> x then let a = (attempts := !attempts+1; raise(wrong_pass)) in a else if !balance < y then raise(not_enough_balance) else if y < 0 then raise(negative_amount) else let c = (attempts := 0; balance := !balance-y ) in c)); deposit = (fun x-> (fun y-> if !attempts >= 5 then raise(too_many_failures) else if !password <> x then let b = (attempts := !attempts+1; raise(wrong_pass)) in b else if y<0 then raise(negative_amount) else let d = (attempts :=0; balance := !balance+y) in d)); show_balance = (fun x-> if !attempts >= 5 then raise(too_many_failures) else if !password <> x then let e = (attempts := !attempts+1; raise(wrong_pass)) in e else let j = (attempts :=0; !balance) in j ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec find (l:('a * 'a * weight) list) (node: 'a) (acc: ('a * weight) list) : ('a * weight) list = match l with | [] -> acc | hd:: tl -> let (a,b,c) = hd in if a = node then find tl node (acc @ [(b,c)]) else find tl node acc in find g.edges vertex [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (c,d) =node in if c = b then (visited,d) else let neighbourlist = neighbours g c in let neighbourlist = List.map (fun (x,y) -> (x,y+d)) neighbourlist in aux_list neighbourlist visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> ([],0) | hd::tl -> let (n,w) = hd in if (List.mem (n) (visited) = false) then let path = aux_node hd (visited @ [n]) in match path with | ([],0) -> aux_list tl visited | (_,_) -> path else aux_list tl visited in let output = aux_list (neighbours g a) [a] in if output = ([],0) then raise Fail else output;; |
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 (c,d) =node in if c = b then sc (visited,d) else let neighbourlist = neighbours g c in let neighbourlist = List.map (fun (x,y) -> (x,y+d)) neighbourlist in aux_list neighbourlist visited fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc ([],0) | hd::tl -> let (n,w) = hd in if (List.mem (n) (visited) = false) then let path = aux_node hd (visited @ [n]) fc sc in match path with | ([],0) -> aux_list tl visited fc sc | (_,_) -> path else aux_list tl visited fc sc in aux_list (neighbours g a) [a] (fun (x,y)-> (x,y)) (fun (x,y) -> raise Fail);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let allpaths = find_all_paths g a b in if allpaths = [] then None else let sorted = List.sort (fun (x,y)-> (fun (z,w)-> y-w)) allpaths in let sorted = List.rev sorted in let max = List.nth sorted 0 in Some max;; |
let open_account (initial_pass: passwd) : bank_account = let attempts = ref 0 in let password = ref initial_pass in let balance = ref 0 in { update_pass = (fun x-> (fun y-> if !password <> x then let p = (attempts := !attempts+1; raise(wrong_pass)) in p else let w = (attempts := 0; password := y) in w )); retrieve = ( fun x-> (fun y-> if !attempts >= 5 then raise(too_many_failures) else if !password <> x then let a = (attempts := !attempts+1; raise(wrong_pass)) in a else if !balance < y then raise(not_enough_balance) else if y < 0 then raise(negative_amount) else let c = (attempts := 0; balance := !balance-y ) in c)); deposit = (fun x-> (fun y-> if !attempts >= 5 then raise(too_many_failures) else if !password <> x then let b = (attempts := !attempts+1; raise(wrong_pass)) in b else if y<0 then raise(negative_amount) else let d = (attempts :=0; balance := !balance+y) in d)); show_balance = (fun x-> if !attempts >= 5 then raise(too_many_failures) else if !password <> x then let e = (attempts := !attempts+1; raise(wrong_pass)) in e else let j = (attempts :=0; !balance) in j ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec find (l:('a * 'a * weight) list) (node: 'a) (acc: ('a * weight) list) : ('a * weight) list = match l with | [] -> acc | hd:: tl -> let (a,b,c) = hd in if a = node then find tl node (acc @ [(b,c)]) else find tl node acc in find g.edges vertex [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (c,d) =node in if c = b then (visited,d) else let neighbourlist = neighbours g c in let neighbourlist = List.map (fun (x,y) -> (x,y+d)) neighbourlist in aux_list neighbourlist visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> ([],0) | hd::tl -> let (n,w) = hd in if not (List.mem n visited) then let path = aux_node hd (visited @ [n]) in match path with | ([],0) -> aux_list tl visited | (_,_) -> path else aux_list tl visited in let output = aux_list (neighbours g a) [a] in if output = ([],0) then raise Fail else output ;; |
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 (c,d) =node in if c = b then sc (visited,d) else let neighbourlist = neighbours g c in let neighbourlist = List.map (fun (x,y) -> (x,y+d)) neighbourlist in let suc2 = sc in let fail2 = fun () -> aux_list neighbourlist visited fc sc in aux_list neighbourlist visited fail2 suc2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd::tl -> let (n,w) = hd in if not (List.mem n visited) then let path = aux_node hd (visited @ [n]) fc sc in match path with | ([],0) -> let sc2 = sc in let fc2 = fun () -> aux_list tl visited fc sc in aux_list tl visited fc2 sc2 | (_,_) -> path else aux_list tl visited fc sc in aux_list (neighbours g a) [a] (fun ()-> raise Fail) (fun (x,y) -> (x,y));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let allpaths = find_all_paths g a b in if allpaths = [] then None else let sorted = List.sort (fun (x,y)-> (fun (z,w)-> y-w)) allpaths in let sorted = List.rev sorted in let max = List.nth sorted 0 in Some max;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let wrong_time = ref 0 in let money = ref 0 in let update_pass oldp newp = if oldp = !pass then (pass := newp ; wrong_time := 0) else (wrong_time := !wrong_time +1 ; raise wrong_pass) in let deposit pw amount= if !wrong_time >= 5 then raise too_many_failures else (if pw = !pass then (if amount >= 0 then (money := !money + amount ; wrong_time := 0) else raise negative_amount) else (wrong_time := !wrong_time + 1; raise wrong_pass ) ) in let retrieve pw amount = if !wrong_time >= 5 then raise too_many_failures else (if pw = !pass then (if amount < 0 then raise negative_amount else if amount > !money then raise not_enough_balance else (money := !money - amount; wrong_time := 0)) else(wrong_time := !wrong_time + 1; raise wrong_pass ) ) in let show_balance pw = if !wrong_time >= 5 then raise too_many_failures else (if pw = !pass then ( wrong_time := 0 ;!money ) else(wrong_time := !wrong_time + 1; raise wrong_pass ) ) in { update_pass; deposit ; retrieve ; show_balance; };; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let add tup vertex lis = match tup with |(a,b,c) when a = vertex -> (b,c)::lis | _ -> lis in let rec mat edges vertex lis = match edges with |x::xs -> mat xs vertex (add x vertex lis) |[] -> lis in mat (g.edges) vertex [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (acc:weight): ('a list * weight) = let (a,w) = node in if List.mem a visited then raise Fail else if a = b then (visited@[a],w+acc) else aux_list (neighbours g a) (visited@[a]) (acc+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc:weight): ('a list * weight) = match nodes with |[] -> raise Fail |x :: xs -> try aux_node x visited acc with Fail -> aux_list xs visited acc in aux_list (neighbours g a) [a] 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (acc:weight) fc sc : ('a list * weight)= let (a,w) = node in if List.mem a visited then raise Fail else if a = b then (fc visited,sc acc) else aux_list (neighbours g a) (fc visited) (sc acc) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc:weight) fc sc : ('a list * weight) = match nodes with |[] -> raise Fail |(a,w) :: xs -> try let sc1 = fun u -> u+w in let fc1 = fun r -> r@[a] in aux_node (a,w) visited acc fc1 sc1 with Fail -> aux_list xs visited acc fc sc in let sc = fun r -> sc(r+w) infor failure*) aux_list (neighbours g a) [a] 0 (fun r -> [a]@r) (fun u -> u+0);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let wei = ([],0) in let all_path = find_all_paths g a b in let rec findmax all_path wei = match all_path with |[] -> let (a,w) = wei in if w = 0 then None else Some(wei) |x::xs -> let (_,w) = x in let (_,w1) = wei in if w1 < w then findmax xs x else findmax xs wei in findmax all_path wei;; |
let open_account (initial_pass: passwd) : bank_account = let oldpass = ref initial_pass in let balance = ref 0 in let errors = ref 0 in { update_pass = (fun a b -> if a = !oldpass then (oldpass := b ; errors := 0) else (errors := !errors + 1 ; raise wrong_pass)); retrieve = (fun a b -> if !errors >= 5 then raise too_many_failures else if a <> !oldpass then (errors := !errors + 1 ; raise wrong_pass) else if (b < 0 || b > !balance) then (if b < 0 then (errors := 0 ; raise negative_amount) else (errors := 0 ;raise not_enough_balance)) else (balance := !balance - b ; errors := 0)); deposit = (fun a b -> if !errors >= 5 then raise too_many_failures else if a <> !oldpass then (errors := !errors + 1 ; raise wrong_pass) else if b < 0 then (errors := 0 ; raise negative_amount) else (balance := !balance + b ; errors := 0)); show_balance = (fun a -> if !errors >= 5 then raise too_many_failures else if a <> !oldpass then (errors := !errors + 1 ; raise wrong_pass) else (errors := 0 ;!balance)) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun x (v,n,w) -> if v = vertex then (n,w) :: x else 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 (n,we) = node in if (List.exists (fun x -> x = n) visited) then raise Fail else if n = b then ((n::visited),we) else let (al,w) = aux_list (neighbours g n) (n::visited) in (al,we+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |(d,we) :: xs -> try aux_node (d,we) visited with Fail -> aux_list xs visited in let (l,w) = aux_list (neighbours g a) [a] in (List.rev l,w);; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (n,we) = node in let (al,w) = sc n we in if n = b then (al,w) else let fc2 = fc in let sc2 = fun x y -> (x::al,y+w) in aux_list (neighbours g n) al fc2 sc2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc: ('a list * weight) = match nodes with |[] -> fc () |(n,we) :: xs -> if (List.exists (fun x -> x = n) visited) then let sc2 = sc in let fc2 = fun () -> aux_list xs visited fc sc in aux_list xs visited fc2 sc2 else let fc2 = fun () -> aux_list xs visited fc sc in aux_node (n,we) visited fc2 sc in let (l,w) = aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun l w -> (l::[a],w)) in (List.rev l, w);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = if (find_all_paths g a b) = [] then None else let rec findmax l (cl,cw)= match l with |[] -> (cl,cw) |(l,w)::xs -> if w > cw then findmax xs (l,w) else findmax xs (cl,cw) in Some (findmax (find_all_paths g a b) ([],0));; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let fails = ref 0 in let verification password func = if !pass = password then (fails := 0; func ()) else (fails := (!fails + 1); raise wrong_pass) in let update_pass old_pass new_pass = verification old_pass (fun () -> pass := new_pass) in let deposit pass amount = if !fails >= 5 then raise too_many_failures else verification pass (fun () -> if amount >= 0 then balance := !balance + amount else raise negative_amount) in let retrieve pass amount = if !fails >= 5 then raise too_many_failures else verification pass (fun () -> if amount < 0 then raise negative_amount else if !balance >= amount then balance := !balance - amount else raise not_enough_balance) in let show_balance pass = if !fails >= 5 then raise too_many_failures else verification pass (fun () -> !balance) in {update_pass; retrieve; deposit; show_balance} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc (v1, v2, w) -> if vertex = v1 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) (path : 'a list) (cost : int) : ('a list * weight) = match node with | (v, w) -> if v = b then (path @ [v], cost + w) else if List.mem v visited then raise Fail else aux_list (neighbours g v) ([v] @ visited) (path @ [v]) (cost + w) and aux_list (node: ('a * weight) list) (visited: 'a list) (path : 'a list) (cost : int) : ('a list * weight) = match node with | [] -> raise Fail | (v, w) :: t -> try aux_node (v, w) visited path cost with Fail -> aux_list t visited path cost in aux_node (a, 0) [] [] 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= match node with | (v, w) -> if v = b then sc ([v], w) else if List.mem v visited then fc () else aux_list (neighbours g v) (v :: visited) fc (fun (path, cost) -> sc ([v] @ path, cost + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (v, w) :: t -> aux_node (v, w) visited (fun () -> aux_list t 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 longest l = match l with | [] -> None | [(path, cost)] -> Some (path, cost) | (p1, c1) :: (p2, c2) :: t -> if c1 <= c2 then longest ((p2, c2) :: t) else longest ((p1, c1) :: t) in longest (find_all_paths g a b);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let count = ref 0 in {update_pass = (fun p1 p2 -> if p1 = !password then let _ = (count := 0) in (password := p2) else let _ = (count := (!count + 1)) in raise wrong_pass) ; retrieve = (fun p amount -> if p = !password && (!count < 5) then let _ = (count := 0) in if amount >= 0 then if !balance >= amount then balance := (!balance - amount) else raise not_enough_balance else raise negative_amount else let _ = (count := (!count + 1)) in if !count < 6 then raise wrong_pass else raise too_many_failures); deposit=(fun p amount -> if p = !password && (!count < 5) then let _ = (count := 0) in if amount >= 0 then balance := (!balance + amount) else raise negative_amount else let _ = (count := (!count + 1)) in if !count < 6 then raise wrong_pass else raise too_many_failures); show_balance = (fun p -> if p = !password && (!count < 5) then let _ = (count := 0) in !balance else let _ = (count := (!count + 1)) in if !count < 6 then raise wrong_pass else raise too_many_failures) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper l result= match l with |x::xs-> (let (v1,v2,w) = x in if v1 = vertex then helper xs ((v2,w) :: result) else helper xs result) |_ -> result in 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) (w:weight): ('a list * weight) = match (neighbours g (fst(node))) with | [] -> raise Fail | x::xs -> if fst(x) = b then (List.rev((fst(x)::fst(node)::visited)),w+snd(node)+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs (fst(node)::visited) (w+snd(node)) else try aux_node x (fst(node)::visited) (w+snd(node)) with Fail -> aux_list xs (fst(node)::visited) (w+snd(node)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w:weight) : ('a list * weight) = match nodes with |[] -> raise Fail |x::xs -> if fst(x) = b then (List.rev(fst(x)::visited),w+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs visited w else try aux_node x visited w with Fail -> aux_list xs visited w in aux_list (neighbours g a) (a::[]) 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (w:weight) fc sc : ('a list * weight)= match (neighbours g (fst(node))) with | [] -> fc () | x::xs -> if fst(x) = b then (List.rev((fst(x)::fst(node)::visited)),w+snd(node)+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs (fst(node)::visited) (w+snd(node)) fc sc else let sc1 = sc in let fc1 = fun () -> aux_node x (fst(node)::visited) (w+snd(node)) fc sc in aux_list xs (fst(node)::visited) (w+snd(node)) fc1 sc1 and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w:weight) fc sc : ('a list * weight) = match nodes with |[] -> fc () |x::xs -> if fst(x) = b then (List.rev(fst(x)::visited),w+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs visited w fc sc else let sc2 = sc in let fc2 = fun () -> aux_node x visited w fc sc in aux_list xs visited w fc2 sc2 in aux_list (neighbours g a) (a::[]) 0 (fun () -> raise Fail) (fun y -> y);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = if List.length(find_all_paths g a b) = 0 then None else let rec helper l max i index= match l with | [] -> Some(List.nth l index) | x::xs -> if snd(x) > max then helper xs (snd(x)) (i+1) (i) else helper xs max (i+1) index in helper (find_all_paths g a b) 0 0 0;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let count = ref 0 in {update_pass = (fun p1 p2 -> if p1 = !password then let _ = (count := 0) in (password := p2) else let _ = (count := (!count + 1)) in raise wrong_pass) ; retrieve = (fun p amount -> if p = !password && (!count < 5) then let _ = (count := 0) in if amount >= 0 then if !balance >= amount then balance := (!balance - amount) else raise not_enough_balance else raise negative_amount else let _ = (count := (!count + 1)) in if !count < 6 then raise wrong_pass else raise too_many_failures); deposit=(fun p amount -> if p = !password && (!count < 5) then let _ = (count := 0) in if amount >= 0 then balance := (!balance + amount) else raise negative_amount else let _ = (count := (!count + 1)) in if !count < 6 then raise wrong_pass else raise too_many_failures); show_balance = (fun p -> if p = !password && (!count < 5) then let _ = (count := 0) in !balance else let _ = (count := (!count + 1)) in if !count < 6 then raise wrong_pass else raise too_many_failures) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper l result= match l with |x::xs-> (let (v1,v2,w) = x in if v1 = vertex then helper xs ((v2,w) :: result) else helper xs result) |_ -> result in 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) (w:weight): ('a list * weight) = match (neighbours g (fst(node))) with | [] -> raise Fail | x::xs -> if fst(x) = b then (List.rev((fst(x)::fst(node)::visited)),w+snd(node)+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs (fst(node)::visited) (w+snd(node)) else try aux_node x (fst(node)::visited) (w+snd(node)) with Fail -> aux_list xs (fst(node)::visited) (w+snd(node)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w:weight) : ('a list * weight) = match nodes with |[] -> raise Fail |x::xs -> if fst(x) = b then (List.rev(fst(x)::visited),w+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs visited w else try aux_node x visited w with Fail -> aux_list xs visited w in aux_list (neighbours g a) (a::[]) 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (w:weight) fc sc : ('a list * weight)= match (neighbours g (fst(node))) with | [] -> fc () | x::xs -> if fst(x) = b then (List.rev((fst(x)::fst(node)::visited)),w+snd(node)+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs (fst(node)::visited) (w+snd(node)) fc sc else let sc1 = sc in let fc1 = fun () -> aux_node x (fst(node)::visited) (w+snd(node)) fc sc in aux_list xs (fst(node)::visited) (w+snd(node)) fc1 sc1 and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w:weight) fc sc : ('a list * weight) = match nodes with |[] -> fc () |x::xs -> if fst(x) = b then (List.rev(fst(x)::visited),w+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs visited w fc sc else let sc2 = sc in let fc2 = fun () -> aux_node x visited w fc sc in aux_list xs visited w fc2 sc2 in aux_list (neighbours g a) (a::[]) 0 (fun () -> raise Fail) (fun y -> y);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = if List.length(find_all_paths g a b) = 0 then None else let rec helper l max i index= match l with | [] -> Some(List.nth (find_all_paths g a b) index) | x::xs -> if snd(x) > max then helper xs (snd(x)) (i+1) (i) else helper xs max (i+1) index in helper (find_all_paths g a b) 0 0 0;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let count = ref 0 in {update_pass = (fun p1 p2 -> if p1 = !password then let _ = (count := 0) in (password := p2) else let _ = (count := (!count + 1)) in raise wrong_pass) ; retrieve = (fun p amount -> if p = !password && (!count < 5) then let _ = (count := 0) in if amount >= 0 then if !balance >= amount then balance := (!balance - amount) else raise not_enough_balance else raise negative_amount else let _ = (count := (!count + 1)) in if !count < 6 then raise wrong_pass else raise too_many_failures); deposit=(fun p amount -> if p = !password && (!count < 5) then let _ = (count := 0) in if amount >= 0 then balance := (!balance + amount) else raise negative_amount else let _ = (count := (!count + 1)) in if !count < 6 then raise wrong_pass else raise too_many_failures); show_balance = (fun p -> if p = !password && (!count < 5) then let _ = (count := 0) in !balance else let _ = (count := (!count + 1)) in if !count < 6 then raise wrong_pass else raise too_many_failures) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper l result= match l with |x::xs-> (let (v1,v2,w) = x in if v1 = vertex then helper xs ((v2,w) :: result) else helper xs result) |_ -> result in 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) (w:weight): ('a list * weight) = match (neighbours g (fst(node))) with | [] -> raise Fail | x::xs -> if fst(x) = b then (List.rev((fst(x)::fst(node)::visited)),w+snd(node)+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs (fst(node)::visited) (w+snd(node)) else try aux_node x (fst(node)::visited) (w+snd(node)) with Fail -> aux_list xs (fst(node)::visited) (w+snd(node)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w:weight) : ('a list * weight) = match nodes with |[] -> raise Fail |x::xs -> if fst(x) = b then (List.rev(fst(x)::visited),w+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs visited w else try aux_node x visited w with Fail -> aux_list xs visited w in aux_list (neighbours g a) (a::[]) 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (w:weight) fc sc : ('a list * weight)= match (neighbours g (fst(node))) with | [] -> fc () | x::xs -> if fst(x) = b then (List.rev((fst(x)::fst(node)::visited)),w+snd(node)+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs (fst(node)::visited) (w+snd(node)) fc sc else let sc1 = sc in let fc1 = fun () -> aux_node x (fst(node)::visited) (w+snd(node)) fc sc in aux_list xs (fst(node)::visited) (w+snd(node)) fc1 sc1 and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w:weight) fc sc : ('a list * weight) = match nodes with |[] -> fc () |x::xs -> if fst(x) = b then (List.rev(fst(x)::visited),w+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs visited w fc sc else let sc2 = sc in let fc2 = fun () -> aux_node x visited w fc sc in aux_list xs visited w fc2 sc2 in aux_list (neighbours g a) (a::[]) 0 (fun () -> raise Fail) (fun y -> y);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = if List.length(find_all_paths g a b) = 0 then None else let rec helper l max i index= match l with | [] -> Some(List.nth (find_all_paths g a b) index) | x::xs -> if snd(x) > max then helper xs (snd(x)) (i+1) (i) else helper xs max (i+1) index in helper (find_all_paths g a b) 0 0 0;; |
let open_account (initial_pass: passwd) : bank_account = let account_pass = ref initial_pass in let balance = ref 0 in let tries = ref 0 in { update_pass = (fun old_pass new_pass -> if !account_pass = old_pass then (account_pass := new_pass; tries := 0) else (tries := !tries + 1; raise wrong_pass)); retrieve = (fun pass amount -> if !tries >= 5 then raise too_many_failures else (if !account_pass = pass then (tries := 0; if amount < 0 then raise negative_amount else if !balance < amount then raise not_enough_balance else balance := !balance - amount) else (tries := !tries + 1; raise wrong_pass))); deposit = (fun pass amount -> if !tries >= 5 then raise too_many_failures else ( if !account_pass = pass then (tries := 0; if amount < 0 then raise negative_amount else balance := !balance + amount) else (tries := !tries + 1; raise wrong_pass))); show_balance = (fun pass -> if !tries >= 5 then raise too_many_failures else ( if !account_pass = pass then (tries := 0; !balance;) else (tries := !tries + 1; raise wrong_pass))); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper edges vertex acc = match edges with | [] -> acc | (a, b, w)::t -> if vertex = a then helper t vertex ((b, w)::acc) else helper t vertex acc in helper g.edges vertex [];; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (w : weight): ('a list * weight) = if (fst node) = b then (visited@[(fst node)], (w+(snd node))) else aux_list (neighbours g (fst node)) (visited@[(fst node)]) (w+(snd node)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w : weight): ('a list * weight) = match nodes with | [] -> raise Fail | (a, b)::t -> if List.mem a visited then aux_list t visited w else try aux_node (a, b) visited w with Fail -> aux_list t visited w in aux_node (a, 0) [] 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc: ('a list * weight)= if (fst node) = b then (visited@[(fst node)], (sc (snd node))) else let sc2 = fun x -> sc (x+(snd node)) in aux_list (neighbours g (fst node)) (visited@[(fst node)]) fc sc2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc: ('a list * weight) = match nodes with | [] -> fc () | (a, b)::t -> if List.mem a visited then aux_list t visited fc sc else let fc2 = fun () -> aux_list t visited fc sc in aux_node (a, b) visited fc2 sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun x -> x);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper paths highest = match paths with | [] -> if highest = ([], 0) then None else Some highest | (a, w)::t -> if w > (snd highest) then helper t (a, w) else helper t highest in helper (find_all_paths g a b) ([], 0);; |
let open_account (initial_pass: passwd) : bank_account = let failure = ref 0 in let cur = ref 0 in let pwd = ref initial_pass in {update_pass = (fun (a: passwd)->(fun (b:passwd) -> if a = !pwd then (pwd := b; failure := 0;) else (incr failure; raise wrong_pass;);)); retrieve = (fun (c: passwd) -> (fun (d: int) -> if !failure >= 5 then raise too_many_failures else( if c = !pwd then (if d>=0 then ( if d <= !cur then (cur := !cur - d;failure := 0;) else raise not_enough_balance) else raise negative_amount) else (incr failure; raise wrong_pass;);))); deposit = (fun (e: passwd) -> (fun (f: int) -> if !failure >= 5 then raise too_many_failures else if (e = !pwd && f>=0) then (cur := !cur + f; failure := 0;) else if f < 0 then raise negative_amount else (incr failure; raise wrong_pass;);)); show_balance = (fun (g: passwd) -> if !failure >= 5 then raise too_many_failures else if g = !pwd then (failure := 0; !cur;) else (incr failure; raise wrong_pass;))} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun y x -> let (v1, v2, w) = x in if v1 = vertex then (v2,w)::y else y) [] 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) (w0: int) (target: 'a): ('a list * weight) = let (n,w) = node in if List.mem n visited then raise Fail else if n == target then (visited@[n],w0+w) else (try aux_list (neighbours g n) (visited@[n]) (w0+w) with Fail -> raise Fail) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w1: int) : ('a list * weight) = match nodes with |[] -> raise Fail |x::y -> (try aux_node x visited w1 b with Fail -> aux_list y visited w1) in aux_node (a,0) [] 0 b;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (w0: int) (target: 'a) fc sc : ('a list * weight)= let (n,w) = node in if List.mem n visited then fc () else if n == target then sc (visited@[n],w0+w) else aux_list (neighbours g n) (visited@[n]) (w0+w) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w1: int) fc sc : ('a list * weight) = match nodes with |[] -> fc() |x::y -> let sc2 = sc in let fc2 = fun () -> aux_list y visited w1 fc sc in aux_node x visited w1 b fc2 sc2 in aux_node (a,0) [] 0 b (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 failure = ref 0 in let cur = ref 0 in let pwd = ref initial_pass in {update_pass = (fun (a: passwd)->(fun (b:passwd) -> if a = !pwd then (pwd := b; failure := 0;) else (incr failure; raise wrong_pass;);)); retrieve = (fun (c: passwd) -> (fun (d: int) -> if !failure >= 5 then raise too_many_failures else( if c = !pwd then (if d>=0 then ( if d <= !cur then (cur := !cur - d;failure := 0;) else raise not_enough_balance) else raise negative_amount) else (incr failure; raise wrong_pass;);))); deposit = (fun (e: passwd) -> (fun (f: int) -> if !failure >= 5 then raise too_many_failures else if (e = !pwd && f>=0) then (cur := !cur + f; failure := 0;) else if f < 0 then raise negative_amount else (incr failure; raise wrong_pass;);)); show_balance = (fun (g: passwd) -> if !failure >= 5 then raise too_many_failures else if g = !pwd then (failure := 0; !cur;) else (incr failure; raise wrong_pass;))} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun y x -> let (v1, v2, w) = x in if v1 = vertex then (v2,w)::y else y) [] 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) (w0: int) (target: 'a): ('a list * weight) = let (n,w) = node in if List.mem n visited then raise Fail else if n == target then (visited@[n],w0+w) else (try aux_list (neighbours g n) (visited@[n]) (w0+w) with Fail -> raise Fail) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w1: int) : ('a list * weight) = match nodes with |[] -> raise Fail |x::y -> (try aux_node x visited w1 b with Fail -> aux_list y visited w1) in aux_node (a,0) [] 0 b;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (w0: int) (target: 'a) fc sc : ('a list * weight)= let (n,w) = node in if List.mem n visited then fc () else if n == target then sc (visited@[n],w0+w) else aux_list (neighbours g n) (visited@[n]) (w0+w) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (w1: int) fc sc : ('a list * weight) = match nodes with |[] -> fc() |x::y -> let sc2 = sc in let fc2 = fun () -> aux_list y visited w1 fc sc in aux_node x visited w1 b fc2 sc2 in aux_node (a,0) [] 0 b (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 password : passwd ref = ref initial_pass in let bal : int ref = ref 0 in let auth_fail : int ref = ref 0 in let locked : bool ref = ref false in { update_pass = (fun (old_p: passwd) (new_p: passwd) -> if (old_p = !password) then (password := new_p; auth_fail := 0; locked := false) else (print_endline (string_of_int !auth_fail); incr auth_fail; if !auth_fail > 4 then locked := true; raise wrong_pass)); retrieve = (fun (p: passwd) (n: int) -> if (p = !password && not !locked) then ((if n < 0 then raise negative_amount else if n <= !bal then bal := (!bal - n) else raise not_enough_balance); auth_fail := 0) else if !locked then raise too_many_failures else (print_endline (string_of_int !auth_fail); incr auth_fail; if !auth_fail > 4 then locked := true; raise wrong_pass)); deposit = (fun (p: passwd) (n: int) -> if (p = !password && not !locked) then ((if n < 0 then raise negative_amount else bal := !bal + n); auth_fail := 0) else if (!locked) then raise too_many_failures else (print_endline (string_of_int !auth_fail); incr auth_fail; if !auth_fail > 4 then locked := true; raise wrong_pass)); show_balance = (fun (p: passwd) : int -> if (p = !password && not !locked) then (auth_fail := 0; !bal) else if (!locked) then raise too_many_failures else (print_endline (string_of_int !auth_fail); incr auth_fail; if !auth_fail > 4 then locked := true; raise wrong_pass)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (fun (_, v2, w) -> (v2, w)) (List.filter (fun (v, _, _) -> v = vertex) g.edges) ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let remove_vis (nodes: ('a * weight) list) (vis:'a list) : ('a * weight) list = let filter (v, _) = not (List.exists (fun v1 -> v1 = v) vis) in List.filter filter nodes in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight)= if fst node = b then ([fst node], snd node) else let l = aux_list (neighbours g (fst node)) visited in ((fst node)::(fst l), snd l + snd node) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match (remove_vis nodes visited) with [] -> raise Fail | x::xs -> try aux_node x ((fst x)::visited) with | Fail -> aux_list xs ((fst x)::visited) in aux_node (a, 0) [a] ;; let remove_visited (nodes: ('a * weight) list) (visited: 'a list) = let filter (v, _) = not (List.exists (fun v1 -> v1 = v) visited) in List.filter filter nodes ;; |
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 node else ( let suc2 = ( fun (n: 'a * weight) -> let k = sc node in ((fst k) @ [fst n], snd k + snd n) ) in aux_list (neighbours g (fst node)) ((fst node)::visited) fc suc2 ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match (remove_visited nodes visited) with [] -> fc () | x::xs -> let fail2 = fun () -> aux_list xs visited fc sc in aux_node x visited fail2 sc in aux_node (a, 0) [a] (fun () -> raise Fail) (fun (x: 'a * weight) -> ([a]), 0) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths: ('a list * weight) list = find_all_paths g a b in if all_paths = [] then None else Some (List.hd (List.sort (fun (_, x) (_, y) -> y - x) all_paths)) ;; |
let open_account (initial_pass: passwd) : bank_account = let password : passwd ref = ref initial_pass in let bal : int ref = ref 0 in let auth_fail : int ref = ref 0 in let locked : bool ref = ref false in { update_pass = (fun (old_p: passwd) (new_p: passwd) -> if (old_p = !password) then (password := new_p; auth_fail := 0; locked := false) else (print_endline (string_of_int !auth_fail); incr auth_fail; if !auth_fail > 4 then locked := true; raise wrong_pass)); retrieve = (fun (p: passwd) (n: int) -> if (p = !password && not !locked) then ((if n < 0 then raise negative_amount else if n <= !bal then bal := (!bal - n) else raise not_enough_balance); auth_fail := 0) else if !locked then raise too_many_failures else (print_endline (string_of_int !auth_fail); incr auth_fail; if !auth_fail > 4 then locked := true; raise wrong_pass)); deposit = (fun (p: passwd) (n: int) -> if (p = !password && not !locked) then ((if n < 0 then raise negative_amount else bal := !bal + n); auth_fail := 0) else if (!locked) then raise too_many_failures else (print_endline (string_of_int !auth_fail); incr auth_fail; if !auth_fail > 4 then locked := true; raise wrong_pass)); show_balance = (fun (p: passwd) : int -> if (p = !password && not !locked) then (auth_fail := 0; !bal) else if (!locked) then raise too_many_failures else (print_endline (string_of_int !auth_fail); incr auth_fail; if !auth_fail > 4 then locked := true; raise wrong_pass)); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (fun (_, v2, w) -> (v2, w)) (List.filter (fun (v, _, _) -> v = vertex) g.edges) ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let remove_vis (nodes: ('a * weight) list) (vis:'a list) : ('a * weight) list = let filter (v, _) = not (List.exists (fun v1 -> v1 = v) vis) in List.filter filter nodes in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight)= if fst node = b then ([fst node], snd node) else let l = aux_list (neighbours g (fst node)) visited in ((fst node)::(fst l), snd l + snd node) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match (remove_vis nodes visited) with [] -> raise Fail | x::xs -> try aux_node x ((fst x)::visited) with | Fail -> aux_list xs ((fst x)::visited) in aux_node (a, 0) [a] ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let remove_vis (nodes: ('a * weight) list) (vis:'a list) : ('a * weight) list = let filter (v, _) = not (List.exists (fun v1 -> v1 = v) vis) in List.filter filter nodes in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if fst node = b then (List.rev ((fst node)::fc), snd node + sc) else aux_list (neighbours g (fst node)) visited ((fst node)::fc) (snd node + sc) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match (remove_vis nodes visited) with [] -> raise Fail | x::xs -> try aux_node x ((fst x)::visited) fc sc with | Fail -> aux_list xs ((fst x)::visited) fc sc in aux_node (a,0) [a] [] 0 ;; |
let find_all_paths (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) list = let paths : ('a list * weight) list ref = ref [] in let remove_vis (nodes: ('a * weight) list) (vis:'a list) : ('a * weight) list = let filter (v, _) = not (List.exists (fun v1 -> v1 = v) vis) in List.filter filter nodes in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc = begin if fst node = b then (paths := !paths @ [(List.rev ((fst node)::fc), snd node + sc)]; ) else aux_list (neighbours g (fst node)) ((fst node)::visited) ((fst node)::fc) (snd node + sc) end and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc = begin match (remove_vis nodes visited) with [] -> () | x::xs -> ((aux_node x (visited) fc sc); aux_list xs (visited) fc sc) end in (try aux_node (a,0) [a] [] 0; with | Fail -> ()); !paths ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let remove_vis (nodes: ('a * weight) list) (vis:'a list) : ('a * weight) list = let filter (v, _) = not (List.exists (fun v1 -> v1 = v) vis) in List.filter filter nodes in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if fst node = b then (List.rev ((fst node)::fc), snd node + sc) else aux_list (neighbours g (fst node)) ((fst node)::visited) ((fst node)::fc) (snd node + sc) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match List.sort (fun (_, x) (_, y) -> y - x) (remove_vis nodes visited) with [] -> raise Fail | x::xs -> try aux_node x ((fst x)::visited) fc sc with | Fail -> aux_list xs (visited) fc sc in try Some (aux_node (a,0) [a] [] 0) with | Fail -> None ;; *);; |
let open_account (initial_pass: passwd) : bank_account = notimplemented () ;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let wrong = ref 0 in let update_pass old_pass new_pass = if old_pass = !password then let _ = wrong := 0 in password := new_pass else match !wrong with | 5 -> raise wrong_pass | _ -> let _ = wrong := ((!wrong) + 1) in raise wrong_pass in let retrieve pass amount = if amount < 0 then raise negative_amount else match !wrong with | 5 -> raise too_many_failures | _ -> if pass = !password then match wrong := 0 with | u when !balance - amount < 0 -> raise not_enough_balance | _ -> balance := !balance - amount else let _ = wrong := ((!wrong) + 1) in raise wrong_pass in let deposit pass amount = if amount < 0 then raise negative_amount else match !wrong with | 5 -> raise too_many_failures | _ -> if pass = !password then match wrong := 0 with | u when !balance + amount < 0 -> raise not_enough_balance | _ -> balance := !balance + amount else let _ = wrong := ((!wrong) + 1) in raise wrong_pass in let show_balance pass = match !wrong with | 5 -> raise too_many_failures | _ -> if pass = !password then let _ = wrong := 0 in !balance else let _ = wrong := ((!wrong) + 1) in raise wrong_pass in {update_pass = update_pass; retrieve = retrieve; deposit = deposit; show_balance = show_balance} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let {nodes; edges} = g in match (nodes, edges) with | ([], []) | (_, []) | ([], _) -> [] | _ -> let choose list (v1, v2, weight) = if vertex = v1 then (v2, weight)::list else list in List.fold_left choose [] 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 (pos, w) = node in if List.mem pos visited then raise Fail else if pos = b then (pos::visited, w) else let (node_list, total) = aux_list (neighbours g pos) (pos::visited) in (node_list, total + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | next::remain -> try aux_node next visited with Fail -> aux_list remain visited in let (path, total_weight) = aux_list (neighbours g a) [a] in (List.rev path, total_weight);; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (fc : unit -> 'a list * weight) (total : weight) : ('a list * weight) = let (pos, w) = node in if pos = b then (b::visited, total + w) else if List.mem pos visited then fc () else match (neighbours g pos) with | [] -> fc () | l -> aux_list l (pos::visited) fc (total + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (fc : unit -> 'a list * weight) (total : weight) : ('a list * weight) = match nodes with | [] -> fc () | next::remain -> aux_node next visited (fun () -> aux_list remain visited fc total) total in let init_fc = fun () -> raise Fail in let (path, total_weight) = aux_list (neighbours g a) [a] init_fc 0 in (List.rev path, total_weight);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let aux (g: 'a graph) (a: 'a) (b: 'a) (init_w: weight) : ('a list * weight) option = let max = ref ([], init_w) in let find_max (path, w) = let (cur_path, cur_max) = !max in if cur_max < w then max := (path, w) else max := !max in match find_all_paths g a b with | [] -> None | all_path -> let _ = List.iter find_max all_path in Some !max in aux g a b 0;; |
let open_account (initial_pass: passwd) : bank_account = notimplemented () ;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let wrong = ref 0 in let update_pass old_pass new_pass = if old_pass = !password then let _ = wrong := 0 in password := new_pass else match !wrong with | 5 -> raise wrong_pass | _ -> let _ = wrong := ((!wrong) + 1) in raise wrong_pass in let retrieve pass amount = if amount < 0 then raise negative_amount else match !wrong with | 5 -> raise too_many_failures | _ -> if pass = !password then match wrong := 0 with | u when !balance - amount < 0 -> raise not_enough_balance | _ -> balance := !balance - amount else let _ = wrong := ((!wrong) + 1) in raise wrong_pass in let deposit pass amount = if amount < 0 then raise negative_amount else match !wrong with | 5 -> raise too_many_failures | _ -> if pass = !password then match wrong := 0 with | u when !balance + amount < 0 -> raise not_enough_balance | _ -> balance := !balance + amount else let _ = wrong := ((!wrong) + 1) in raise wrong_pass in let show_balance pass = match !wrong with | 5 -> raise too_many_failures | _ -> if pass = !password then let _ = wrong := 0 in !balance else let _ = wrong := ((!wrong) + 1) in raise wrong_pass in {update_pass = update_pass; retrieve = retrieve; deposit = deposit; show_balance = show_balance} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let {nodes; edges} = g in match (nodes, edges) with | ([], []) | (_, []) | ([], _) -> [] | _ -> let choose list (v1, v2, weight) = if vertex = v1 then (v2, weight)::list else list in List.fold_left choose [] 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 (pos, w) = node in if List.mem pos visited then raise Fail else if pos = b then (pos::visited, w) else let (node_list, total) = aux_list (neighbours g pos) (pos::visited) in (node_list, total + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | next::remain -> try aux_node next visited with Fail -> aux_list remain visited in let (path, total_weight) = aux_list (neighbours g a) [a] in (List.rev path, total_weight);; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (fc : unit -> 'a list * weight) (total : weight) : ('a list * weight) = let (pos, w) = node in if pos = b then (b::visited, total + w) else if List.mem pos visited then fc () else match (neighbours g pos) with | [] -> fc () | l -> aux_list l (pos::visited) fc (total + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (fc : unit -> 'a list * weight) (total : weight) : ('a list * weight) = match nodes with | [] -> fc () | next::remain -> aux_node next visited (fun () -> aux_list remain visited fc total) total in let init_fc = fun () -> raise Fail in let (path, total_weight) = aux_list (neighbours g a) [a] init_fc 0 in (List.rev path, total_weight);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec find_max (l : ('a list * weight) list) (max : 'a list * weight) : ('a list * weight) = match l with | [] -> max | cur::remain -> let (_, cur_weight) = cur in let (_, max_weight) = max in if cur_weight > max_weight then find_max remain cur else find_max remain max in match find_all_paths g a b with | [] -> None | l -> Some (find_max l ([], 0));; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let passwd = ref initial_pass in let failure = ref 0 in let update_pass old_password new_password = if old_password <> !passwd then (failure := !failure + 1; raise wrong_pass) else (passwd := new_password;failure := 0) in let deposit password n = if !failure >= 5 then (raise too_many_failures) else if !failure = 5 then (raise wrong_pass) else if password <> !passwd then (failure := !failure + 1; raise wrong_pass) else if n < 0 then (raise negative_amount) else (balance := !balance + n; failure := 0) in let retrieve password n = if !failure >= 5 then (raise too_many_failures) else if password <> !passwd then (failure := !failure + 1; raise wrong_pass) else if n < 0 then (raise negative_amount ) else if n > !balance then (raise not_enough_balance ) else (balance := !balance - n; failure := 0) in let show_balance password = if !failure >= 5 then (raise too_many_failures) else if password <> !passwd then (failure := !failure + 1; raise wrong_pass) else (failure := 0; !balance) in {update_pass; deposit; retrieve; show_balance};; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = notimplemented();; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.