Commit 0f7dc44d authored by Andreas Pfurtscheller's avatar Andreas Pfurtscheller

Add HW06 馃搫

parent cfb1db24
......@@ -80,27 +80,78 @@ let print_tree filename btree =
(**************************** HOMEWORK STARTS HERE ***************************)
(*****************************************************************************)
(* Assignment 6.5 [3 points] *)
let int_to_nat = todo
let rec int_to_nat i = match i with
| 0 -> Zero
| n -> Succ (int_to_nat (n-1))
let nat_to_int = todo
let rec nat_to_int n = match n with
| Zero -> 0
| Succ (s) -> 1 + nat_to_int s
let add = todo
let rec add a b = match a, b with
| Zero, Zero -> Zero
| Succ (a), Zero -> Succ (add a Zero)
| Zero, Succ (b) -> Succ (add Zero b)
| Succ (a), Succ (b) -> Succ (Succ (add a b))
let mul = todo
let rec mul a b = match a with
| Zero -> Zero
| Succ (x) -> add b (mul x b)
let pow = todo
let rec pow a b = match a, b with
| _, Zero -> Succ (Zero)
| Zero, _ -> Zero
| xa, Succ (xb) -> mul xa (pow xa xb)
let leq = todo
let rec leq a b = match a, b with
| Zero, _ -> true
| Succ (_), Zero -> false
| Succ (xa), Succ (xb) -> leq xa xb
(*****************************************************************************)
(* Assignment 6.6 [6 points] *)
let insert = todo
let insert p tree =
let rec insert_node p node x0 y0 depth =
let (x,y) = p in
let pow_int a b = (nat_to_int (pow (int_to_nat a) (int_to_nat b))) in
match node with
| NoPoint -> Point (x, y)
| Point (ax,ay) -> if x==ax && y==ay then Point (ax, ay) else
(insert_node (x,y) (
insert_node (ax,ay) (QNode(NoPoint, NoPoint, NoPoint, NoPoint)) x0 y0 depth
) x0 y0 depth)
| QNode (bl, tl, br, tr) ->
let d = pow_int 2 depth in
let x1 = x0 + (tree.width / d) in
let y1 = y0 + (tree.height / d) in
let xc = (x0 + x1) / 2 in
let yc = (y0 + y1) / 2 in
if x >= x0 && x < xc && y >= y0 && y < yc then
QNode ((insert_node p bl x0 y0 (depth+1)), tl, br, tr)
else if x >= x0 && x < xc && y >= yc && y < y1 then
QNode (bl, (insert_node p tl x0 yc (depth+1)), br, tr)
else if x >= xc && x < x1 && y >= y0 && y < yc then
QNode (bl, tl, (insert_node p br xc y0 (depth+1)), tr)
else if x >= xc && x < x1 && y >= yc && y < y1 then
QNode (bl, tl, br, (insert_node p tr xc yc (depth+1)))
else NoPoint in
{ width=tree.width; height=tree.height; root=(insert_node p tree.root 0 0 0) }
(*****************************************************************************)
(* Assignment 6.6 [4 points] *)
let eval_expr = todo
(* Assignment 6.7 [4 points] *)
let rec eval_expr expr =
let eval_binary o (a1,b1) (a2,b2) = match o with
| Add -> (a1*b2 + a2*b1, b1*b2)
| Sub -> (a1*b2 - a2*b1, b1*b2)
| Mul -> (a1*a2, b1*b2)
| Div -> (a1*b2, b1*a2) in
let eval_unary o (a1,b1) = match o with
| Neg -> (-1*a1, b1) in
match expr with
| Const (a,b) -> (a,b)
| UnOp (o, a) -> eval_unary o (eval_expr a)
| BinOp (o, a, b) -> eval_binary o (eval_expr a) (eval_expr b)
(*****************************************************************************)
......
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