restructure + lecutre 07

This commit is contained in:
SowinskiBraeden committed 2026-02-19 11:59:58 -08:00
1 parent b8c19aa341
commit 7c3198fee4
96 files changed
+458 -4

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
(* bstree with records *)
type 'a t = L | N of {v: 'a; l: 'a t; r: 'a t};;
+7
View File
@@ -0,0 +1,7 @@
let counter =
let c = ref 0 in
(fun () -> incr c; !c);;
let make_counter init =
let c = ref init in
(fun () -> let x = !c in incr c; x);;
+12
View File
@@ -0,0 +1,12 @@
module F = Map.Make(String)
let rec count map =
let word = Scanf.scanf " %s" (fun w -> w) in
if word = "" then map
else count (F.update word (function
| None -> Some 1
| Some n -> Some (n + 1))
map);;
let () =
List.iter (fun (s, n) -> Printf.printf "%s: %d\n" s n) @@ count F.empty;;
+46
View File
@@ -0,0 +1,46 @@
(** rank of node = shortest distance to a leaf (empty node)
* hence, rank of node = 1 + min(rnak of left, rank of right)
* leftist tree has 2 properties:
* For ever node
* * leftist: rnak(left) >= rank(right)
* * min-heap: value(parent) <= value(node)
*)
type 'a t = L | N of int * 'a * 'a t * 'a t;;
exception Empty;;
let rank = function
| L -> 0
| N (rk, _, _, _) -> rk;;
let empty = L;;
let is_empty t = t = L;;
let rec merge t1 t2 =
match t1, t2 with
| t, L | L, t -> t
| N (_, x1, _, _), N (_, x2, _, _) when x2 < x1 ->
merge t2 t1
| N (_, x, l, r), t ->
let t' = merge r t in
if rank t' > rank l then N (1 + rank l, x, t', l)
else N (1 + rank t', x, l, t');;
let insert x t =
merge t (N (1, x, L, L));;
let of_list l = List.fold_left (Fun.flip insert) L l;;
let get_min = function
| L -> raise Empty
| N (_, x, _, _) -> x;;
let delete_min = function
| L -> L
| N (_, _, l, r) -> merge l r;;
let rec to_list t =
if is_empty t then []
else get_min t :: to_list (delete_min t);;
+47
View File
@@ -0,0 +1,47 @@
(*
- a module can contain other modules as well as module type definitions
- a module type/sig (signature) can not contain modules but can contain other sigs
- for a .ml/.mli pair,
* anything not specified in .mli file is hidden
* everything specified in the .mli file must be defined in the .ml file
*)
module M = struct
let f x = 2 * x
end;;
module type S = module type of M;;
module M = struct
let f x = 2 * x;;
let g x = 3 * x;;
module N = struct
type t = int
end
end;;
(* S signature is the same as M if loaded in utop *)
module type S = module type of M;;
(***** EXTENDED MODULES *****)
(* lets extend List module *)
module ListExt = struct
include List;; (* include module to extend *)
let to_array l = Array.of_list l;;
end;;
ListExt.to_array [3;2;7;6;8];;
module F = Map.Make(String);;
(* returns abstract type *)
let m = F.empty |> F.add "hello" 1 |> F.add "hello" 2;;
F.to_list m;;
let incr key m =
F.update key (function | None -> Some 1 | Some x -> Some (x + 1))
m;;
let m = incr "hello" m;;
let m = incr "world" m;;
+16
View File
@@ -0,0 +1,16 @@
let isqrt n = n |> float_of_int |> sqrt |> int_of_float;;
let sieve n =
let is_prime = Array.make n true in
for i = 2 to isqrt n do
if is_prime.(i) then
let j = ref i in
while i * !j < n do
is_prime.(i * !j) <- false;
incr j
done
done;
is_prime.(0) <- false;
is_prime.(1) <- false;
is_prime |> Array.to_list |> List.mapi (fun i b -> (i, b)) |>
List.filter_map (fun (i, b) -> if b then Some i else None);;