restructure + lecutre 07
This commit is contained in:
96 files changed
+458
-4
No files matched your search
@@ -0,0 +1,3 @@
|
||||
"Rule: ocaml dependencies ml (%=two_list_queue )": "\153s\252\225.\205\197\1597\030\142\254Q\213.\135"
|
||||
"Resource: /home/flami/Projects/comp3958/lecture06/two_list_queue.ml": "\174\189*\255(\166==\251\006\232\150\141\1714\130"
|
||||
"Rule: ocaml: ml -> cmo & cmi (%=two_list_queue )": "9\165\179\031F'#6\127\149\136<\242y\019t"
|
||||
@@ -0,0 +1,8 @@
|
||||
### Starting build.
|
||||
# Target: /home/flami/.opam/default/bin/ocamlc.opt -config, tags: { }
|
||||
/home/flami/.opam/default/bin/ocamlc.opt -config
|
||||
# Target: two_list_queue.ml.depends, tags: { extension:ml, file:two_list_queue.ml, ocaml, ocamldep, quiet }
|
||||
/home/flami/.opam/default/bin/ocamldep.opt -modules two_list_queue.ml > two_list_queue.ml.depends
|
||||
# Target: two_list_queue.cmo, tags: { byte, compile, extension:cmo, extension:ml, file:two_list_queue.cmo, file:two_list_queue.ml, implem, ocaml, quiet }
|
||||
/home/flami/.opam/default/bin/ocamlc.opt -c -o two_list_queue.cmo two_list_queue.ml
|
||||
# Compilation successful.
|
||||
@@ -0,0 +1,42 @@
|
||||
(* queue is empty iff the first list is empty *)
|
||||
type 'a t = 'a list * 'a list;;
|
||||
|
||||
exception Empty;;
|
||||
|
||||
let empty = ([], []);;
|
||||
|
||||
let is_empty (l, _) = l = [];;
|
||||
|
||||
let enqueue x (l1, l2) =
|
||||
match l1 with
|
||||
| [] -> [x], l2
|
||||
| _ -> (l1, x :: l2);;
|
||||
|
||||
let dequeue (l1, l2) =
|
||||
match l1 with
|
||||
| [] -> raise Empty
|
||||
| [_] -> (List.rev l2, [])
|
||||
| _ :: xs -> (xs, l2);;
|
||||
|
||||
let dequeue_opt q =
|
||||
try
|
||||
Some (dequeue q)
|
||||
with
|
||||
| Empty -> None;;
|
||||
|
||||
let front (l, _) =
|
||||
match l with
|
||||
| [] -> raise Empty
|
||||
| x :: _ -> x;;
|
||||
|
||||
let front_opt (l, _) =
|
||||
match l with
|
||||
| [] -> None
|
||||
| x :: _ -> Some x;;
|
||||
|
||||
let of_list l =
|
||||
List.fold_left (Fun.flip enqueue) empty l;;
|
||||
|
||||
let to_list (l1, l2) =
|
||||
l1 @ List.rev l2;;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
two_list_queue.ml: Fun List
|
||||
@@ -0,0 +1,63 @@
|
||||
type 'a t = Leaf | Node of 'a * 'a t * 'a t;;
|
||||
|
||||
let rec size t =
|
||||
match t with
|
||||
| Leaf -> 0
|
||||
| Node (_, l, r) ->
|
||||
1 + size l + size r;;
|
||||
|
||||
let rec height t =
|
||||
match t with
|
||||
| Leaf -> 0
|
||||
| Node (_, l, r) ->
|
||||
1 + max (height l) (height r);;
|
||||
|
||||
let empty = Leaf;;
|
||||
|
||||
let is_empty t = t = Leaf;;
|
||||
|
||||
let rec insert x t =
|
||||
match t with
|
||||
| Leaf -> Node (x, Leaf, Leaf)
|
||||
| Node (x', l, r) when x < x' ->
|
||||
Node (x', insert x l, r)
|
||||
| Node (x', l, r) when x > x' ->
|
||||
Node (x', l, insert x r)
|
||||
| _ -> t;;
|
||||
|
||||
let of_list l =
|
||||
List.fold_left (Fun.flip insert) Leaf l;;
|
||||
|
||||
let rec mem x t =
|
||||
match t with
|
||||
| Leaf -> false
|
||||
| Node (x', l, _) when x < x' ->
|
||||
mem x l
|
||||
| Node (x', _, r) when x > x' ->
|
||||
mem x r
|
||||
| _ -> true;;
|
||||
|
||||
let rec largest t =
|
||||
match t with
|
||||
| Leaf -> failwith "largest: empty tree"
|
||||
| Node (x, _, Leaf) -> x
|
||||
| Node (_, _, r) -> largest r;;
|
||||
|
||||
let rec smallest t =
|
||||
match t with
|
||||
| Leaf -> failwith "smallest: empty tree"
|
||||
| Node (x, Leaf, _) -> x
|
||||
| Node (_, l, _) -> smallest l;;
|
||||
|
||||
let rec delete x t =
|
||||
match t with
|
||||
| Leaf -> Leaf
|
||||
| Node (x', l, r) when x < x' ->
|
||||
Node (x', delete x l, r)
|
||||
| Node (x', l, r) when x > x' ->
|
||||
Node (x', l, delete x r)
|
||||
| Node (_, l, Leaf) -> l
|
||||
| Node (_, Leaf, r) -> r
|
||||
| Node (_, l, r) ->
|
||||
let succ = largest l in
|
||||
Node (succ, delete succ l, r);;
|
||||
@@ -0,0 +1,14 @@
|
||||
(* type 'a t = Leaf | Node of 'a * 'a t * 'a t *)
|
||||
|
||||
type 'a t (* for abstract data type *)
|
||||
|
||||
val size : 'a t -> int
|
||||
val height : 'a t -> int
|
||||
val empty : 'a t
|
||||
val is_empty : 'a t -> bool
|
||||
val insert : 'a -> 'a t -> 'a t
|
||||
val of_list : 'a list -> 'a t
|
||||
val mem : 'a -> 'a t -> bool
|
||||
val largest : 'a t -> 'a
|
||||
val smallest : 'a t -> 'a
|
||||
val delete : 'a -> 'a t -> 'a t
|
||||
@@ -0,0 +1,68 @@
|
||||
(* dont do this *)
|
||||
|
||||
|
||||
module Bstree = struct
|
||||
type 'a t = Leaf | Node of 'a * 'a t * 'a t;;
|
||||
|
||||
let rec size t =
|
||||
match t with
|
||||
| Leaf -> 0
|
||||
| Node (_, l, r) ->
|
||||
1 + size l + size r;;
|
||||
|
||||
let rec height t =
|
||||
match t with
|
||||
| Leaf -> 0
|
||||
| Node (_, l, r) ->
|
||||
1 + max (height l) (height r);;
|
||||
|
||||
let empty = Leaf;;
|
||||
|
||||
let is_empty t = t = Leaf;;
|
||||
|
||||
let rec insert x t =
|
||||
match t with
|
||||
| Leaf -> Node (x, Leaf, Leaf)
|
||||
| Node (x', l, r) when x < x' ->
|
||||
Node (x', insert x l, r)
|
||||
| Node (x', l, r) when x > x' ->
|
||||
Node (x', l, insert x r)
|
||||
| _ -> t;;
|
||||
|
||||
let of_list l =
|
||||
List.fold_left (Fun.flip insert) Leaf l;;
|
||||
|
||||
let rec mem x t =
|
||||
match t with
|
||||
| Leaf -> false
|
||||
| Node (x', l, _) when x < x' ->
|
||||
mem x l
|
||||
| Node (x', _, r) when x > x' ->
|
||||
mem x r
|
||||
| _ -> true;;
|
||||
|
||||
let rec largest t =
|
||||
match t with
|
||||
| Leaf -> failwith "largest: empty tree"
|
||||
| Node (x, _, Leaf) -> x
|
||||
| Node (_, _, r) -> largest r;;
|
||||
|
||||
let rec smallest t =
|
||||
match t with
|
||||
| Leaf -> failwith "smallest: empty tree"
|
||||
| Node (x, Leaf, _) -> x
|
||||
| Node (_, l, _) -> smallest l;;
|
||||
|
||||
let rec delete x t =
|
||||
match t with
|
||||
| Leaf -> Leaf
|
||||
| Node (x', l, r) when x < x' ->
|
||||
Node (x', delete x l, r)
|
||||
| Node (x', l, r) when x > x' ->
|
||||
Node (x', l, delete x r)
|
||||
| Node (_, l, Leaf) -> l
|
||||
| Node (_, Leaf, r) -> r
|
||||
| Node (_, l, r) ->
|
||||
let succ = largest l in
|
||||
Node (succ, delete succ l, r);;
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
"Rule: ocaml dependencies mli (%=bstree )": "!\134$\216+23\213\196T\181K\021\128\195\018"
|
||||
"Resource: /home/flami/Projects/comp3958/lecture06/functor/bstree.mli": "\194\172\208\246\172^Y\171\1563@\144\158H\027l"
|
||||
"Rule: ocaml: ml & cmi -> cmo (%=bstree )": "\172\028\140\193\160\2525\162\225\231@cE\2307\244"
|
||||
"Resource: /home/flami/Projects/comp3958/lecture06/functor/bstree.ml": "\228\211>\161\t\1291\180\144c_ \189\147\236x"
|
||||
"Rule: ocaml: mli -> cmi (%=bstree )": "\225aH6\244\021+9l\172\247\209 d\183\159"
|
||||
"Rule: ocaml dependencies ml (%=bstree )": "\031$\199\007@\208\227\1694l\138\193r\227\203\171"
|
||||
@@ -0,0 +1,12 @@
|
||||
### Starting build.
|
||||
# Target: /home/flami/.opam/default/bin/ocamlc.opt -config, tags: { }
|
||||
/home/flami/.opam/default/bin/ocamlc.opt -config
|
||||
# Target: bstree.mli.depends, tags: { extension:mli, file:bstree.mli, ocaml, ocamldep, quiet }
|
||||
/home/flami/.opam/default/bin/ocamldep.opt -modules bstree.mli > bstree.mli.depends # cached
|
||||
# Target: bstree.cmi, tags: { byte, compile, extension:mli, file:bstree.mli, interf, ocaml, quiet }
|
||||
/home/flami/.opam/default/bin/ocamlc.opt -c -o bstree.cmi bstree.mli # cached
|
||||
# Target: bstree.ml.depends, tags: { extension:ml, file:bstree.ml, ocaml, ocamldep, quiet }
|
||||
/home/flami/.opam/default/bin/ocamldep.opt -modules bstree.ml > bstree.ml.depends
|
||||
# Target: bstree.cmo, tags: { byte, compile, extension:cmo, extension:ml, file:bstree.cmo, file:bstree.ml, implem, ocaml, quiet }
|
||||
/home/flami/.opam/default/bin/ocamlc.opt -c -o bstree.cmo bstree.ml
|
||||
# Compilation successful.
|
||||
@@ -0,0 +1,86 @@
|
||||
module type OrderedType = sig
|
||||
type t
|
||||
val compare : t -> t -> int
|
||||
end
|
||||
|
||||
module type S = sig
|
||||
type elt
|
||||
type t
|
||||
|
||||
val size : t -> int
|
||||
val height : t -> int
|
||||
val empty : t
|
||||
val is_empty : t -> bool
|
||||
val insert : elt -> t -> t
|
||||
val of_list : elt list -> t
|
||||
val mem : elt -> t -> bool
|
||||
val delete : elt -> t -> t
|
||||
end
|
||||
|
||||
module Make(Ord : OrderedType) = struct
|
||||
type elt = Ord.t;;
|
||||
type t = L | N of elt * t * t;;
|
||||
|
||||
let rec size t =
|
||||
match t with
|
||||
| L -> 0
|
||||
| N (_, l, r) ->
|
||||
1 + size l + size r;;
|
||||
|
||||
let rec height t =
|
||||
match t with
|
||||
| L -> 0
|
||||
| N (_, l, r) ->
|
||||
1 + max (height l) (height r);;
|
||||
|
||||
let empty = L;;
|
||||
|
||||
let is_empty t = t = L;;
|
||||
|
||||
let rec insert x t =
|
||||
match t with
|
||||
| L -> N (x, L, L)
|
||||
| N (x', l, r) when Ord.compare x x' < 0 ->
|
||||
N (x', insert x l, r)
|
||||
| N (x', l, r) when Ord.compare x x' > 0 ->
|
||||
N (x', l, insert x r)
|
||||
| _ -> t;;
|
||||
|
||||
let of_list l =
|
||||
List.fold_left (Fun.flip insert) L l;;
|
||||
|
||||
let rec mem x t =
|
||||
match t with
|
||||
| L -> false
|
||||
| N (x', l, _) when Ord.compare x x' < 0 ->
|
||||
mem x l
|
||||
| N (x', _, r) when Ord.compare x x' > 0 ->
|
||||
mem x r
|
||||
| _ -> true;;
|
||||
|
||||
let rec largest t =
|
||||
match t with
|
||||
| L -> failwith "largest: empty tree"
|
||||
| N (x, _, L) -> x
|
||||
| N (_, _, r) -> largest r;;
|
||||
|
||||
let rec smallest t =
|
||||
match t with
|
||||
| L -> failwith "smallest: empty tree"
|
||||
| N (x, L, _) -> x
|
||||
| N (_, l, _) -> smallest l;;
|
||||
|
||||
let rec delete x t =
|
||||
match t with
|
||||
| L -> L
|
||||
| N (x', l, r) when Ord.compare x x' < 0 ->
|
||||
N (x', delete x l, r)
|
||||
| N (x', l, r) when Ord.compare x x' > 0 ->
|
||||
N (x', l, delete x r)
|
||||
| N (_, L, L) -> L (* this does not need to be here, its a special case but ill leave it for clarity *)
|
||||
| N (_, l, L) -> l
|
||||
| N (_, L, r) -> r
|
||||
| N (_, l, r) ->
|
||||
let succ = largest l in
|
||||
N (succ, delete succ l, r);;
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
bstree.ml: Fun List
|
||||
@@ -0,0 +1,20 @@
|
||||
module type OrderedType = sig
|
||||
type t
|
||||
val compare : t -> t -> int
|
||||
end
|
||||
|
||||
module type S = sig
|
||||
type elt
|
||||
type t
|
||||
|
||||
val size : t -> int
|
||||
val height : t -> int
|
||||
val empty : t
|
||||
val is_empty : t -> bool
|
||||
val insert : elt -> t -> t
|
||||
val of_list : elt list -> t
|
||||
val mem : elt -> t -> bool
|
||||
val delete : elt -> t -> t
|
||||
end
|
||||
|
||||
module Make(Ord: OrderedType) : S with type elt = Ord.t
|
||||
@@ -0,0 +1 @@
|
||||
bstree.mli:
|
||||
@@ -0,0 +1,86 @@
|
||||
module type OrderedType = sig
|
||||
type t
|
||||
val compare : t -> t -> int
|
||||
end
|
||||
|
||||
module type S = sig
|
||||
type elt
|
||||
type t
|
||||
|
||||
val size : t -> int
|
||||
val height : t -> int
|
||||
val empty : t
|
||||
val is_empty : t -> bool
|
||||
val insert : elt -> t -> t
|
||||
val of_list : elt list -> t
|
||||
val mem : elt -> t -> bool
|
||||
val delete : elt -> t -> t
|
||||
end
|
||||
|
||||
module Make(Ord : OrderedType) = struct
|
||||
type elt = Ord.t;;
|
||||
type t = L | N of elt * t * t;;
|
||||
|
||||
let rec size t =
|
||||
match t with
|
||||
| L -> 0
|
||||
| N (_, l, r) ->
|
||||
1 + size l + size r;;
|
||||
|
||||
let rec height t =
|
||||
match t with
|
||||
| L -> 0
|
||||
| N (_, l, r) ->
|
||||
1 + max (height l) (height r);;
|
||||
|
||||
let empty = L;;
|
||||
|
||||
let is_empty t = t = L;;
|
||||
|
||||
let rec insert x t =
|
||||
match t with
|
||||
| L -> N (x, L, L)
|
||||
| N (x', l, r) when Ord.compare x x' < 0 ->
|
||||
N (x', insert x l, r)
|
||||
| N (x', l, r) when Ord.compare x x' > 0 ->
|
||||
N (x', l, insert x r)
|
||||
| _ -> t;;
|
||||
|
||||
let of_list l =
|
||||
List.fold_left (Fun.flip insert) L l;;
|
||||
|
||||
let rec mem x t =
|
||||
match t with
|
||||
| L -> false
|
||||
| N (x', l, _) when Ord.compare x x' < 0 ->
|
||||
mem x l
|
||||
| N (x', _, r) when Ord.compare x x' > 0 ->
|
||||
mem x r
|
||||
| _ -> true;;
|
||||
|
||||
let rec largest t =
|
||||
match t with
|
||||
| L -> failwith "largest: empty tree"
|
||||
| N (x, _, L) -> x
|
||||
| N (_, _, r) -> largest r;;
|
||||
|
||||
let rec smallest t =
|
||||
match t with
|
||||
| L -> failwith "smallest: empty tree"
|
||||
| N (x, L, _) -> x
|
||||
| N (_, l, _) -> smallest l;;
|
||||
|
||||
let rec delete x t =
|
||||
match t with
|
||||
| L -> L
|
||||
| N (x', l, r) when Ord.compare x x' < 0 ->
|
||||
N (x', delete x l, r)
|
||||
| N (x', l, r) when Ord.compare x x' > 0 ->
|
||||
N (x', l, delete x r)
|
||||
| N (_, L, L) -> L (* this does not need to be here, its a special case but ill leave it for clarity *)
|
||||
| N (_, l, L) -> l
|
||||
| N (_, L, r) -> r
|
||||
| N (_, l, r) ->
|
||||
let succ = largest l in
|
||||
N (succ, delete succ l, r);;
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
module type OrderedType = sig
|
||||
type t
|
||||
val compare : t -> t -> int
|
||||
end
|
||||
|
||||
module type S = sig
|
||||
type elt
|
||||
type t
|
||||
|
||||
val size : t -> int
|
||||
val height : t -> int
|
||||
val empty : t
|
||||
val is_empty : t -> bool
|
||||
val insert : elt -> t -> t
|
||||
val of_list : elt list -> t
|
||||
val mem : elt -> t -> bool
|
||||
val delete : elt -> t -> t
|
||||
end
|
||||
|
||||
module Make(Ord: OrderedType) : S with type elt = Ord.t
|
||||
Whitespace-only changes.
@@ -0,0 +1,8 @@
|
||||
module / struct
|
||||
module type / sig / interface
|
||||
|
||||
open M
|
||||
let opem M in ..
|
||||
M.(...)
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
type 'a t = 'a list;;
|
||||
|
||||
exception Empty
|
||||
|
||||
let empty = [];;
|
||||
|
||||
let is_empty q = q = [];;
|
||||
|
||||
let enqueue x q = q @ [x];;
|
||||
|
||||
let dequeue q =
|
||||
match q with
|
||||
| [] -> raise Empty
|
||||
| _ :: xs -> xs;;
|
||||
|
||||
let dequeue_opt q =
|
||||
match q with
|
||||
| [] -> None
|
||||
| _ :: xs -> Some xs;;
|
||||
|
||||
let front q =
|
||||
match q with
|
||||
| [] -> raise Empty
|
||||
| x :: _ -> x;;
|
||||
|
||||
let front_opt q =
|
||||
match q with
|
||||
| [] -> None
|
||||
| x :: _ -> Some x;;
|
||||
|
||||
let length q =
|
||||
List.length q;;
|
||||
|
||||
let of_list l = l;;
|
||||
|
||||
let to_list q = q;;
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
(* queue is empty iff the first list is empty *)
|
||||
type 'a t = 'a list * 'a list;;
|
||||
|
||||
exception Empty;;
|
||||
|
||||
let empty = ([], []);;
|
||||
|
||||
let is_empty (l, _) = l = [];;
|
||||
|
||||
let enqueue x (l1, l2) =
|
||||
match l1 with
|
||||
| [] -> [x], l2
|
||||
| _ -> (l1, x :: l2);;
|
||||
|
||||
let dequeue (l1, l2) =
|
||||
match l1 with
|
||||
| [] -> raise Empty
|
||||
| [_] -> (List.rev l2, [])
|
||||
| _ :: xs -> (xs, l2);;
|
||||
|
||||
let dequeue_opt q =
|
||||
try
|
||||
Some (dequeue q)
|
||||
with
|
||||
| Empty -> None;;
|
||||
|
||||
let front (l, _) =
|
||||
match l with
|
||||
| [] -> raise Empty
|
||||
| x :: _ -> x;;
|
||||
|
||||
let front_opt (l, _) =
|
||||
match l with
|
||||
| [] -> None
|
||||
| x :: _ -> Some x;;
|
||||
|
||||
let of_list l =
|
||||
List.fold_left (Fun.flip enqueue) empty l;;
|
||||
|
||||
let to_list (l1, l2) =
|
||||
l1 @ List.rev l2;;
|
||||
|
||||
Reference in new issue
Block a user