lec 06 modules???
This commit is contained in:
15 files changed
+464
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user