type a_exp = Aval of int | Plus of a_exp * a_exp | Minus of a_exp * a_exp | Times of a_exp * a_exp | Of_bool of b_exp and b_exp = Bval of bool | And of b_exp * b_exp | Or of b_exp * b_exp | Not of b_exp | Minor of a_exp * a_exp let rec eval_a_exp node = match node with Aval (i) -> i | Plus (i, j) -> (eval_a_exp i) + (eval_a_exp j) | Minus (i, j) -> (eval_a_exp i) - (eval_a_exp j) | Times (i, j) -> (eval_a_exp i) * (eval_a_exp j) | Of_bool b -> if (eval_b_exp b) then 1 else 0 and eval_b_exp node = match node with Bval (b) -> b | And (a, b) -> (eval_b_exp a) && (eval_b_exp b) | Or (a, b) -> (eval_b_exp a) || (eval_b_exp b) | Not b -> not (eval_b_exp b) | Minor (i, j) -> (eval_a_exp i) < (eval_a_exp j) type 'a my_tree = Leaf of 'a | Node of ('a my_tree) list let mod_list y = (List.fold_left (fun acc x -> match acc with | [a] when ((List.hd a) = x) -> [x :: a] | a :: tl when ((List.hd a) = x) -> (x :: a) :: tl | _ -> [x] :: acc) [] y) |> List.rev (* -------------------------------------------------------------------------- *) let to_tup f g = fun x -> match x with (a, b) -> (f a, g b) let partialsum l = snd (List.fold_left_map (fun acc x -> (acc+x, acc+x)) 0 l) type label = string type 'a finite_state_automata = { l: label; next: ('a finite_state_automata * 'a list) list; final: bool; } let rec check_included input fsa = match input with [] -> fsa.final | a::rest -> ( match List.find_opt (fun x -> List.mem a (snd x)) fsa.next with None -> false | Some x -> check_included rest (fst x) ) (* -------------------------------------------------------------------------- *) module StringMap = Map.Make(String) type fsa = { vertices: bool StringMap.t; edges: (string * char) StringMap.t; state: string; } let ex8 (instr: char list) (infsa: fsa) = let rec helper_ex8 (i: char list) (ifsa: fsa) (current: string) = match i with [] -> ( match StringMap.find_opt current ifsa.vertices with None -> false | Some b -> b ) | a::rest -> ( match StringMap.find_first_opt (fun _ -> true) (StringMap.filter (fun x (_, y) -> x = current && y = a) ifsa.edges) with None -> false | Some (_, (outedge, _)) -> helper_ex8 rest ifsa outedge ) in helper_ex8 instr infsa infsa.state type binary_tree = Node of binary_tree * binary_tree | Leaf of int let ex9 b = let rec helper_ex9 b' n = match b' with Leaf a -> a + n | Node (r, l) -> (helper_ex9 r (helper_ex9 l n)) in helper_ex9 b 0 (* -------------------------------------------------------------------------- *)