separated into modules, added parser and lexer for miniImp
This commit is contained in:
5
lib/exercises/dune
Normal file
5
lib/exercises/dune
Normal file
@ -0,0 +1,5 @@
|
||||
(library
|
||||
(name exercises)
|
||||
(public_name exercises))
|
||||
|
||||
(include_subdirs qualified)
|
||||
109
lib/exercises/exercises.ml
Normal file
109
lib/exercises/exercises.ml
Normal file
@ -0,0 +1,109 @@
|
||||
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
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
60
lib/exercises/exercises.mli
Normal file
60
lib/exercises/exercises.mli
Normal file
@ -0,0 +1,60 @@
|
||||
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
|
||||
|
||||
|
||||
val eval_a_exp: a_exp -> int
|
||||
val eval_b_exp: b_exp -> bool
|
||||
|
||||
type 'a my_tree =
|
||||
Leaf of 'a
|
||||
| Node of ('a my_tree) list
|
||||
|
||||
val mod_list: 'a list -> 'a list list
|
||||
|
||||
(* --------------------------------------------------------------------------- *)
|
||||
|
||||
val to_tup : ('a -> 'b) -> ('c -> 'd) -> (('a * 'c) -> ('b * 'd))
|
||||
|
||||
val partialsum : int list -> int list
|
||||
|
||||
type label =
|
||||
string
|
||||
|
||||
type 'a finite_state_automata = {
|
||||
l: label;
|
||||
next: ('a finite_state_automata * 'a list) list;
|
||||
final: bool;
|
||||
}
|
||||
|
||||
val check_included : 'a list -> 'a finite_state_automata -> bool
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
|
||||
module StringMap : Map.S with type key = string
|
||||
|
||||
type fsa = {
|
||||
vertices: bool StringMap.t;
|
||||
edges: (string * char) StringMap.t;
|
||||
state: string;
|
||||
}
|
||||
|
||||
val ex8 : char list -> fsa -> bool
|
||||
|
||||
|
||||
type binary_tree =
|
||||
Node of binary_tree * binary_tree
|
||||
| Leaf of int
|
||||
|
||||
val ex9 : binary_tree -> int
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
Reference in New Issue
Block a user