text
stringlengths 0
601k
|
---|
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 all_paths = [] then None else let rec helper l path_max w_max = match l with | head :: rest -> (let p,w = head in if w > w_max then helper rest p w else helper rest path_max w_max) | [] -> Some (path_max, w_max) in helper all_paths [] 0;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let remain = ref 5 in if !remain > 0 then { update_pass = (fun a (new_pass:passwd) -> if a = !pass then pass := new_pass else raise wrong_pass; decr remain); retrieve = (fun a (x:int) -> if a = !pass then if x>=0 then if !balance >= x then balance := (!balance - x) else raise not_enough_balance else raise negative_amount else raise wrong_pass; decr remain); deposit = (fun a (y:int) -> if a = !pass then if y>=0 then balance := (!balance + y) else raise negative_amount else raise wrong_pass;decr remain); show_balance = (fun a -> if a = !pass then !balance else raise wrong_pass); } else raise too_many_failures ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let check (result: ('a * weight) list) (m: 'a * 'a * weight) = let (a,b,c) = m in if a = vertex then (b,c)::result else result in List.fold_left check [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (cost:int): ('a list * weight) = let (n, w) = node in if n = b then (List.append visited [n], cost+w) else if neighbours g n = [] then raise Fail else if List.exists (fun x -> x= n) visited then raise Fail else aux_list (neighbours g n) (List.append visited [n]) (cost+w); and aux_list (nodes: ('a * weight) list) (visited: 'a list) (cost:int): ('a list * weight) = match nodes with | [] -> raise Fail | node::rest_nodes -> try aux_node node visited cost with Fail -> aux_list rest_nodes visited cost in aux_node (a,0) [] 0;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (cost:int) fc sc : ('a list * weight)= let (n, w) = node in if n = b then sc visited n cost w else if neighbours g n = [] then fc () else if List.exists (fun x -> x= n) visited then fc () else aux_list (neighbours g n) (List.append visited [n]) (cost+w) fc sc; and aux_list (nodes: ('a * weight) list) (visited: 'a list) (cost:int) fc sc : ('a list * weight) = match nodes with | [] -> fc () | node :: rest_nodes -> let suc2 = sc in let fail2 = fun () -> aux_list (rest_nodes) visited cost fc sc in aux_node node visited cost fail2 suc2 in aux_node (a,0) [] 0 (fun () -> raise Fail) (fun v node c w -> (List.append v [node], c+w));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let path_list = (find_all_paths g a b) in if path_list=[] then None else let compare = fun path1 path2 -> let (_,c)=path1 in let (_,d)=path2 in if c<=d then path2 else path1 in let result = (List.fold_left compare ([], 0) path_list) in Some result;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let failed_attempts = ref 0 in { update_pass = (fun (old_pass : passwd)(new_pass : passwd) -> if (old_pass = !pass) then (pass := new_pass ; failed_attempts := 0 ) else (failed_attempts := !failed_attempts + 1 ; raise wrong_pass ) ) ; deposit = (fun (p : passwd)(dep : int) -> if !failed_attempts >= 5 then raise too_many_failures else (if p = !pass then (failed_attempts := 0 ; if dep >= 0 then balance := !balance + dep else raise negative_amount) else (failed_attempts := !failed_attempts + 1 ; raise wrong_pass) ) ) ; retrieve = (fun (p : passwd)(amt : int) -> if !failed_attempts >= 5 then raise too_many_failures else (if p = !pass then (failed_attempts := 0 ; if amt >= 0 then (if !balance - amt >= 0 then balance := !balance - amt else raise not_enough_balance) else raise negative_amount) else (failed_attempts := !failed_attempts + 1 ; raise wrong_pass) ) ) ; show_balance = (fun (p : passwd) -> if !failed_attempts >= 5 then raise too_many_failures else (if p = !pass then (failed_attempts := 0 ; !balance) else (failed_attempts := !failed_attempts + 1 ; raise wrong_pass)) ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let is_neighbor (v1, v2, d) = (v1 = vertex) in let get_neighbor (v1, v2, d) = (v2, d) in List.map (get_neighbor)(List.filter is_neighbor g.edges);; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = notimplemented ();; |
let find_all_paths (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) list = let rec get_path_weight lst = let get_edge x y = List.hd (List.filter (fun (v1, v2, _) -> v1 = x && v2 = y) g.edges) in let get_edge_weight (edge : (string * string * weight)) = match edge with | (_, _, w) -> w in match lst with [] | [_] -> 0 | x::y::tl -> (get_edge_weight (get_edge x y) + get_path_weight(y::tl)) in let rec find_all_rec (visited) (current: 'a) = if current = b then ([List.rev (current::visited)]) else let neighbors_to_check = List.map (fst)(List.filter (fun (v, w) -> (not(List.mem v visited))) (neighbours g current)) in List.fold_left (@) [] (List.map (find_all_rec (current::visited)) neighbors_to_check) in List.map (fun l -> (l , get_path_weight l))(find_all_rec [] a);; |
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 longest = ref ([],0) in let get_weight (path : 'a list * weight) = match path with | (l , w) -> w in List.iter (fun p -> if (get_weight p) > get_weight(!longest) then longest := (p)) all_paths ; Some !longest;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let counter = ref 0 in let acc = { update_pass = (fun p1 p2 -> if p1 = !password then (counter:=0;password:=p2) else (counter:= !counter + 1; raise wrong_pass) ); retrieve = (fun p i -> if !counter < 5 then if p = !password then if i <= !balance && i >= 0 then (counter:=0; balance:=!balance-i) else if i > !balance then (counter:=0; raise not_enough_balance) else (counter:=0; raise negative_amount) else (counter:= !counter + 1; raise wrong_pass) else raise too_many_failures ); deposit = (fun p i -> if !counter<5 then if p = !password then if i >= 0 then (counter:=0; balance:=!balance+i) else (counter:=0; raise negative_amount) else (counter:= !counter+1;raise wrong_pass) else raise too_many_failures ); show_balance = (fun p -> if !counter<5 then if p = !password then (counter:=0;!balance) else (counter:= !counter+1; raise wrong_pass) else raise too_many_failures ); } in acc ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let out_neighbours = [] in let edges = g.edges in let get_v1 (x: ('a * 'a * weight)) = let (v1,_,_) = x in v1 in let get_v2_w (x: ('a * 'a * weight)) = let (_,v2,w) = x in (v2,w) in let rec filter (e: ('a * 'a * weight) list) (v: 'a) (acc : ('a * weight) list) = match e with | [] -> acc | x::xs -> let v1 = get_v1 x in let n = get_v2_w x in if v1 = v then filter xs v (n::acc) else filter xs v acc in filter edges vertex out_neighbours ;; |
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 a1 = fst(node) in let w1 = snd(node) in if a1 = b then ([a1],w1) else if List.mem a1 visited then raise Fail else let (path, w2) = aux_list (neighbours g a1) (a1::visited) in (a1::path,w1+w2) 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 a1 = fst(node) in let w1 = snd(node) in if a1 = b then sc ([a1],w1) else if List.mem a1 visited then fc () else aux_list (neighbours g a1) (a1::visited) fc (fun (path, cost) -> sc (a1::path, cost+w1)) 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 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 let rec path_max paths = match paths with | [] -> None | x :: xs -> let w = snd(x) in begin match path_max xs with | None -> Some x | Some m -> let w1 = snd(m) in if w < w1 then Some m else Some x end in path_max all_paths ;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let counter = ref 0 in let acc = { update_pass = (fun p1 p2 -> if p1 = !password then (counter:=0;password:=p2) else (counter:= !counter + 1; raise wrong_pass) ); retrieve = (fun p i -> if !counter < 5 then if p = !password then if i <= !balance && i >= 0 then (counter:=0; balance:=!balance-i) else if i > !balance then (counter:=0; raise not_enough_balance) else (counter:=0; raise negative_amount) else (counter:= !counter + 1; raise wrong_pass) else raise too_many_failures ); deposit = (fun p i -> if !counter<5 then if p = !password then if i >= 0 then (counter:=0; balance:=!balance+i) else (counter:=0; raise negative_amount) else (counter:= !counter+1;raise wrong_pass) else raise too_many_failures ); show_balance = (fun p -> if !counter<5 then if p = !password then (counter:=0;!balance) else (counter:= !counter+1; raise wrong_pass) else raise too_many_failures ); } in acc ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let out_neighbours = [] in let edges = g.edges in let get_v1 (x: ('a * 'a * weight)) = let (v1,_,_) = x in v1 in let get_v2_w (x: ('a * 'a * weight)) = let (_,v2,w) = x in (v2,w) in let rec filter (e: ('a * 'a * weight) list) (v: 'a) (acc : ('a * weight) list) = match e with | [] -> acc | x::xs -> let v1 = get_v1 x in let n = get_v2_w x in if v1 = v then filter xs v (n::acc) else filter xs v acc in filter edges vertex out_neighbours ;; |
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 a1 = fst(node) in let w1 = snd(node) in if a1 = b then ([a1],w1) else if List.mem a1 visited then raise Fail else let (path, w2) = aux_list (neighbours g a1) (a1::visited) in (a1::path,w1+w2) 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 a1 = fst(node) in let w1 = snd(node) in if a1 = b then sc ([a1],w1) else if List.mem a1 visited then fc () else aux_list (neighbours g a1) (a1::visited) fc (fun (path, cost) -> sc (a1::path, cost+w1)) 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 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 let rec path_max paths = match paths with | [] -> None | x :: xs -> let w = snd(x) in begin match path_max xs with | None -> Some x | Some m -> let w1 = snd(m) in if w < w1 then Some m else Some x end in path_max all_paths ;; |
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 and count = ref 0 and pass = ref initial_pass in { update_pass = (fun o_pwd n_pwd -> if o_pwd <> !pass then (count := !count + 1; raise wrong_pass) else (pass := n_pwd; count := 0)) ; retrieve = (fun pwd amount -> if !count >= 5 then raise too_many_failures else if pwd <> !pass then (count := !count + 1; raise wrong_pass) else (if amount<0 then raise negative_amount else if amount > !balance then raise not_enough_balance else balance := !balance - amount ; count := 0)); deposit = (fun pwd amount -> if !count >= 5 then raise too_many_failures else if pwd <> !pass then raise (count := !count + 1; raise wrong_pass) else count:=0;if amount <0 then raise negative_amount else balance:= !balance + amount; ); show_balance = (fun pwd -> if !count >= 5 then raise too_many_failures else if pwd <> !pass then raise (count := !count + 1; raise wrong_pass) else (count := 0; !balance))} ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun neighbors edge -> let (v,v2,w) = edge in if v = vertex then neighbors @ [(v2,w)] else neighbors ) [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (total: weight): ('a list * weight) = let (v2,w) = node in if v2 = b then (visited@[b],w +total) else if List.mem v2 visited then raise Fail else aux_list (neighbours g v2) (visited@[v2]) (total + w ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total: weight): ('a list * weight) = match nodes with | [] -> raise Fail | node :: tl -> try aux_node node visited total with Fail -> aux_list tl visited total in aux_list (neighbours g a) [a] 0 ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (sum:weight) fc sc : ('a list * weight) = let (nod,w)=node in if nod = b then sc (visited @ [b]), (sum + w) else if (List.mem nod visited) then fc () else aux_list (neighbours g nod) (visited @ [nod]) (sum + w) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (sum:weight) fc sc : ('a list * weight) = match nodes with |[]-> fc () |(x,weight)::xs-> aux_node (x,weight) visited sum (fun ()-> aux_list xs (visited) (sum) fc sc) sc 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 = match find_all_paths g a b with | [] -> None | l -> Some ( List.hd ( List.sort (fun x y -> let (_,w1) = x and (_,w2) = y in if w1 > w2 then -1 else if w1< w2 then 1 else 0 )l) );; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let count_mistake = ref 0 in let bala = ref 0 in { update_pass = (fun x y -> if x = !pass then (pass := y; count_mistake := 0) else (count_mistake := !count_mistake +1; raise wrong_pass) ) ; retrieve= (fun x a -> if x = !pass && !count_mistake < 5 then (count_mistake := 0; if a >= 0 then if (!bala >= a) then (bala := !bala - a) else raise not_enough_balance else raise negative_amount ) else if !count_mistake < 5 && x != !pass then (count_mistake := !count_mistake +1; raise wrong_pass) else (raise too_many_failures) ) ; deposit = (fun x b -> if x = !pass && !count_mistake < 5 then (count_mistake := 0; if b >= 0 then bala := !bala + b else raise negative_amount ) else if !count_mistake < 5 && x != !pass then(count_mistake := !count_mistake +1; raise wrong_pass) else (raise too_many_failures) ) ; show_balance = (fun x -> if x = !pass && !count_mistake < 5 then ( count_mistake := 0; !bala) else if !count_mistake < 5 && x != !pass then (count_mistake := !count_mistake +1; raise wrong_pass) else (raise too_many_failures) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let get1 (elst:('a * 'a * weight)) = let (v1,_,_) = elst in v1 in let get2w (elst:('a * 'a * weight)) = let (_,v2,w) = elst in (v2,w) in let check acc lst = let v1 = get1 lst in let n = get2w lst in if v1 = vertex then (n::acc) else acc in List.fold_left check [] 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 (v1,w) = node in if List.mem v1 visited then raise Fail else if v1 = b then (visited @ [v1], w) else (fst(aux_list (neighbours g v1) (visited @[v1])), snd(aux_list (neighbours g v1) (visited @ [v1])) + 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 (v1,w1) = node in if List.mem v1 visited then fc() else if v1 = b then sc ([v1],w1) else aux_list (neighbours g v1) (v1::visited) fc (fun (path, cost) -> sc (v1::path, cost + w1)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd :: tl -> let f = (fun () -> aux_list tl visited fc sc) in aux_node hd visited f 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 all_ways = find_all_paths g a b in let max (acc: ('a list * weight)) (lst :('a list * weight)): ('a list * weight) = if snd (lst) > snd(acc) then lst else acc in match all_ways with | [] -> None | _ -> Some (List.fold_left max ([],0) all_ways) ;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let count_mistake = ref 0 in let bala = ref 0 in { update_pass = (fun x y -> if x = !pass then (pass := y; count_mistake := 0) else (count_mistake := !count_mistake +1; raise wrong_pass) ) ; retrieve= (fun x a -> if x = !pass && !count_mistake < 5 then (count_mistake := 0; if a >= 0 then if (!bala >= a) then (bala := !bala - a) else raise not_enough_balance else raise negative_amount ) else if !count_mistake < 5 && x != !pass then (count_mistake := !count_mistake +1; raise wrong_pass) else (raise too_many_failures) ) ; deposit = (fun x b -> if x = !pass && !count_mistake < 5 then (count_mistake := 0; if b >= 0 then bala := !bala + b else raise negative_amount ) else if !count_mistake < 5 && x != !pass then(count_mistake := !count_mistake +1; raise wrong_pass) else (raise too_many_failures) ) ; show_balance = (fun x -> if x = !pass && !count_mistake < 5 then ( count_mistake := 0; !bala) else if !count_mistake < 5 && x != !pass then (count_mistake := !count_mistake +1; raise wrong_pass) else (raise too_many_failures) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let get1 (elst:('a * 'a * weight)) = let (v1,_,_) = elst in v1 in let get2w (elst:('a * 'a * weight)) = let (_,v2,w) = elst in (v2,w) in let check acc lst = let v1 = get1 lst in let n = get2w lst in if v1 = vertex then (n::acc) else acc in List.fold_left check [] 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 (v1,w) = node in if List.mem v1 visited then raise Fail else if v1 = b then (visited @ [v1], w) else (fst(aux_list (neighbours g v1) (visited @[v1])), snd(aux_list (neighbours g v1) (visited @ [v1])) + 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 (v1,w1) = node in if List.mem v1 visited then fc() else if v1 = b then sc ([v1],w1) else aux_list (neighbours g v1) (v1::visited) fc (fun (path, cost) -> sc (v1::path, cost + w1)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd :: tl -> let f = (fun () -> aux_list tl visited fc sc) in aux_node hd visited f 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 all_ways = find_all_paths g a b in let max (acc: ('a list * weight)) (lst :('a list * weight)): ('a list * weight) = if snd (lst) > snd(acc) then lst else acc in match all_ways with | [] -> None | _ -> Some (List.fold_left max ([],0) all_ways) ;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let count_mistake = ref 0 in let bala = ref 0 in { update_pass = (fun x y -> if x = !pass then (pass := y; count_mistake := 0) else (count_mistake := !count_mistake +1; raise wrong_pass) ) ; retrieve= (fun x a -> if x = !pass && !count_mistake < 5 then (count_mistake := 0; if a >= 0 then if (!bala >= a) then (bala := !bala - a) else raise not_enough_balance else raise negative_amount ) else if !count_mistake < 5 && x != !pass then (count_mistake := !count_mistake +1; raise wrong_pass) else (raise too_many_failures) ) ; deposit = (fun x b -> if x = !pass && !count_mistake < 5 then (count_mistake := 0; if b >= 0 then bala := !bala + b else raise negative_amount ) else if !count_mistake < 5 && x != !pass then(count_mistake := !count_mistake +1; raise wrong_pass) else (raise too_many_failures) ) ; show_balance = (fun x -> if x = !pass && !count_mistake < 5 then ( count_mistake := 0; !bala) else if !count_mistake < 5 && x != !pass then (count_mistake := !count_mistake +1; raise wrong_pass) else (raise too_many_failures) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let get1 (elst:('a * 'a * weight)) = let (v1,_,_) = elst in v1 in let get2w (elst:('a * 'a * weight)) = let (_,v2,w) = elst in (v2,w) in let check acc lst = let v1 = get1 lst in let n = get2w lst in if v1 = vertex then (n::acc) else acc in List.fold_left check [] 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 (v1,w) = node in if List.mem v1 visited then raise Fail else if v1 = b then (visited @ [v1], w) else (fst(aux_list (neighbours g v1) (visited @[v1])), snd(aux_list (neighbours g v1) (visited @ [v1])) + 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 (v1,w1) = node in if List.mem v1 visited then fc() else if v1 = b then sc ([v1],w1) else aux_list (neighbours g v1) (v1::visited) fc (fun (path, cost) -> sc (v1::path, cost + w1)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd :: tl -> let f = (fun () -> aux_list tl visited fc sc) in aux_node hd visited f 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 all_ways = find_all_paths g a b in let max (acc: ('a list * weight)) (lst :('a list * weight)): ('a list * weight) = if snd (lst) > snd(acc) then lst else acc in match all_ways with | [] -> None | _ -> Some (List.fold_left max ([],0) all_ways) ;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let fail_cnt = ref 0 in let cur_balance = ref 0 in { update_pass = ( fun oldpass newpass -> if oldpass = !pass then (pass := newpass; fail_cnt := 0) else (fail_cnt := (!fail_cnt + 1); raise wrong_pass) ); deposit = ( fun pwd amount -> if !fail_cnt >= 5 then raise too_many_failures else if pwd = !pass then ( if amount < 0 then raise negative_amount else cur_balance := (!cur_balance + amount) ; fail_cnt := 0 ) else (fail_cnt := (!fail_cnt + 1); raise wrong_pass) ); retrieve = ( fun pwd amount -> if !fail_cnt >= 5 then raise too_many_failures else if pwd = !pass then ( if amount < 0 then raise negative_amount else if amount <= !cur_balance then cur_balance := (!cur_balance - amount) else raise not_enough_balance ; fail_cnt := 0 ) else (fail_cnt := (!fail_cnt + 1); raise wrong_pass) ); show_balance = ( fun pwd -> if !fail_cnt >= 5 then raise too_many_failures else if pwd = !pass then ( fail_cnt := 0; !cur_balance ) else (fail_cnt := (!fail_cnt + 1); raise wrong_pass) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec all_neighbours (es: (('a * 'a * weight) list)) (cur: 'a) (nbs: (('a * weight) list)) = match es with | [] -> nbs | x::xs -> let (v1, v2, w) = x in if v1 = cur then all_neighbours xs cur ((v2, w)::nbs) else all_neighbours xs cur nbs in let {nodes; edges} = g in all_neighbours 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 (v, w) = node in if v = b then ([],0) else aux_list (neighbours g v) visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (dir, w)::xs -> if List.exists (fun a -> a = dir) visited then aux_list xs visited else try let (li, weight_to_goal) = (aux_node (dir,w) (dir::visited)) in ((dir::li), (w+weight_to_goal)) with Fail -> aux_list xs (dir::visited) in let (li, w) = aux_list (neighbours g a) [a] in (a::li, 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 (v, w) = node in if v = b then sc ([], 0) else aux_list (neighbours g v) (v::visited) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc Fail | (dir, w)::xs -> if List.exists (fun a -> a = dir) visited then aux_list xs visited fc sc else try aux_node (dir, w) (dir::visited) fc (fun (v, weight) -> sc ((dir::v), (weight + w))) with Fail -> aux_list xs (dir::visited) fc sc in aux_node (a, 0) [a] (fun e -> raise e) (fun (v, weight) -> (a::v, weight));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_path = (find_all_paths g a b) in let rec long_from_list li (max_l, max_w) = match li with | [] -> (max_l, max_w) | (l, w)::xs -> if max_w < w then long_from_list xs (l,w) else long_from_list xs (max_l, max_w) in match all_path with | [] -> None | x::xs -> Some (long_from_list xs x);; |
let wrong_pw = fun c -> c := !c + 1; raise wrong_pass ;; |
let open_account (initial_pass: passwd) : bank_account = let counter = ref 0 in let pw = ref initial_pass in let balance = ref 0 in { update_pass = (fun old_pw new_pw -> if old_pw = !pw then begin pw := new_pw; counter := 0 end else wrong_pw counter); retrieve = (fun p m -> if !counter >= 5 then raise too_many_failures else if p = !pw then begin counter := 0; if m < 0 then raise negative_amount else if !balance >= m then balance := !balance - m else raise not_enough_balance end else wrong_pw counter ); deposit = (fun p m -> if !counter >= 5 then raise too_many_failures else if p = !pw then begin counter := 0; if m < 0 then raise negative_amount else balance := !balance + m end else wrong_pw counter ); show_balance = (fun p -> if !counter >= 5 then raise too_many_failures else if p = !pw then begin counter := 0; balance := !balance; !balance end else wrong_pw counter ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let e_list = List.filter (fun x -> let (x1, _, _) = x in x1 = vertex) g.edges in List.map (fun (a,b,c) -> (b,c)) e_list ;; let add_end (a: 'a) (b: 'a list) : ('a list) = List.rev (a :: (List.rev b)) ;; |
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 | (n1,n2) when n1 = b -> ((add_end n1 visited),n2) | (n1,n2) -> if List.mem n1 visited then raise Fail else let n = neighbours g n1 in let (p,w) = aux_list n (add_end n1 visited) in (p, w+n2) 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 | (n1, n2) when n1 = b -> sc ([n1], n2) | (n1, n2) -> if List.mem n1 visited then fc () else let n = neighbours g n1 in let v = n1::visited in aux_list n v fc (fun (x,y) -> sc (n1::x, y+n2)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc: ('a list * weight) = match nodes with | [] -> fc () | x::xs -> let sc2 = sc in let fc2 = fun () -> aux_list xs visited fc sc in aux_node x visited fc2 sc2 in aux_node (a,0) [] (fun () -> raise Fail) (fun a -> a) ;; let concat2 s t = let k = fun r -> r in let rec concat s t k = match s with | [] -> k t | x::xs -> concat xs t (fun r -> k (x::r)) in concat s t (fun r -> r) ;; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let p = find_all_paths g a b in match p with | [] -> None | x :: xs -> Some (sort p) ;; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let funds = ref 0 in let attempts = ref 0 in { update_pass = (fun pass1 pass2 -> if (String.equal !pass pass1) then (pass := pass2; attempts := 0;) else (attempts := !attempts + 1; raise (wrong_pass))); retrieve = (fun password amount -> if !attempts >= 5 then raise (too_many_failures); if String.equal !pass password then (if amount < 0 then raise (negative_amount); if !funds >= amount then (funds:= !funds - amount; attempts := 0) else raise (not_enough_balance)) else (attempts := !attempts + 1; raise (wrong_pass)) ); deposit = (fun password amount -> if !attempts >= 5 then raise (too_many_failures); if (String.equal !pass password) then (attempts := 0; (if amount < 0 then raise (negative_amount); funds := !funds + amount)) else (attempts := !attempts + 1; raise (wrong_pass))); show_balance = (fun password -> if !attempts >= 5 then raise (too_many_failures); if (String.equal !pass password) then (attempts := 0; !funds) else (attempts := !attempts + 1; raise (wrong_pass))); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun (output : ('a * weight) list) (n1,n2,w) -> if (compare n1 vertex) == 0 then List.append output [(n2, w)] else output) [] g.edges;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let w = ref 0 in let path = ref [a] in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = if not (List.mem (fst(node)) (visited)) then (path := fst(node)::!path; w:=snd(node) + !w; if fst(node) == b then (!path, !w) else try aux_list (neighbours g (fst(node))) ((fst(node))::visited) with Fail -> (path:= List.tl !path; w:= !w - snd(node); raise (Fail))) else 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 let output = aux_list (neighbours g a) [a] in ((List.rev (fst(output))),snd(output)) ;; |
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let w = ref 0 in let path = ref [a] in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if not (List.mem (fst(node)) (visited)) then (path := fst(node)::!path; w:=snd(node) + !w; if fst(node) == b then sc (!path, !w) else let suc = sc in let fail = fun () -> (path:= List.tl !path; w:= !w - snd(node); fc ()) in aux_list (neighbours g (fst(node))) ((fst(node))::visited) fail suc) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd::tl -> let suc2 = sc in let fail2 = fun () -> aux_list tl visited fc sc in aux_node hd visited fail2 suc2 in let output = aux_list (neighbours g a) [a] (fun () -> raise (Fail)) (fun x -> x) in ((List.rev (fst(output))),snd(output));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();; |
let open_account (initial_pass: passwd) : bank_account = let current_balance=ref 0 in let current_p= ref initial_pass in let wrong_p = ref 0 in let attempts() = if !wrong_p>5 then begin raise too_many_failures end else if !wrong_p<=5 then raise wrong_pass in let check_pass = fun n -> if (n= !current_p) then true else begin wrong_p:=!wrong_p+1; attempts(); false end in { update_pass = (fun o n -> if (check_pass o) then current_p:= n ); retrieve = (fun p m -> if (check_pass p) then if (m<0) then raise negative_amount else if m<= !current_balance then current_balance := !current_balance-m else raise not_enough_balance ); deposit = (fun p d-> if (check_pass p) then if (d>0) then current_balance := !current_balance+d else raise negative_amount ); show_balance = (fun p -> if (check_pass p) then !current_balance else raise wrong_pass ) } ;; |
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 current_balance=ref 0 in let current_p= ref initial_pass in let wrong_p = ref 0 in let attempts() = if !wrong_p>5 then begin raise too_many_failures end else if !wrong_p<=5 then raise wrong_pass in let check_pass = fun n -> if (n= !current_p && !wrong_p<5) then begin wrong_p:=0; true end else begin wrong_p:=!wrong_p+1; attempts(); false end in { update_pass = (fun o n -> if o<> !current_p then begin wrong_p:=!wrong_p+1; raise wrong_pass end else begin current_p:= n; wrong_p:=0 end ); retrieve = (fun p m -> if (check_pass p) then if (m<0) then raise negative_amount else if m<= !current_balance then current_balance := !current_balance-m else raise not_enough_balance ); deposit = (fun p d-> if (check_pass p) then if (d>0) then current_balance := !current_balance+d else raise negative_amount ); show_balance = (fun p -> if (check_pass p) then !current_balance else raise wrong_pass ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun x-> fun (v1, v2, w) -> if v1=vertex then (v2, 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) = 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 initial_pass = ref initial_pass in let balance = ref 0 in let lock_count = ref 0 in let incr_lock_count() = lock_count := !lock_count + 1; raise wrong_pass; in let update_pass (old_pass: passwd) (new_pass: passwd) = if old_pass <> !initial_pass then incr_lock_count() else lock_count := 0; initial_pass := new_pass in let retrieve (pass: passwd) (amnt : int) = if !lock_count > 4 then raise too_many_failures else if pass <> !initial_pass then incr_lock_count() else lock_count := 0; if amnt<0 then raise negative_amount else if !balance - amnt < 0 then raise not_enough_balance else balance := !balance - amnt; () in let deposit (pass: passwd) (amnt : int) = if !lock_count > 4 then raise too_many_failures else if pass <> !initial_pass then incr_lock_count() else lock_count := 0; if amnt<0 then raise negative_amount else balance := !balance + amnt; () in let show_balance (pass: passwd) = if !lock_count > 4 then raise too_many_failures else if pass <> !initial_pass then incr_lock_count() else lock_count := 0; !balance in {update_pass; retrieve; deposit; show_balance} ;; let a = {nodes = ["Paris"; "Berlin"; "Cyprus"]; edges = [("Paris","Berlin", 2); ("Berlin", "Cyprus", 1)]} in ((a, "Paris"), [("Berlin", 2)]); ];; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = ( let f g edge = let (a,b,w) = edge in if a == vertex then (b, w) :: g else g in List.fold_left f [] g.edges; );; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (nn,w) = node in if List.mem nn visited then raise Fail else if nn == b then (nn :: [], w) else let visited = (nn) :: visited in let prepath = aux_list (neighbours g nn) visited in let (nodepath, tw) = prepath in let path = (nn :: nodepath, tw+w) in path and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = if nodes == [] then raise Fail else let fnode = (List.hd nodes) in let fnodepath = aux_node (fnode) visited in let (nodelist, w) = fnodepath in if List.mem b nodelist then fnodepath else aux_list ( nodes) 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) : ('a list * weight) = let (nn,w) = node in if List.mem nn visited then raise Fail else if nn == b then (nn :: [], w) else let visited = (nn) :: visited in let prepath = aux_list (neighbours g nn) visited in let (nodepath, tw) = prepath in let path = (nn :: nodepath, tw+w) in path and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = if nodes == [] then raise Fail else let fnode = (List.hd nodes) in let fnodepath = aux_node (fnode) visited in let (nodelist, w) = fnodepath in if List.mem b nodelist then fnodepath else aux_list ( nodes) visited in aux_node (a,0) [];; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = (notimplemented ());; |
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let wrong_pass_counter = ref 0 in {update_pass = (fun old_pass new_pass -> if (old_pass = !pass) then pass := new_pass else let _ = wrong_pass_counter := !wrong_pass_counter + 1 in raise wrong_pass ); show_balance = (fun old_pass -> if (!wrong_pass_counter > 4) then raise too_many_failures else if (old_pass = !pass) then let _ = wrong_pass_counter := 0 in !balance else let _ = wrong_pass_counter := !wrong_pass_counter + 1 in raise wrong_pass ); retrieve = (fun old_pass input -> if (!wrong_pass_counter > 4) then raise too_many_failures else if (old_pass = !pass) then let _ = wrong_pass_counter := 0 in if (input > !balance) then raise not_enough_balance else if (input < 0) then raise negative_amount else balance := !balance - input else let _ = wrong_pass_counter := !wrong_pass_counter + 1 in raise wrong_pass ); deposit = (fun old_pass input -> if (!wrong_pass_counter > 4) then raise too_many_failures else if (old_pass = !pass) then let _ = wrong_pass_counter := 0 in if (input < 0) then raise negative_amount else balance := !balance + input else let _ = wrong_pass_counter := !wrong_pass_counter + 1 in raise wrong_pass ) } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left ( fun list edge -> let v1,v2,w = edge in if (vertex = v1) then list @ [(v2,w)] else 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) : ('a list * weight) = let (n,nweight) = node in if (List.mem n visited) then raise Fail else if (n = b) then visited @ [n], nweight else let nodes_neighbours = neighbours g n in if (List.length nodes_neighbours) = 0 then raise Fail else aux_list ( List.map ( fun (neighbour,neighbourweight)-> (neighbour, (neighbourweight+nweight)) ) nodes_neighbours ) (visited @ [n]) 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,nweight) = node in if (List.mem n visited) then raise Fail else if (n = b) then (fc (visited @ [n]), sc nweight) else let nodes_neighbours = neighbours g n in if (List.length nodes_neighbours) = 0 then raise Fail else aux_list ( List.map ( fun (neighbour,neighbourweight)-> (neighbour, (neighbourweight+nweight)) ) nodes_neighbours ) (visited @ [n]) (fun nodes -> fc (nodes)) (fun node -> node) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> raise Fail | h::t -> try aux_node h visited fc sc with Fail -> aux_list t visited fc sc in aux_node (a,0) [] (fun nodes -> (nodes)) (fun node -> node);; |
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 (List.length paths = 0) then None else let longest = ref (List.hd paths) in let _ = List.iter (fun path -> let _, pweight = path in let _, lweight = !longest in if (pweight >= lweight) then let _ = longest := path in () else () ) (List.tl paths) in Some !longest;; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let wrong_count = ref 0 in let deposit = ref 0 in let check_pass = (fun pass -> if !wrong_count >= 5 then (raise too_many_failures) else if pass <> !password then (wrong_count := !wrong_count + 1; raise wrong_pass) else (wrong_count := 0; true)) in { update_pass = (fun old_passwd new_passwd -> if old_passwd = !password then (wrong_count := 0; password := new_passwd) else (wrong_count := !wrong_count + 1; raise wrong_pass)); deposit = (fun pass depos -> if (check_pass pass && depos >= 0) then deposit:= !deposit + depos else raise negative_amount); retrieve = (fun pass retr -> if (check_pass pass && !deposit >= retr && retr >= 0) then deposit:= !deposit - retr else if retr < 0 then raise negative_amount else raise not_enough_balance); show_balance = (fun pass -> if check_pass pass then (); !deposit); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec search_list (lst: ('a * 'a * weight) list) (acc: ('a * weight) list): ('a * weight) list = match lst with | [] -> acc | x::xs -> let (a,b,c) = x in if a = vertex then let y = (b,c) in search_list xs (y::acc) else search_list xs acc in search_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) : ('a list * weight) = let (vertex, w) = node in if vertex = b then (vertex::[], w) else try (List.find (fun x -> x = vertex) visited; raise Fail) with Not_found -> let lst, tot_weight = (aux_list (neighbours g vertex) (vertex::visited)) in (vertex::lst, w + tot_weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> let (vertex,w) = x in try aux_node (vertex,w) 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)= let (vertex, w) = node in if vertex = b then sc (vertex::[], w) else try (List.find (fun x -> x = vertex) visited; fc ()) with Not_found -> aux_list (neighbours g vertex) (vertex::visited) fc (fun (lst, tot_weight) -> sc (vertex::lst, w + tot_weight)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> let (vertex,w) = x in aux_node (vertex,w) visited (fun () -> aux_list xs visited fc sc) sc in aux_node (a,0) [] (fun () -> raise Fail) (fun r -> r);; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec longest l max_path max_cost = match l with | [] -> if max_path != [] then Some (max_path, max_cost) else None | x::xs -> let (path, cost) = x in if cost >= max_cost then longest xs path cost else longest xs max_path max_cost in (longest (find_all_paths g a b) [] 0);; |
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let num_of_failures = ref 0 in { update_pass = (fun oldPass newPass -> if oldPass = !password then ( password := newPass; num_of_failures := 0; ) else ( num_of_failures := !num_of_failures + 1; raise wrong_pass; ) ); retrieve = (fun pass amount -> if !num_of_failures >= 5 then raise too_many_failures else if pass = !password then ( num_of_failures := 0; if amount < 0 then raise negative_amount else if amount <= !balance then balance := !balance - amount else raise not_enough_balance ) else ( num_of_failures := !num_of_failures + 1; raise wrong_pass; ) ); deposit = (fun pass amount -> if !num_of_failures >= 5 then raise too_many_failures else if pass = !password then ( num_of_failures := 0; if amount < 0 then raise negative_amount else balance := !balance + amount ) else ( num_of_failures := !num_of_failures + 1; raise wrong_pass; ) ); show_balance = (fun pass -> if !num_of_failures >= 5 then raise too_many_failures else if pass = !password then ( num_of_failures := 0; !balance ) else ( num_of_failures := !num_of_failures + 1; raise wrong_pass; ) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc edge -> let (f,t,c) = edge in if (f = vertex) then acc @ [(t,c)] else acc ) [] (g.edges) ;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (n,w) = node in if List.mem n visited then raise Fail else if n = b then ([n],w) else let neighbours_n = neighbours g n in let (path, weight) = aux_list neighbours_n (visited @ [n]) in (n :: path, w + weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | h :: t -> try let (path, weight) = aux_node h visited in (path, weight) 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 if List.mem n visited then fc () else if n = b then sc ([n],w) else let neighbours_n = neighbours g n in aux_list neighbours_n (visited @ [n]) (fun () -> fc ()) (fun (path, weight) -> sc (n :: path, w + weight)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | h :: t -> let sc2 = sc in let fc2 = fun () -> aux_node h visited fc sc in aux_list t visited fc2 sc2 in aux_node (a,0) [] (fun () -> raise Fail) (fun (path, weight) -> (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 if List.length paths = 0 then None else let max_weight = List.fold_left (fun acc (_, weight) -> if weight > acc then weight else acc ) min_int paths in let best_paths = List.filter (fun (_, weight) -> weight = max_weight) paths in Some (List.hd best_paths) ;; |
let open_account (initial_pass: passwd) : bank_account = let (pass, balance, fail_count) = (ref initial_pass, ref 0, ref 0) in {update_pass = (fun old_pass new_pass -> if old_pass <> !pass then (fail_count := !fail_count + 1; raise wrong_pass) else (pass := new_pass; fail_count := 0) ); retrieve = (fun input_pass amount -> if !fail_count >= 5 then raise too_many_failures else if input_pass <> !pass then (fail_count := !fail_count + 1; raise wrong_pass) else if amount < 0 then raise negative_amount else if !balance < amount then raise not_enough_balance else (fail_count := 0 ;balance := !balance - amount) ); deposit = (fun input_pass amount -> if !fail_count >= 5 then raise too_many_failures else if input_pass <> !pass then (fail_count := !fail_count + 1; raise wrong_pass) else if amount < 0 then raise negative_amount else (fail_count := 0 ; balance := !balance + amount) ); show_balance = (fun input_pass -> if !fail_count >= 5 then raise too_many_failures else if input_pass <> !pass then (fail_count := !fail_count + 1; raise wrong_pass) else (fail_count := 0 ; !balance) ); } ;; |
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc edge -> let (v1, v2, w) = edge in if v1 = vertex then (v2, w) :: acc else acc ) [] g.edges let rec print_list l = match l with | [] -> print_string "" | (e, _) :: t -> print_string e; print_string " "; print_list t let rec print_list2 l = match l with | [] -> print_string "" | n :: t -> print_string (n ^ " "); print_list2 t;; |
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (results: 'a list * weight) : ('a list * weight) = let (n, w) = node in let (rl, rw) = results in if n = b then (rl @ [n], rw + w) else aux_list (neighbours g n) (n :: visited) (rl @ [n], rw + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (results: 'a list * weight) : ('a list * weight) = let not_visited = List.filter (fun (n, w) -> List.for_all ((<>) n) visited) nodes in match not_visited with | [] -> raise Fail | (n, w) :: t -> try aux_node (n, w) visited results with Fail -> aux_list t (n :: visited) results in aux_list (neighbours g a) [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 (n, w) = node in if n = b then sc ([b], w) else aux_list (neighbours g n) (n :: visited) fc (fun (rl, rw) -> sc (n :: rl, w + rw)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = let not_visited = List.filter (fun (n, w) -> List.for_all ((<>) n) visited) nodes in match not_visited with | [] -> fc () | (n, w) :: t -> aux_node (n, w) visited (fun () -> aux_list t (n :: visited) fc sc) sc in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun (rl, rw) -> (a :: rl, rw));; |
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec aux (paths: ('a list * weight) list) (best: ('a list * weight) option) : ('a list * weight) option = match paths with | [] -> best | (p, w) :: t -> match best with | None -> aux t (Some (p, w)) | Some (bp, bw) -> if w >= bw then aux t (Some (p, w)) else aux t best in aux (find_all_paths g a b) None;; |
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) [];; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.