Commit 6f9f38c3 authored by Taddeus Kroes's avatar Taddeus Kroes

Code cleanup.

parent 2091563a
...@@ -5,7 +5,7 @@ type bin_op = ArithOp of arith_op | RelOp of rel_op | LogicOp of logic_op ...@@ -5,7 +5,7 @@ type bin_op = ArithOp of arith_op | RelOp of rel_op | LogicOp of logic_op
type mon_op = UnaryMinus | Negation type mon_op = UnaryMinus | Negation
type const = BoolConst of bool | IntConst of int type const = BoolConst of bool | IntConst of int
type expr = type expr =
Enclosure of expr Enclosure of expr
| BinOp of expr * bin_op * expr | BinOp of expr * bin_op * expr
| MonOp of mon_op * expr | MonOp of mon_op * expr
| Id of string | Id of string
...@@ -15,14 +15,14 @@ type expr = ...@@ -15,14 +15,14 @@ type expr =
let rec eval_expr = let rec eval_expr =
let eval_bin_op = let eval_bin_op =
let eval_arith_op = function let eval_arith_op = function
Plus -> "+" | Plus -> "+"
| Minus -> "-" | Minus -> "-"
| Times -> "*" | Times -> "*"
| Divide -> "/" | Divide -> "/"
| Modulo -> "mod" | Modulo -> "mod"
in in
let eval_rel_op = function let eval_rel_op = function
Eq -> "=" | Eq -> "="
| Neq -> "!=" | Neq -> "!="
| Lt -> "<" | Lt -> "<"
| Lte -> "<=" | Lte -> "<="
...@@ -30,24 +30,24 @@ let rec eval_expr = ...@@ -30,24 +30,24 @@ let rec eval_expr =
| Gte -> ">=" | Gte -> ">="
in in
let eval_logic_op = function let eval_logic_op = function
And -> "&&" | And -> "&&"
| Or -> "||" | Or -> "||"
in in
function function
ArithOp op -> eval_arith_op(op) | ArithOp op -> eval_arith_op(op)
| RelOp op -> eval_rel_op(op) | RelOp op -> eval_rel_op(op)
| LogicOp op -> eval_logic_op(op) | LogicOp op -> eval_logic_op(op)
in in
let eval_mon_op = function let eval_mon_op = function
UnaryMinus -> "-" | UnaryMinus -> "-"
| Negation -> "!" | Negation -> "!"
in in
let eval_const = function let eval_const = function
IntConst i -> string_of_int i | IntConst i -> string_of_int i
| BoolConst b -> string_of_bool b | BoolConst b -> string_of_bool b
in in
function function
Enclosure e -> "(" ^ eval_expr(e) ^ ")" | Enclosure e -> "(" ^ eval_expr(e) ^ ")"
| BinOp (e1, op, e2) -> eval_expr(e1) ^ " " ^ eval_bin_op(op) | BinOp (e1, op, e2) -> eval_expr(e1) ^ " " ^ eval_bin_op(op)
^ " " ^ eval_expr(e2) ^ " " ^ eval_expr(e2)
| MonOp (op, e) -> eval_mon_op(op) ^ eval_expr(e) | MonOp (op, e) -> eval_mon_op(op) ^ eval_expr(e)
......
...@@ -9,17 +9,17 @@ let emptySet = Set([]) ...@@ -9,17 +9,17 @@ let emptySet = Set([])
(* Delete an element from a set (if it does not exists in it yet) (* Delete an element from a set (if it does not exists in it yet)
* and return the new set with the element *) * and return the new set with the element *)
let addElem elem set = match set with let addElem elem set = match set with
Set ([]) -> Set([elem]) | Set ([]) -> Set([elem])
| Set (l) -> if List.mem elem l then set else Set(elem::l) | Set (l) -> if List.mem elem l then set else Set(elem::l)
(* Delete an element from a set (if it exists in it) and return (* Delete an element from a set (if it exists in it) and return
* the new set without the element *) * the new set without the element *)
let delElem elem set = let delElem elem set =
let rec delFromList elem = function let rec delFromList elem = function
[] -> [] | [] -> []
| h::t -> if h = elem then t else h::(delFromList elem t) | h::t -> if h = elem then t else h::(delFromList elem t)
in match set with in match set with
Set ([]) -> set | Set ([]) -> set
| Set (l) -> Set(delFromList elem l) | Set (l) -> Set(delFromList elem l)
(* Check if an element exists in a set *) (* Check if an element exists in a set *)
...@@ -28,19 +28,19 @@ let isElem elem = function Set (l) -> List.mem elem l ...@@ -28,19 +28,19 @@ let isElem elem = function Set (l) -> List.mem elem l
(* Get the union of sets a and b as a new set *) (* Get the union of sets a and b as a new set *)
let union a b = let union a b =
match a with match a with
Set ([]) -> b | Set ([]) -> b
| Set(la) -> match b with | Set(la) -> match b with
Set ([]) -> a | Set ([]) -> a
| Set(lb) -> | Set(lb) ->
Set(la @ List.filter (fun e -> not (List.mem e la)) lb) Set(la @ List.filter (fun e -> not (List.mem e la)) lb)
(* Get the intersection of sets a and b as a new set *) (* Get the intersection of sets a and b as a new set *)
let intersection a b = let intersection a b =
match a with match a with
Set ([]) -> a | Set ([]) -> a
| Set(la) -> match b with | Set(la) -> match b with
Set ([]) -> b | Set ([]) -> b
| Set(lb) -> | Set(lb) ->
let b_in_a = List.filter (fun e -> List.mem e la) lb in let b_in_a = List.filter (fun e -> List.mem e la) lb in
Set(List.filter (fun e -> List.mem e lb) b_in_a) Set(List.filter (fun e -> List.mem e lb) b_in_a)
...@@ -63,4 +63,4 @@ let b = addElem 9 b;; ...@@ -63,4 +63,4 @@ let b = addElem 9 b;;
let b = addElem 0 b;; (* 0, 9, 3, 8, 2 *) let b = addElem 0 b;; (* 0, 9, 3, 8, 2 *)
union a b;; (* 1, 7, 3, 8, 2, 0, 9 *) union a b;; (* 1, 7, 3, 8, 2, 0, 9 *)
intersection a b;; (* 3, 8, 2 *) intersection a b;; (* 3, 8, 2 *)
\ No newline at end of file
module type SET = module type SET = sig
sig
type 'a set type 'a set
val emptySet : 'a set val emptySet : 'a set
val addElem : 'a -> 'a set -> 'a set val addElem : 'a -> 'a set -> 'a set
...@@ -9,8 +8,7 @@ sig ...@@ -9,8 +8,7 @@ sig
val intersection : 'a set -> 'a set -> 'a set val intersection : 'a set -> 'a set -> 'a set
end end
module Set : SET = module Set : SET = struct
struct
type 'a set = Set of 'a list type 'a set = Set of 'a list
(* Create an empty set *) (* Create an empty set *)
...@@ -19,17 +17,17 @@ struct ...@@ -19,17 +17,17 @@ struct
(* Delete an element from a set (if it does not exists in it yet) (* Delete an element from a set (if it does not exists in it yet)
* and return the new set with the element *) * and return the new set with the element *)
let addElem elem set = match set with let addElem elem set = match set with
Set ([]) -> Set([elem]) | Set ([]) -> Set([elem])
| Set (l) -> if List.mem elem l then set else Set(elem::l) | Set (l) -> if List.mem elem l then set else Set(elem::l)
(* Delete an element from a set (if it exists in it) and return (* Delete an element from a set (if it exists in it) and return
* the new set without the element *) * the new set without the element *)
let delElem elem set = let delElem elem set =
let rec delFromList elem = function let rec delFromList elem = function
[] -> [] | [] -> []
| h::t -> if h = elem then t else h::(delFromList elem t) | h::t -> if h = elem then t else h::(delFromList elem t)
in match set with in match set with
Set ([]) -> set | Set ([]) -> set
| Set (l) -> Set(delFromList elem l) | Set (l) -> Set(delFromList elem l)
(* Check if an element exists in a set *) (* Check if an element exists in a set *)
...@@ -38,19 +36,19 @@ struct ...@@ -38,19 +36,19 @@ struct
(* Get the union of sets a and b as a new set *) (* Get the union of sets a and b as a new set *)
let union a b = let union a b =
match a with match a with
Set ([]) -> b | Set ([]) -> b
| Set(la) -> match b with | Set(la) -> match b with
Set ([]) -> a | Set ([]) -> a
| Set(lb) -> | Set(lb) ->
Set(la @ List.filter (fun e -> not (List.mem e la)) lb) Set(la @ List.filter (fun e -> not (List.mem e la)) lb)
(* Get the intersection of sets a and b as a new set *) (* Get the intersection of sets a and b as a new set *)
let intersection a b = let intersection a b =
match a with match a with
Set ([]) -> a | Set ([]) -> a
| Set(la) -> match b with | Set(la) -> match b with
Set ([]) -> b | Set ([]) -> b
| Set(lb) -> | Set(lb) ->
let b_in_a = List.filter (fun e -> List.mem e la) lb in let b_in_a = List.filter (fun e -> List.mem e la) lb in
Set(List.filter (fun e -> List.mem e lb) b_in_a) Set(List.filter (fun e -> List.mem e lb) b_in_a)
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment