text
stringlengths 0
601k
|
---|
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (w,v)=node in if w=b then let (c,d)=sc() in (c@[w],d+v) else if List.mem w visited then fc() else aux_list(neighbours g w)(visited@[w])(fc)(fun()-> let (x,y)=sc() in (x@[w],y+v)) 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()->([],0));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths=find_all_paths(g)(a)(b) in if paths=[] then None else let (t,u)=List.nth paths 0 in let rec pred(paths:('a list * weight) list)(biggest:int)(list_with_biggest:('a list *weight))= match paths with |[]-> list_with_biggest |(a,b)::t -> if b>biggest then pred(t)(b)(a,b) else pred(t)(biggest)(list_with_biggest) in Some (pred(paths)(u)(t,u));; |
let open_account (initial_pass: passwd) : bank_account = let pass= ref initial_pass in let balance= ref 0 in let wrongcount= ref 0 in let resetcount()= wrongcount:=0 in let resetpass(inputpass:passwd)= pass:= inputpass ; wrongcount:= 0 in let negativeinput(amount:int)=if amount<0 then raise negative_amount in let wrongpassword()=if !wrongcount>=5 then raise too_many_failures else (incr(wrongcount) ;raise wrong_pass) in let notenoughbalance(amount:int)(balance:int)=if amount>balance then raise not_enough_balance in {update_pass=(fun(oldpass:passwd)(newpass:passwd) ->if oldpass= !pass then resetpass(newpass) else (incr(wrongcount);raise wrong_pass)); retrieve=(fun(inputpass:passwd)(amount:int)->if !wrongcount>=5 then raise too_many_failures else if inputpass<> !pass then wrongpassword() else resetcount(); negativeinput(amount); notenoughbalance(amount)(!balance); balance:= !balance-amount); deposit=(fun(inputpass:passwd)(amount:int)->if !wrongcount>=5 then raise too_many_failures else if inputpass<> !pass then wrongpassword() else resetcount();negativeinput(amount); balance:= !balance+amount); show_balance=(fun(inputpass:passwd)-> if !wrongcount>=5 then raise too_many_failures else if inputpass<> !pass then wrongpassword() else resetcount();!balance) } ;; let g1={nodes=["a";"b";"c"];edges=[("a","b",4);("a","c",2)]} in let g2={nodes=["a";"b";"c"];edges=[("b","a",4);("a","c",2)]} in let g3={nodes=["a";"b";"c"];edges=[("a","b",4);("a","c",2)]} in let g4={nodes=["q"];edges=[]} in ((g4,"q"),([])); let g5={nodes=["a";"b";"c"];edges=[("a","b",4);("a","c",2);("c","a",2)]} in let g6={nodes=["a";"c"];edges=[("c","a",6)]} in let g7={nodes=[];edges=[]} in let g8={nodes=["a";"b"];edges=[("b","a",7)]} in [ ((g1,"a"),([("b",4);("c",2)])); ((g2,"a"),([("c",2)])); ((g3,"b"),([])); ((g4,"a"),([])); ((g4,"b"),([])); ((g5,"c"),([("a",2)])); ((g6,""),([])); ((g1,""),([])); ((g4,"q"),([])); ((g8,"b"),([("a",7)])); ((g4,"q"),([])); ((g8,"a"),([])) ];; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let pred((a,b,c):'a*'a*weight)= a=vertex in let y=List.filter pred g.edges in let f((a,b,c):'a*'a*weight)=(b,c) in List.map f y;; |
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 (i,j)=node in if List.mem i visited then raise Fail else if i=b then ([i],j) else (let visited=visited@[i] in let (path,total)=aux_list (neighbours g i) (visited) in ([i]@path,total+j)) and aux_list (nodes: ('a * weight) list) (visited: 'a list): ('a list * weight) = match nodes with |[]->raise Fail |node::rest-> try aux_node(node)(visited) with Fail-> aux_list(rest)(visited) in aux_node (a,0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (w,v)=node in if w=b then let (c,d)=sc() in (c@[w],d+v) else if List.mem w visited then fc() else aux_list(neighbours g w)(visited@[w])(fc)(fun()-> let (x,y)=sc() in (x@[w],y+v)) 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()->([],0));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths=find_all_paths(g)(a)(b) in if paths=[] then None else let (t,u)=List.nth paths 0 in let rec pred(paths:('a list * weight) list)(biggest:int)(list_with_biggest:('a list *weight))= match paths with |[]-> list_with_biggest |(a,b)::t -> if b>biggest then pred(t)(b)(a,b) else pred(t)(biggest)(list_with_biggest) in Some (pred(paths)(u)(t,u));; |
let open_account (initial_pass: passwd) : bank_account = let pass= ref initial_pass in let balance= ref 0 in let wrongcount= ref 0 in let resetcount()= wrongcount:=0 in let resetpass(inputpass:passwd)= pass:= inputpass ; wrongcount:= 0 in let negativeinput(amount:int)=if amount<0 then raise negative_amount in let wrongpassword()=if !wrongcount>=5 then raise too_many_failures else (incr(wrongcount) ;raise wrong_pass) in let notenoughbalance(amount:int)(balance:int)=if amount>balance then raise not_enough_balance in {update_pass=(fun(oldpass:passwd)(newpass:passwd) ->if oldpass= !pass then resetpass(newpass) else (incr(wrongcount);raise wrong_pass)); retrieve=(fun(inputpass:passwd)(amount:int)->if !wrongcount>=5 then raise too_many_failures else if inputpass<> !pass then wrongpassword() else resetcount(); negativeinput(amount); notenoughbalance(amount)(!balance); balance:= !balance-amount); deposit=(fun(inputpass:passwd)(amount:int)->if !wrongcount>=5 then raise too_many_failures else if inputpass<> !pass then wrongpassword() else resetcount();negativeinput(amount); balance:= !balance+amount); show_balance=(fun(inputpass:passwd)-> if !wrongcount>=5 then raise too_many_failures else if inputpass<> !pass then wrongpassword() else resetcount();!balance) } ;; let g1={nodes=["a";"b";"c"];edges=[("a","b",4);("a","c",2)]} in let g2={nodes=["a";"b";"c"];edges=[("b","a",4);("a","c",2)]} in let g3={nodes=["a";"b";"c"];edges=[("a","b",4);("a","c",2)]} in let g4={nodes=["q"];edges=[]} in ((g4,"q"),([])); let g5={nodes=["a";"b";"c"];edges=[("a","b",4);("a","c",2);("c","a",2)]} in let g6={nodes=["a";"c"];edges=[("c","a",6)]} in let g7={nodes=[];edges=[]} in let g8={nodes=["a";"b"];edges=[("b","a",7)]} in [ ((g1,"a"),([("b",4);("c",2)])); ((g2,"a"),([("c",2)])); ((g3,"b"),([])); ((g4,"a"),([])); ((g4,"b"),([])); ((g5,"c"),([("a",2)])); ((g6,""),([])); ((g1,""),([])); ((g4,"q"),([])); ((g8,"b"),([("a",7)])); ((g4,"q"),([])); ((g8,"a"),([])) ];; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let pred((a,b,c):'a*'a*weight)= a=vertex in let y=List.filter pred g.edges in let f((a,b,c):'a*'a*weight)=(b,c) in List.map f y;; |
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 (i,j)=node in if List.mem i visited then raise Fail else if i=b then ([i],j) else (let visited=visited@[i] in let (path,total)=aux_list (neighbours g i) (visited) in ([i]@path,total+j)) and aux_list (nodes: ('a * weight) list) (visited: 'a list): ('a list * weight) = match nodes with |[]->raise Fail |node::rest-> try aux_node(node)(visited) with Fail-> aux_list(rest)(visited) in aux_node (a,0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (w,v)=node in if w=b then let (c,d)=sc() in (c@[w],d+v) else if List.mem w visited then fc() else aux_list(neighbours g w)(visited@[w])(fc)(fun()-> let (x,y)=sc() in (x@[w],y+v)) 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()->([],0));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths=find_all_paths(g)(a)(b) in if paths=[] then None else let (t,u)=List.nth paths 0 in let rec pred(paths:('a list * weight) list)(biggest:int)(list_with_biggest:('a list *weight))= match paths with |[]-> list_with_biggest |(a,b)::t -> if b>biggest then pred(t)(b)(a,b) else pred(t)(biggest)(list_with_biggest) in Some (pred(paths)(u)(t,u));; |
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 (initial_pass:passwd) (new_pass:passwd) -> if initial_pass = !pass then (counter := 0; pass := new_pass) else (counter := !counter + 1; raise wrong_pass)); show_balance = (fun (p:passwd) -> if !counter >= 5 then raise too_many_failures else if p <> !pass then (counter := !counter +1; raise wrong_pass) else counter := 0; !balance); retrieve = (fun (p:passwd) amount -> if !counter >= 5 then raise too_many_failures else if p <> !pass then (counter := !counter +1; raise wrong_pass) else if amount < 0 then raise negative_amount else if amount > !balance then raise not_enough_balance else counter := 0; balance := !balance - amount); deposit = (fun (p:passwd) amount -> if !counter >= 5 then raise too_many_failures else if p <> !pass then (counter := !counter +1; raise wrong_pass) else if amount < 0 then raise negative_amount else counter := 0; balance := !balance + amount); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let e = g.edges in let rec n e vertex acc = match e with |[] -> acc |(vert1,vert2,weight) :: e' -> if vert1 = vertex then n e' vertex ((vert2,weight)::acc) else n e' vertex acc in n e vertex [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (x: ('a * weight)) (visited: ('a list * weight))= if fst x = b then ( (fst visited)@[(fst x)] , snd x + (snd visited)) else if List.mem (fst x) (fst visited) then raise Fail else aux_list (neighbours g (fst x)) ((fst visited)@[(fst x)],(snd x) + (snd visited)) and aux_list (nodes: ('a * weight) list) (visited : ('a list * weight)) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try aux_node x visited with Fail -> aux_list xs visited in aux_list (neighbours g a) ([a],0) ;; |
let find_path' g a b= aux_list (neighbours g a) visited (fun () -> raise Fail) (fun r -> r);; *);; |
let find_path' g a b :('a list * weight) = let rec aux_node (node, w) visited (fc: (unit ->'a list * weight)) 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 visited (fc: (unit -> 'a list * weight)) sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> aux_node x visited (fun () -> aux_list xs 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 = if find_all_paths g a b = [] then None else let longest = ref ([a],0) in let rec find_longest p = match p with |[] -> Some !longest |x::xs -> if (snd x) >= snd !longest then (longest := x; find_longest xs) else find_longest xs in find_longest (find_all_paths g a b);; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let password = ref initial_pass in let failed_attempts = ref 0 in let check_pw (pw: passwd) = if !failed_attempts >= 5 then raise too_many_failures else if pw <> !password then ( failed_attempts := (!failed_attempts + 1); raise wrong_pass ) else failed_attempts := 0 in let update_pass (old_pw: passwd) (new_pw: passwd) = if old_pw = !password then ( password := new_pw; failed_attempts := 0 ) else ( failed_attempts := (!failed_attempts + 1); raise wrong_pass ) in let deposit (pw: passwd) (amt: int) = check_pw pw; if amt < 0 then raise negative_amount else balance := (!balance + amt) in let retrieve (pw: passwd) (amt: int) = check_pw pw; if amt < 0 then raise negative_amount else if amt > !balance then raise not_enough_balance else balance := (!balance - amt); in let show_balance (pw: passwd) = check_pw pw; !balance in {update_pass; retrieve; deposit; show_balance} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let out_edges: ('a * 'a * weight) list = List.filter (fun ((v, u, w): ('a * 'a * weight)) -> v = vertex) g.edges in List.map (fun ((v, u, w): ('a * 'a * weight)) -> (u, w)) out_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 let unvisited_adjacent_nodes: ('a * weight) list = List.filter (fun (v, w) -> not (List.mem v visited)) (neighbours g (fst node)) in if unvisited_adjacent_nodes = [] then raise Fail else let return = aux_list unvisited_adjacent_nodes ((fst node)::visited) in ((fst node)::(fst return), (snd node)+(snd return)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try aux_node x visited with Fail -> aux_list xs visited in aux_node (a, 0) [] ;; ([(fst node)], (snd node)) else let unvisited_adjacent_nodes: ('a * weight) list = List.filter (fun (v, w) -> not (List.mem v visited)) (neighbours g (fst node)) in if unvisited_adjacent_nodes = [] then fc () else let return = aux_list unvisited_adjacent_nodes ((fst node)::visited) fc sc in ((fst node)::(fst return), (snd node)+(snd return)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> let fail = fun () -> aux_list xs ((fst x)::visited) fc sc in aux_node x visited fail sc in let fc = fun () -> raise Fail in aux_node (a, 0) [] fc () *);; |
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 unvisited_adjacent_nodes: ('a * weight) list = List.filter (fun (v, w) -> not (List.mem v visited)) (neighbours g (fst node)) in if unvisited_adjacent_nodes = [] then fc () else let succ = fun ((a: 'a), (w: weight)) -> ((fst (sc node))@[a], w+(snd (sc node))) in aux_list unvisited_adjacent_nodes ((fst node)::visited) fc succ and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> let fail = fun () -> aux_list xs (visited) fc sc in aux_node x visited fail sc in let fc = fun () -> raise Fail in let sc = fun ((a: 'a), (w: weight)) -> (a::[], w+0) in aux_node (a, 0) [] fc sc;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let longest (p1: ('a list * weight)) (p2: ('a list * weight)) = if ((snd p1) >= (snd p2)) then p1 else p2 in let paths: ('a list * weight) list = find_all_paths g a b in match paths with | [] -> None | x::xs -> Some (List.fold_left longest x xs) ;; |
let open_account (initial_pass: passwd) : bank_account = let wrong_pwd_counter = ref 0 in let passwd_cur = ref initial_pass in let balance = ref 0 in let check_password pwd = pwd = (!passwd_cur) in let check_amount money = if money < 0 then raise negative_amount in let check_balance money = if money > !balance then raise not_enough_balance in let check_lock_account () = if !wrong_pwd_counter >= 5 then raise too_many_failures in let raise_exn () = (wrong_pwd_counter := !wrong_pwd_counter + 1; if !wrong_pwd_counter <= 5 then raise wrong_pass else raise too_many_failures) in let update_pass = (fun passwd_old passwd_new -> if check_password passwd_old then (passwd_cur := passwd_new; wrong_pwd_counter := 0) else (wrong_pwd_counter := !wrong_pwd_counter + 1; raise wrong_pass)) in let retrieve = (fun pwd money -> check_lock_account (); if check_password pwd then (check_amount money; check_balance money; wrong_pwd_counter := 0; balance := (!balance - money)) else raise_exn ()) in let deposit = (fun pwd money -> check_lock_account (); if check_password pwd then (check_amount money; wrong_pwd_counter := 0; balance := (!balance + money)) else raise_exn ()) in let show_balance = (fun pwd -> check_lock_account (); if check_password pwd then (wrong_pwd_counter := 0; !balance) else raise_exn ()) in {update_pass; retrieve; deposit; show_balance; } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc edge -> match edge with | (target, neighbour, w) when target = vertex -> (neighbour, w) :: acc | _ -> acc ) [] g.edges ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (key, w) = node in if key = b then ([key], w) else if List.mem key visited then raise Fail else let (path, w') = aux_list (neighbours g key) (key :: visited) in (key :: path, w + w') 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 (key, w) = node in if key = b then sc ([key], w) else if List.mem key visited then fc () else aux_list (neighbours g key) (key :: visited) fc (fun (path, w') -> sc (key :: path, w + w')) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd :: tl -> let sc2 = sc in let fc2 = fun () -> aux_list tl visited fc sc in aux_node hd visited fc2 sc2 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 = match find_all_paths g a b with | [] -> None | hd :: tl -> let rec helper cur res = let (_, w) = cur in match res with | [] -> Some cur | h :: t -> let (_, w') = h in let cur' = if w' > w then h else cur in helper cur' t in helper hd tl ;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass and bal = ref 0 and attempts = ref 0 in {update_pass = (fun (old: passwd) (new_ : passwd) -> if !pass = old then (attempts := 0; pass := new_) else (attempts := (!attempts + 1);raise wrong_pass)) ; retrieve = (fun (input_pass: passwd) (amount: int) -> if !attempts >= 5 then raise too_many_failures else if (input_pass = !pass) then (attempts := 0;if amount > !bal then raise not_enough_balance else (if amount < 0 then raise negative_amount else bal := (!bal - amount))) else (attempts := (!attempts + 1); raise wrong_pass )) ; deposit = (fun (input_pass: passwd) (amount: int) -> if (!attempts >= 5) then raise too_many_failures else if (input_pass = !pass) then (attempts := 0;if (amount >= 0) then bal := !bal + amount else raise negative_amount) else ( (attempts := !attempts + 1); raise wrong_pass )) ; show_balance = fun (input_pass: passwd) -> if (!attempts >= 5) then raise too_many_failures else if (input_pass = !pass) then (attempts := 0;!bal ) else ((attempts := !attempts + 1) ; raise wrong_pass ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f (acc: ('a * weight) list) (edges: ('a * 'a * weight) ) : ('a * weight) list = let (v1, v2, w) = edges in (if v1 = vertex then (v2, w) :: acc else acc) in List.fold_left f [] g.edges ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let sum = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () and nextNode (neighbour: ('a * weight) list) (visited: 'a list) : ('a * weight) list = List.filter (fun (n, w) -> not (List.mem n visited)) neighbour in let rec main (cur_node: 'a) (tr_list : ('a * weight) list) (visited : 'a list) (acc: int ): 'a list = if cur_node = b then (sum := acc; [b]) else let visited = visited @ [cur_node] in match tr_list with | [] -> raise Fail | (next_node, w) :: tl -> let next_list = nextNode (neighbours g next_node) visited in try cur_node :: main (next_node) next_list visited (acc + w) with Fail -> main cur_node tl visited acc in ( let init_list = nextNode (neighbours g a) [] in (let _ = main a init_list [] 0 in main a init_list [] 0,!sum) ) ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let sum = ref 0 in 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 () and nextNode (neighbour: ('a * weight) list) (visited: 'a list) : ('a * weight) list = List.filter (fun (n, w) -> not (List.mem n visited)) neighbour in let rec main (cur_node: 'a) (tr_list: ('a * weight) list) (visited: 'a list) fail succ acc = if cur_node = b then (sum := acc; List.append succ [b]) else let visited = visited @ [cur_node] in match tr_list with | [] -> fail () | (next_node, w) :: tl -> let next_list = nextNode (neighbours g next_node) visited and fail2 = fun () -> main (cur_node) tl visited fail succ acc in main (next_node) next_list visited fail2 (List.append succ [cur_node]) (acc + w) in let init_list = nextNode (neighbours g a) [] in let _ = main a init_list [] (fun () -> raise Fail) [] 0 in (main a init_list [] (fun () -> raise Fail) [] 0, !sum) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths g a b in if List.length all_paths = 0 then None else let rec check (l : ('a list * weight) list) (path: 'a list) (cur_weight : weight) : ('a list * weight) option = match l with | [] -> Some (path , cur_weight) | (next_path, next_weight) :: tl -> if next_weight > cur_weight then check tl next_path next_weight else check tl path cur_weight in check all_paths [] 0 ;; |
let makeCounter () : counter = let localcount = ref 0 in { increase = (fun () -> localcount := !localcount + 1; !localcount) ; reset = (fun () -> localcount := 0) ; show = (fun () -> !localcount) } ;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let c = makeCounter () in { update_pass = (fun pass newpass -> if pass <> !password then (c.increase (); raise wrong_pass) else (c.reset (); password := newpass) ) ; retrieve = (fun pass debit -> if pass <> !password then if c.increase () <= 5 then raise wrong_pass ; if c.show () >= 5 then raise too_many_failures ; c.reset () ; if debit < 0 then raise negative_amount else if debit > !balance then raise not_enough_balance else balance := !balance - debit ) ; deposit = (fun pass credit -> if pass <> !password then if c.increase () <= 5 then raise wrong_pass ; if c.show () >= 5 then raise too_many_failures ; c.reset () ; if credit < 0 then raise negative_amount else balance := !balance + credit ) ; show_balance = (fun pass -> if pass <> !password then if c.increase () <= 5 then raise wrong_pass ; if c.show () >= 5 then raise too_many_failures ; c.reset () ; !balance ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f ((a, b, c): ('a * 'a * weight)) (l: ('a * weight) list) = if a = vertex then [b, c] @ l else l in List.fold_right f g.edges [] ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let path_list = ref [a] in let weight_acc = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let temp_path = !path_list in let temp_weight = !weight_acc in let curNode = fst node in let curWeight = snd node in if List.mem curNode visited then raise Fail else if curNode = b then (!path_list @ [b], !weight_acc + curWeight) else try begin path_list:= !path_list @ [curNode] ; weight_acc := !weight_acc + curWeight ; aux_list (neighbours g curNode) (visited @ [curNode]) end with Fail -> path_list := temp_path ; weight_acc := temp_weight ; 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 if a = b then ([], 0) else aux_list (neighbours g a) [a] ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let path_list = ref [a] in let weight_acc = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let temp_path = !path_list in let temp_weight = !weight_acc in let curNode = fst node in let curWeight = snd node in if List.mem curNode visited then fc () else if curNode = b then (!path_list @ [b], !weight_acc + curWeight) else let fc3 = fun () -> begin path_list := temp_path ; weight_acc := temp_weight ; fc () end in begin path_list := !path_list @ [curNode] ; weight_acc := !weight_acc + curWeight; aux_list (neighbours g curNode) (visited @ [curNode]) fc3 sc end and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd :: tl -> let sc2 = sc in let fc2 = fun () -> aux_list tl visited fc sc in aux_node hd visited fc2 sc2 in if a = b then ([], 0) else aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun x -> x) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let outputlist = ref [] in let outputweight = ref 0 in let (l: ('a list * weight) list) = find_all_paths g a b in let rec helper l1 : unit = match l1 with | [] -> () | hd :: tl -> if (snd hd) > !outputweight then begin outputlist := (fst hd) ; outputweight := (snd hd) ; helper tl end else helper tl in helper l ; if !outputweight = 0 then None else Some (!outputlist, !outputweight) ;; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let num_failure = ref 0 in let password = ref initial_pass in { update_pass = (fun old_passwd new_passwd -> if old_passwd = (!password) then (num_failure := 0; password := new_passwd) else (num_failure := (!num_failure) + 1; raise wrong_pass)) ; deposit = (fun input_passwd amount -> if (!num_failure) = 5 then raise too_many_failures else if input_passwd = (!password) then ( if amount < 0 then (num_failure := 0; raise negative_amount) else (num_failure := 0; balance := (!balance) + amount) ) else (num_failure := (!num_failure) + 1; raise wrong_pass )) ; retrieve = (fun input_passwd amount -> if (!num_failure) = 5 then raise too_many_failures else if input_passwd = (!password) then( if amount < 0 then (num_failure := 0; raise negative_amount) else if amount > (!balance) then (num_failure := 0; raise not_enough_balance) else (num_failure := 0; balance := (!balance) - amount) ) else (num_failure := (!num_failure) + 1; raise wrong_pass )) ; show_balance = (fun input_passwd -> if (!num_failure) = 5 then raise too_many_failures else if input_passwd = (!password) then (num_failure := 0; !balance) else (num_failure := (!num_failure) + 1; raise wrong_pass)) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left ( fun output_list (v, neighbour, cost) -> if v = vertex then (neighbour, cost)::output_list else output_list ) [] 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) acc: ('a list * weight) = let (v2, w) = node in if v2 = b then ( List.append visited [v2], w+acc) else if List.mem v2 visited then raise Fail else aux_list (neighbours g v2) (List.append visited [v2]) (w+acc) and aux_list (nodes: ('a * weight) list) (visited: 'a list) acc: ('a list * weight) = match nodes with | [] -> raise Fail | hd::tl -> try aux_node hd visited acc with Fail -> aux_list tl 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) fc sc : ('a list * weight)= let (v2, w2) = node in if v2 = b then sc ([v2], w2) else if List.mem v2 visited then fc () else aux_list (neighbours g v2) (v2::visited) fc (fun (v, w) -> sc (v2::v,w2+w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd::tl -> aux_node hd visited (fun () -> aux_list tl visited fc sc) sc in aux_node (a,0) [] (fun () -> raise Fail) (fun (v,w) -> (v,w)) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths g a b in let rec to_find paths = match paths with | [] -> None | (p1, w1) :: tl -> match tl with | [] -> Some (p1, w1) | (p2, w2) :: tl2 -> if List.length p1 >= List.length p2 then to_find ((p1, w1) ::tl2) else to_find ((p2, w2) ::tl2) in to_find all_paths ;; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let num_failure = ref 0 in let password = ref initial_pass in { update_pass = (fun old_passwd new_passwd -> if old_passwd = (!password) then (num_failure := 0; password := new_passwd) else (num_failure := (!num_failure) + 1; raise wrong_pass)) ; deposit = (fun input_passwd amount -> if (!num_failure) = 5 then raise too_many_failures else if input_passwd = (!password) then ( if amount < 0 then (num_failure := 0; raise negative_amount) else (num_failure := 0; balance := (!balance) + amount) ) else (num_failure := (!num_failure) + 1; raise wrong_pass )) ; retrieve = (fun input_passwd amount -> if (!num_failure) = 5 then raise too_many_failures else if input_passwd = (!password) then( if amount < 0 then (num_failure := 0; raise negative_amount) else if amount > (!balance) then (num_failure := 0; raise not_enough_balance) else (num_failure := 0; balance := (!balance) - amount) ) else (num_failure := (!num_failure) + 1; raise wrong_pass )) ; show_balance = (fun input_passwd -> if (!num_failure) = 5 then raise too_many_failures else if input_passwd = (!password) then (num_failure := 0; !balance) else (num_failure := (!num_failure) + 1; raise wrong_pass)) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left ( fun output_list (v, neighbour, cost) -> if v = vertex then (neighbour, cost)::output_list else output_list ) [] 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) acc: ('a list * weight) = let (v2, w) = node in if v2 = b then ( List.append visited [v2], w+acc) else if List.mem v2 visited then raise Fail else aux_list (neighbours g v2) (List.append visited [v2]) (w+acc) and aux_list (nodes: ('a * weight) list) (visited: 'a list) acc: ('a list * weight) = match nodes with | [] -> raise Fail | hd::tl -> try aux_node hd visited acc with Fail -> aux_list tl 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) fc sc : ('a list * weight)= let (v2, w2) = node in if v2 = b then sc ([v2], w2) else if List.mem v2 visited then fc () else aux_list (neighbours g v2) (v2::visited) fc (fun (v, w) -> sc (v2::v,w2+w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd::tl -> aux_node hd visited (fun () -> aux_list tl visited fc sc) sc in aux_node (a,0) [] (fun () -> raise Fail) (fun (v,w) -> (v,w)) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths g a b in let rec to_find paths = match paths with | [] -> None | (p1, w1) :: tl -> match tl with | [] -> Some (p1, w1) | (p2, w2) :: tl2 -> if List.length p1 > List.length p2 then to_find ((p1, w1) ::tl2) else if List.length p1 < List.length p2 then to_find ((p2, w2) ::tl2) else (if w1 >= w2 then to_find ((p1, w1) ::tl2) else to_find ((p2, w2) ::tl2) ) in to_find all_paths ;; |
let open_account (initial_pass: passwd) : bank_account = let currpass = ref initial_pass and currbal = ref 0 and tries = ref 0 in let checkpass = if !tries >= 5 then raise too_many_failures in {update_pass = (fun oldpass newpass -> if !currpass <> oldpass then begin tries := !tries + 1; raise wrong_pass end else begin tries := 0; currpass:= newpass end ) ; retrieve = (fun pass money -> if !tries >= 5 then raise too_many_failures; if !currpass <> pass then begin tries := !tries + 1; raise wrong_pass end else if money < 0 then begin tries := 0; raise negative_amount end else if !currbal - money < 0 then begin tries := 0; raise not_enough_balance end ; currbal:= (!currbal - money); tries := 0 ); deposit = (fun pass money -> if !tries >= 5 then raise too_many_failures else if !currpass <> pass then begin tries := !tries + 1; raise wrong_pass end else if money < 0 then begin tries := 0; raise negative_amount end else begin tries := 0; currbal:= (!currbal + money) end); show_balance = (fun pass -> if !tries >= 5 then raise too_many_failures else if !currpass <> pass then begin tries := !tries + 1; raise wrong_pass end else begin tries := 0; !currbal end) } let check_pass tries = if !tries >= 5 then raise too_many_failures ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (fun (_,v2,w) -> (v2,w)) (List.filter (fun (v1,_,_) -> vertex = v1) g.edges) let typ1 = {nodes = ["LocA"; "LocB"; "LocC"]; edges = [("LocC", "LocB", 5);("LocA","LocC", 3)]};; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = match node with |(v,w) when (v = b) -> [v], w |(v,w) -> if List.mem v visited then raise Fail else let (ls, w2) = aux_list (neighbours g v) (v::visited) in (v::ls, w+w2) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try aux_node x visited with Fail -> aux_list xs visited in aux_node (a,0) [];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= match node with |(v,w) when (v = b) -> sc [v], w |(v,w) -> if List.mem v visited then raise Fail else let suc2 = sc in let fail2 = fun () -> aux_list (neighbours g v) (v::visited) fc sc in let (l2, w2) = aux_list (neighbours g v) (v::visited) fail2 suc2 in (v::l2, w+w2) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> raise Fail |x::xs -> try aux_node x visited fc sc with Fail -> aux_list xs visited fc sc in aux_node (a,0) [] (fun () -> ([],0)) (fun x-> x);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let m = find_all_paths g a b in let rec max_in_list l = match l with |[] -> None |x::[] -> Some x |(c,r)::xs -> let Some (v,w) = max_in_list xs in if r > w then Some (c,r) else Some (v,w) in max_in_list m;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let wrong = ref 0 in let update_pass ps1 ps2 = if not (ps1 = !pass) then ( wrong := !wrong + 1; raise wrong_pass ) else ( pass := ps2; wrong := 0 ) in let deposit pswd amount = if !wrong >= 5 then raise too_many_failures else if not (pswd = !pass) then ( wrong := !wrong + 1; raise wrong_pass ) else if amount < 0 then ( wrong := 0; raise negative_amount ) else ( wrong := 0; balance := !balance + amount ) in let retrieve pswd amount = if !wrong > 4 then raise too_many_failures else if not (pswd = !pass) then ( wrong := !wrong + 1; raise wrong_pass) else ( wrong := 0; match ((amount < 0), (amount > !balance)) with | (true, false) -> raise negative_amount | (false, true) -> raise not_enough_balance | _ -> balance := !balance - amount ) in let show_balance pswd = if !wrong > 4 then raise too_many_failures else if not (pswd = !pass) then ( wrong := !wrong + 1; raise wrong_pass) else ( wrong := 0; !balance ) in {update_pass; retrieve; deposit; show_balance} ;; let graphTest = {nodes = ["one"; "two"; "three"; "four"; "five"]; edges = [("one", "three", 5); ("one", "two", 1); ("two", "one", 2); ("two", "five", 3); ("three", "five", 4); ("five", "four", 8); ("three", "two", 3)]} in [((graphTest, "one"), [("three", 5); ("two", 1)]); ((graphTest, "two"), [("one", 2); ("five", 3)]); ((graphTest, "three"), [("five", 4); ("two", 3)]); ((graphTest, "four"), []); ((graphTest, "five"), [("four", 8)]) ];; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let helper node acc edge = let (a, b, c) = edge in if a = node then (b, c) :: acc else acc in List.fold_left (helper vertex) [] g.edges ;; 8255;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited: 'a list) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented () ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented () ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented () ;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let pass_attempts = ref 0 in let balance = ref 0 in { update_pass = (fun p1 p2 -> if (p1 = !pass) then (pass_attempts := 0; pass := p2) else (pass_attempts := (!pass_attempts + 1); raise wrong_pass) ); retrieve = (fun p amount -> if (!pass_attempts >= 5) then raise too_many_failures else if (p <> !pass) then (pass_attempts := (!pass_attempts + 1); raise wrong_pass) else (pass_attempts := 0; if (amount < 0) then raise negative_amount else if (amount > !balance) then raise not_enough_balance else balance := (!balance - amount) ) ); deposit = (fun p amount -> if (!pass_attempts >= 5) then raise too_many_failures else if (p <> !pass) then (pass_attempts := (!pass_attempts + 1); raise wrong_pass) else (pass_attempts := 0; if (amount < 0) then raise negative_amount else balance := (!balance + amount) ) ); show_balance = (fun p -> if (!pass_attempts >= 5) then raise too_many_failures else if (p <> !pass) then (pass_attempts := (!pass_attempts + 1); raise wrong_pass) else (pass_attempts := 0; !balance) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let out_edges = List.filter (fun (x, _, _) -> x = vertex) g.edges in List.map (fun (_, o, w) -> (o, w)) out_edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let total_weight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let _ = (total_weight := !total_weight + (snd node)) in let out_nodes = neighbours g (fst node) in let filtered = List.filter (fun n -> not (List.exists (fun m -> m = (fst n)) visited)) out_nodes in aux_list filtered visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = try if (List.length nodes = 0) then raise Fail else let node = List.hd nodes in if (fst node = b) then (visited @ [fst node], !total_weight + snd node) else aux_node node (visited @ [fst node]) with Fail -> if (List.length visited = 0 || List.length nodes = 0) then raise Fail else let _ = (total_weight := !total_weight - (snd (List.hd nodes))) in if (List.length nodes = 1) then raise Fail else aux_list (List.tl nodes) visited in aux_list (neighbours g a) [a];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let total_weight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let _ = (total_weight := !total_weight + (snd node)) in let out_nodes = neighbours g (fst node) in let filtered = List.filter (fun n -> not (List.exists (fun m -> m = (fst n)) visited)) out_nodes in aux_list filtered visited fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = let sc2 = sc in let fc2 = fun () -> ( if (List.length visited = 0 || List.length nodes = 0) then raise Fail else let _ = (total_weight := !total_weight - (snd (List.hd nodes))) in if (List.length nodes = 1) then raise Fail else aux_list (List.tl nodes) visited fc sc) in if (List.length nodes = 0) then raise Fail else let node = List.hd nodes in if (fst node = b) then (visited @ [fst node], !total_weight + snd node) else aux_node node (visited @ [fst node]) fc2 sc2 in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun l -> l);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths g a b in if (List.length all_paths > 0) then let rec max_path max_edge others = match others with | [] -> Some max_edge | (edges, w)::other_edges -> let (_, weight) = max_edge in if (w > weight) then max_path (edges, w) other_edges else max_path max_edge other_edges in max_path (List.hd all_paths) (List.tl all_paths) else None;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let pass_attempts = ref 0 in let balance = ref 0 in { update_pass = (fun p1 p2 -> if (p1 = !pass) then (pass_attempts := 0; pass := p2) else (pass_attempts := (!pass_attempts + 1); raise wrong_pass) ); retrieve = (fun p amount -> if (!pass_attempts >= 5) then raise too_many_failures else if (p <> !pass) then (pass_attempts := (!pass_attempts + 1); raise wrong_pass) else (pass_attempts := 0; if (amount < 0) then raise negative_amount else if (amount > !balance) then raise not_enough_balance else balance := (!balance - amount) ) ); deposit = (fun p amount -> if (!pass_attempts >= 5) then raise too_many_failures else if (p <> !pass) then (pass_attempts := (!pass_attempts + 1); raise wrong_pass) else (pass_attempts := 0; if (amount < 0) then raise negative_amount else balance := (!balance + amount) ) ); show_balance = (fun p -> if (!pass_attempts >= 5) then raise too_many_failures else if (p <> !pass) then (pass_attempts := (!pass_attempts + 1); raise wrong_pass) else (pass_attempts := 0; !balance) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let out_edges = List.filter (fun (x, _, _) -> x = vertex) g.edges in List.map (fun (_, o, w) -> (o, w)) out_edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let total_weight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let _ = (total_weight := !total_weight + (snd node)) in let out_nodes = neighbours g (fst node) in let filtered = List.filter (fun n -> not (List.exists (fun m -> m = (fst n)) visited)) out_nodes in aux_list filtered visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = try if (List.length nodes = 0) then raise Fail else let node = List.hd nodes in if (fst node = b) then (visited @ [fst node], !total_weight + snd node) else aux_node node (visited @ [fst node]) with Fail -> if (List.length visited = 0 || List.length nodes = 0) then raise Fail else let _ = (total_weight := !total_weight - (snd (List.hd nodes))) in if (List.length nodes = 1) then raise Fail else aux_list (List.tl nodes) visited in aux_list (neighbours g a) [a];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let total_weight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let _ = (total_weight := !total_weight + (snd node)) in let out_nodes = neighbours g (fst node) in let filtered = List.filter (fun n -> not (List.exists (fun m -> m = (fst n)) visited)) out_nodes in aux_list filtered visited fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = let sc2 = sc in let fc2 = fun () -> ( if (List.length visited = 0 || List.length nodes = 0) then raise Fail else let _ = (total_weight := !total_weight - (snd (List.hd nodes))) in if (List.length nodes = 1) then raise Fail else aux_list (List.tl nodes) visited fc sc) in if (List.length nodes = 0) then fc2 () else let node = List.hd nodes in if (fst node = b) then (visited @ [fst node], !total_weight + snd node) else aux_node node (visited @ [fst node]) fc2 sc2 in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun l -> l);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths g a b in if (List.length all_paths > 0) then let rec max_path max_edge others = match others with | [] -> Some max_edge | (edges, w)::other_edges -> let (_, weight) = max_edge in if (w > weight) then max_path (edges, w) other_edges else max_path max_edge other_edges in max_path (List.hd all_paths) (List.tl all_paths) else None;; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let counter = ref 0 in let lock_account = if (!counter >= 5) then raise too_many_failures in let update_pass (old_pass: passwd) (new_pass: passwd) = match old_pass with | initial_pass -> let initial_pass = new_pass in () | _ -> raise wrong_pass in let retrieve (pass: passwd) (amnt : int) = if (pass = initial_pass) then let x = !counter in counter := 0; match amnt with | amnt when (amnt < 0) -> raise negative_amount | amnt when (amnt > !balance) -> raise not_enough_balance | _ -> let y = !balance in balance := y - amnt else let x = !counter in counter := x + 1; if (!counter >= 5) then lock_account else raise wrong_pass in let deposit (pass: passwd) (amnt : int) = if (pass = initial_pass) then let x = !counter in counter := 0; match amnt with | amnt when (amnt < 0) -> raise negative_amount | _ -> let y = !balance in balance := y + amnt; else let x = !counter in counter := x + 1; if (!counter >= 5) then lock_account else raise wrong_pass in let show_balance (pass: passwd) = match pass with | inital_pass -> let x = !counter in counter := 0; !balance | _ -> let x = !counter in counter := x + 1; if (!counter >= 5) then raise too_many_failures else raise wrong_pass in {update_pass; retrieve; deposit; show_balance} ;; |
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) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let curr_pass = ref initial_pass in let tries = ref 0 in { update_pass = (fun pass p -> if pass = !curr_pass then (tries := 0;curr_pass := p) else (tries:= !tries + 1;raise wrong_pass) ); retrieve = (fun pass d -> if !tries > 4 then raise too_many_failures else if pass = !curr_pass then if d > (-1) then if d <= !balance then (tries := 0;balance:= !balance - d ) else raise not_enough_balance else raise negative_amount else (tries:= !tries + 1;raise wrong_pass)); deposit = (fun pass i -> if !tries > 4 then raise too_many_failures else if pass = !curr_pass then if i >= 0 then (tries := 0;balance := !balance + i ) else raise negative_amount else (tries:= !tries + 1 ;raise wrong_pass)); show_balance = (fun pass -> if !tries > 4 then raise too_many_failures else if pass = !curr_pass then (tries:= 0; !balance) else (tries := !tries + 1;raise wrong_pass)) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left( fun acc (v1, v2, w) -> if v1 = vertex then (v2, w)::acc else acc ) [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = match node with | (v,w) when v = b -> (visited@[v], w) | (v,_) when List.mem v visited -> raise Fail | (v,w) -> aux_list (List.map (fun (x,c) -> (x,c+w)) (neighbours g v)) (visited@[v]) 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_list (neighbours g a) [a];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc = match node with | (v,w) when v = b -> sc (visited@[v], w) | (v,_) when List.mem v visited -> fc () | (v,w) -> aux_list (List.map (fun (x,c) -> (x,c+w)) (neighbours g v)) (visited@[v]) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc = match nodes with | [] -> fc () | h::t -> aux_node h visited (fun () -> aux_list t visited fc sc) sc in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun (visited,w) -> (visited,w));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = List.fold_left( fun acc (p,c) -> match acc with | None -> Some (p,c) | Some ((pl,cl)) when c > cl -> Some (p,c) | Some (pl,cl) -> Some (pl,cl) ) None (find_all_paths g a b);; |
let open_account (initial_pass: passwd) : bank_account = let attempt = ref 0 in let balance = ref 0 in let password = ref initial_pass in { update_pass = (fun pi pf -> if (pi <> !password) then (attempt := !attempt +1; raise wrong_pass) else password := pf; attempt := 0; ); retrieve = (fun p amount -> if (!attempt >= 5 ) then (raise too_many_failures) else if (p <> !password) then (attempt := !attempt +1; raise wrong_pass); if (amount < 0 ) then (attempt := 0;raise negative_amount) else if ((!balance - amount) < 0) then (attempt := 0;raise not_enough_balance) else (balance := !balance - amount;attempt := 0); ); deposit = (fun p amount -> if (!attempt >= 5 ) then (raise too_many_failures) else if (p <> !password) then (attempt := !attempt +1; raise wrong_pass) else if (amount < 0 ) then (attempt := 0;raise negative_amount) else (balance := !balance + amount;attempt := 0); ); show_balance = fun p -> if (!attempt >= 5 ) then (raise too_many_failures) else if (p <> !password) then (attempt := !attempt +1; raise wrong_pass) else attempt := 0;!balance; } ;; let g = {nodes = ["a"; "b"; "c"; "d"]; edges = [("a","b", 1);("a","c", 1);("c","d", 1)]} in g,"a"), [("b",1);("c",1)]); ((let g1 = {nodes = ["a"; "b"; "c"; "d"]; edges = [("a","b", 1);("a","c", 1);("c","d", 1)]} in g1,"b"), []); ((let g2 = {nodes = ["a"; "b"; "c"; "d"]; edges = [("a","b", 1);("a","c", 1);("c","d", 1)]} in g2,"c"), [("d",1)]); ];; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let e = g.edges in let af_filter = List.filter (fun x-> let (a,_,_) = x in a = vertex )e in List.map (fun x -> let (_,b,w) = x in (b, w)) af_filter;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let weight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = match neighbours g (fst(node)) with |[] -> (if (fst(node) <> b) then raise Fail else (visited, !weight)) |x::xs -> if (fst(x) = b) then (visited @ [fst(x)], !weight+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs visited else try weight := !weight+ snd(x); aux_node x (visited @ [fst(x)]) with Fail -> weight := !weight- snd(x); aux_list xs visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = if (a = b) then (visited,!weight) else ( match nodes with |[]-> raise Fail |x::xs -> if (fst(x) = b) then (visited @ [fst(x)], !weight+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs visited else try weight := !weight+ snd(x); aux_node x (visited @ [fst(x)]) with Fail -> weight := !weight- snd(x); aux_list xs visited ) in aux_list (neighbours g a) [a];; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let weight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= match neighbours g (fst(node)) with |[] -> (if (fst(node) <> b) then fc raise Fail else (sc [], !weight)) |x::xs -> if (fst(x) = b) then (sc [] @ [fst(x)], !weight+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs visited fc sc else try weight := !weight+ snd(x); aux_node x (visited @ [fst(x)]) fc (fun r -> sc ((fst x) :: r)) with Fail -> weight := !weight- snd(x); aux_list xs visited fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = if (a = b) then (sc [],!weight) else ( match nodes with |[]-> fc raise Fail |x::xs -> if (fst(x) = b) then (sc [] @ [fst(x)] , !weight+snd(x)) else if (List.mem (fst(x)) visited) then aux_list xs visited fc sc else try weight := !weight+ snd(x); aux_node x (visited @ [fst(x)]) fc (fun r -> sc ((fst x) :: r)) with Fail -> weight := !weight- snd(x); aux_list xs visited fc sc) in aux_list (neighbours g a) [a] (fun x-> x) (fun r-> a :: r);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in let max a b = if (a>b) then a else b in let rec list_max p = match p with | [] -> None | x :: xs -> match list_max xs with | None -> Some x | Some xxs -> Some (max x xxs) in list_max paths;; |
let open_account (initial_pass: passwd) : bank_account = let account = ref (initial_pass, 0, 0) in { update_pass = (fun old_pass new_pass -> let (pass, balance, failures_count) = !account in if old_pass = pass then account := (new_pass, balance, 0) else (account := (pass, balance, (failures_count + 1)); raise wrong_pass) ); retrieve = (fun input_pass amount -> let (pass, balance, failures_count) = !account in if failures_count > 4 then raise too_many_failures else if pass <> input_pass then (account := (pass, balance, (failures_count + 1)); raise wrong_pass); account := (pass, balance, 0); if balance < amount then raise not_enough_balance else if amount < 0 then raise negative_amount else account := (pass, (balance - amount), 0) ); deposit = (fun input_pass amount -> let (pass, balance, failures_count) = !account in if failures_count > 4 then raise too_many_failures else if pass <> input_pass then (account := (pass, balance, (failures_count + 1)); raise wrong_pass) else if amount < 0 then (account := (pass, balance, 0); raise negative_amount) else account := (pass, (balance + amount), 0) ); show_balance = (fun input_pass -> let (pass, balance, failures_count) = !account in if failures_count > 4 then raise too_many_failures else if pass = input_pass then (account := (pass, balance, 0); balance) else (account := (pass, balance, (failures_count + 1)); raise wrong_pass) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun edge acc -> let (curr_node, dest_node, curr_weight) = edge in if curr_node = vertex then List.append acc [(dest_node, curr_weight)] 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) w_total : ('a list * weight) = if List.mem (fst(node)) visited then raise Fail else if fst(node) = b then ((visited @ [fst(node)]), (w_total + snd(node))) else aux_list (neighbours g (fst(node))) (visited @ [fst(node)]) (w_total + snd(node)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) w_total: ('a list * weight) = match nodes with | [] -> raise Fail | (c_a, c_weight) :: xs -> try aux_node (c_a, c_weight) visited w_total with Fail -> aux_list xs visited w_total in aux_node (a,0) [] 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) w_total fc sc : ('a list * weight)= if fst(node) = b then sc(((visited @ [fst(node)]), (w_total + snd(node)))) else if List.mem (fst(node)) visited then fc () else aux_list (neighbours g (fst(node))) (visited @ [fst(node)]) (w_total + snd(node)) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) w_total fc sc : ('a list * weight) = match nodes with | [] -> fc () | (c_a, c_weight) :: xs -> let sc2 = sc in let fc2 = fun () -> aux_list xs visited w_total fc sc in aux_node (c_a, c_weight) visited w_total fc2 sc2 in aux_node (a,0) [] 0 (fun () -> (raise Fail)) (fun answer -> answer);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec calc_longest_path (all_paths: ('a list * weight) list) (max: ('a list * weight)) = match all_paths with |[] -> if max = ([], 0) then None else Some max |x::xs -> if snd(x) > snd(max) then calc_longest_path xs x else calc_longest_path xs max in calc_longest_path (find_all_paths g a b) ([], 0);; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let password = ref initial_pass in let counter = ref 0 in { update_pass = (fun old_pass -> fun new_pass -> if old_pass = !password then let _ = (counter := 0) in password := new_pass else let _ = (counter := !counter +1) in raise wrong_pass ) ; deposit = (fun pass -> fun money -> if !counter >=5 then raise too_many_failures else if pass = !password then let _ = (counter := 0) in if money<0 then raise negative_amount else balance := !balance+money else let _ = (counter := !counter +1) in raise wrong_pass ); retrieve = (fun pass -> fun money -> if !counter >=5 then raise too_many_failures else if pass = !password then let _ = (counter := 0) in if money < 0 then raise negative_amount else if !balance >= money then balance := (!balance-money) else raise not_enough_balance else let _ = (counter := !counter +1) in raise wrong_pass ); show_balance = (fun pass -> if !counter >=5 then raise too_many_failures else if pass = !password then let _ = (counter := 0) in !balance else let _ = (counter := !counter +1) in raise wrong_pass ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let v1 (a,b,c) = a in let v2 (a,b,c) = b in let v3 (a,b,c) = c in List.fold_left (fun x y -> if not (List.exists (fun z -> z = vertex) g.nodes) then [] else if vertex = (v1 y) then (v2 y, v3 y)::x else x ) [] g.edges ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let path = ref [a] in let w = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if (fst node) = b then (!path, !w) else let n = neighbours g (fst node) in aux_list n ((fst node)::visited) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | hd :: tl -> if (List.exists (fun z -> z = (fst hd)) visited) then aux_list tl visited else let new_visited = (fst hd)::visited in let _ = w:=(!w + (snd hd)) in let old_path = !path in let _ = path:=!path@[fst hd] in try aux_node hd visited with Fail -> let _ = w:=(!w - (snd hd)) in let _ = path:=old_path in aux_list tl new_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 ([], 0) else let n = neighbours g (fst node) in aux_list n ((fst node)::visited) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd :: tl -> if (List.exists (fun z -> z = (fst hd)) visited) then aux_list tl visited fc sc else let fc2 = fun() -> aux_list tl visited fc sc in let sc2 = fun (r1, r2) -> sc (((fst hd)::r1), (r2+(snd hd))) in aux_node hd visited fc2 sc2 in aux_node (a,0) [] (fun() -> raise Fail) (fun (x,y) -> (a::x, y));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec find_max list = match list with | [] -> 0 | hd::tl -> max (snd hd) (find_max tl) in let rec find_list list w = match list with | [] -> None | hd::tl -> if (snd hd) = w then Some hd else find_list tl w in let l = find_all_paths g a b in find_list l (find_max l) ;; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let password = ref initial_pass in let failures = ref 0 in let verifypass pass = (if pass = !password then (failures:=0; true) else (failures:=!failures+1; false)) in let exceededattempts() = (!failures)>=5 in { update_pass = (fun oldpass newpass -> if verifypass oldpass then password := newpass else raise wrong_pass); retrieve = (fun pass amount -> if exceededattempts() then raise too_many_failures else if amount<0 then raise negative_amount else( if verifypass pass then ( if !balance >= amount then balance := !balance-amount else raise not_enough_balance) else raise wrong_pass)); deposit = (fun pass amount -> if exceededattempts() then raise too_many_failures else if verifypass pass then( if amount<0 then raise negative_amount else balance := !balance+amount ) else raise wrong_pass ) ; show_balance = (fun pass -> if exceededattempts() then raise too_many_failures else if verifypass pass then !balance else raise wrong_pass); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec findneighbour edges = match edges with | [] -> [] | (s,t,w) :: xs -> if s = vertex then (t,w)::(findneighbour xs) else findneighbour xs in findneighbour 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 ([b],0) else aux_list (neighbours g (fst node)) ((fst node)::visited) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> ([],0) | (node,weight)::xs -> if (List.mem node visited) then aux_list xs visited else let (path, w) = aux_node (node,weight) visited in if path = [] then aux_list xs visited else (node::path,weight+w) in let (path,weight) = aux_node (a,0) [] in let rec removelast =fun l-> match l with | [x] -> [] | x::xs -> x::(removelast xs) | [] -> [] in if path = [] then raise Fail else (removelast (a::path),weight) ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight)(visited : 'a list) fc sc : ('a list * weight)= let (current,w)= node in if (current = b) then sc current ([b],w) else aux_list (neighbours g current) current (current::visited) fc ( fun curr (path,weight)->if path = [] then ([],0) else sc current (current::path,weight+w)) and aux_list (nodes: ('a * weight) list) parent (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc parent ([],0) | (node,w)::xs -> if (List.mem node visited) then aux_list xs parent visited fc sc else aux_node (node,w) visited (fun current (path,weight)-> aux_list xs parent visited fc sc) sc in aux_node (a,0) [] (fun current (path,weight)-> if (path = [])then raise Fail else (path,weight)) (fun current (path, weight) -> if (path = [] && current!=a) then ([],0) else (path,weight));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in let rec longest = fun l -> match l with | [] -> ([],0) | (path,weight)::xs -> let (longestpath, longestcost) = longest xs in if (weight >longestcost) then (path,weight) else (longestpath,longestcost) in let (longestpath, longestcost) = longest paths in if (longestpath = []) then None else Some (longestpath, longestcost);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let wrong_pass_count = ref 0 in let locked = ref false in { update_pass = (fun oldpass newpass -> if oldpass = !password then (locked := false; wrong_pass_count := 0; password := newpass) else (wrong_pass_count := !wrong_pass_count + 1; raise wrong_pass)); deposit = (fun pass amount -> if !wrong_pass_count >= 5 then (locked := true; raise too_many_failures )else if (pass = !password && not !locked )then (wrong_pass_count:= 0; if amount < 0 then raise negative_amount else balance := !balance + amount) else (wrong_pass_count := !wrong_pass_count + 1; raise wrong_pass)) ; retrieve = (fun pass amount -> if !wrong_pass_count >= 5 then (locked := true; raise too_many_failures) else if (pass = !password && not !locked ) then (wrong_pass_count := 0; if amount < 0 then raise negative_amount else if !balance >= amount then balance := !balance - amount else raise not_enough_balance) else (wrong_pass_count := !wrong_pass_count + 1; raise wrong_pass)) ; show_balance = (fun pass -> if !wrong_pass_count >= 5 then (locked := true; raise too_many_failures) else if (pass = !password && not !locked ) then (!balance) else (wrong_pass_count := !wrong_pass_count + 1; raise wrong_pass)) } ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.