Skip to content
Snippets Groups Projects
Commit 6f9f38c3 authored by Taddeus Kroes's avatar Taddeus Kroes
Browse files

Code cleanup.

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